Skip to content

Commit

Permalink
Rename JobIdentificationStrategy to JobClassNameProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
nmyphp committed Jul 17, 2020
1 parent 03cae6c commit e9a736e
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import org.apache.shardingsphere.elasticjob.infra.handler.sharding.JobInstance;
import org.apache.shardingsphere.elasticjob.lite.api.listener.AbstractDistributeOnceElasticJobListener;
import org.apache.shardingsphere.elasticjob.lite.internal.guarantee.GuaranteeService;
import org.apache.shardingsphere.elasticjob.lite.internal.setup.JobIdentificationStrategyFactory;
import org.apache.shardingsphere.elasticjob.lite.internal.setup.JobClassNameProviderFactory;
import org.apache.shardingsphere.elasticjob.lite.internal.setup.SetUpFacade;
import org.apache.shardingsphere.elasticjob.reg.base.CoordinatorRegistryCenter;
import org.apache.shardingsphere.elasticjob.tracing.api.TracingConfiguration;
Expand Down Expand Up @@ -90,8 +90,8 @@ public JobScheduler(final CoordinatorRegistryCenter regCenter, final ElasticJob
this.tracingConfig = tracingConfig;
setUpFacade = new SetUpFacade(regCenter, jobConfig.getJobName(), this.elasticJobListeners);
schedulerFacade = new SchedulerFacade(regCenter, jobConfig.getJobName());
String jobIdentification = JobIdentificationStrategyFactory.getStrategy().identify(elasticJob, jobConfig.getJobName());
this.jobConfig = setUpFacade.setUpJobConfiguration(jobIdentification, jobConfig);
String jobClassName = JobClassNameProviderFactory.getProvider().getJobClassName(elasticJob, jobConfig.getJobName());
this.jobConfig = setUpFacade.setUpJobConfiguration(jobClassName, jobConfig);
setGuaranteeServiceForElasticJobListeners(regCenter, this.elasticJobListeners);
jobScheduleController = createJobScheduleController();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,16 @@
import org.apache.shardingsphere.elasticjob.api.ElasticJob;

/**
* Job identification strategy.
* Job class name provider.
*/
public interface JobIdentificationStrategy {
public interface JobClassNameProvider {

/**
* Identify job.
* Get job class name.
*
* @param elasticJob job instance
* @param jobName job name
* @return job identification
* @return job class name
*/
String identify(ElasticJob elasticJob, String jobName);
String getJobClassName(ElasticJob elasticJob, String jobName);
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,26 +20,32 @@
import lombok.AccessLevel;
import lombok.NoArgsConstructor;

import java.util.LinkedList;
import java.util.List;
import java.util.ServiceLoader;

/**
* Job identification strategy factory.
* Job class name provider factory.
*/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class JobIdentificationStrategyFactory {
public final class JobClassNameProviderFactory {

private static final JobIdentificationStrategy DEFAULT_STRATEGY = new JobClassNameIdentificationStrategy();
private static final List<JobClassNameProvider> PROVIDERS = new LinkedList<>();

private static final JobClassNameProvider DEFAULT_STRATEGY = new SimpleJobClassNameProvider();

static {
for (JobClassNameProvider each : ServiceLoader.load(JobClassNameProvider.class)) {
PROVIDERS.add(each);
}
}

/**
* Get job identification strategy.
* Get the first job class name provider.
*
* @return job identification strategy
* @return job class name provider
*/
public static JobIdentificationStrategy getStrategy() {
ServiceLoader<JobIdentificationStrategy> strategies = ServiceLoader.load(JobIdentificationStrategy.class);
if (strategies.iterator().hasNext()) {
return strategies.iterator().next();
}
return DEFAULT_STRATEGY;
public static JobClassNameProvider getProvider() {
return PROVIDERS.isEmpty() ? DEFAULT_STRATEGY : PROVIDERS.get(0);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,12 @@ public SetUpFacade(final CoordinatorRegistryCenter regCenter, final String jobNa
/**
* Set up job configuration.
*
* @param jobIdentification job identification
* @param jobClassName job class name
* @param jobConfig job configuration to be updated
* @return accepted job configuration
*/
public JobConfiguration setUpJobConfiguration(final String jobIdentification, final JobConfiguration jobConfig) {
return configService.setUpJobConfiguration(jobIdentification, jobConfig);
public JobConfiguration setUpJobConfiguration(final String jobClassName, final JobConfiguration jobConfig) {
return configService.setUpJobConfiguration(jobClassName, jobConfig);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@
import org.apache.shardingsphere.elasticjob.api.ElasticJob;

/**
* Identify job with job class name.
* Simple provider for get job class name.
*/
public class JobClassNameIdentificationStrategy implements JobIdentificationStrategy {
public final class SimpleJobClassNameProvider implements JobClassNameProvider {

@Override
public String identify(final ElasticJob elasticJob, final String jobName) {
public String getJobClassName(final ElasticJob elasticJob, final String jobName) {
return elasticJob.getClass().getName();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.junit.Assert.assertThat;

public class JobIdentificationStrategyFactoryTest {
public final class JobClassNameProviderFactoryTest {

@Test
public void assertGetDefaultStrategy() {
assertThat(JobIdentificationStrategyFactory.getStrategy(), instanceOf(JobClassNameIdentificationStrategy.class));
assertThat(JobClassNameProviderFactory.getProvider(), instanceOf(SimpleJobClassNameProvider.class));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,20 @@
package org.apache.shardingsphere.elasticjob.lite.spring.setup;

import org.apache.shardingsphere.elasticjob.api.ElasticJob;
import org.apache.shardingsphere.elasticjob.lite.internal.setup.JobIdentificationStrategy;
import org.apache.shardingsphere.elasticjob.lite.internal.setup.JobClassNameProvider;
import org.apache.shardingsphere.elasticjob.lite.spring.job.util.AopTargetUtils;
import org.springframework.aop.support.AopUtils;

/**
* Identify job with job class name.
* Provider for get job class name.
* <p>
* Consider the proxy object that generated by cglib or jdk dynamic proxy.
* </p>
*/
public class JobClassNameBySpringIdentificationStrategy implements JobIdentificationStrategy {
public final class SpringProxyJobClassNameProvider implements JobClassNameProvider {

@Override
public String identify(final ElasticJob elasticJob, final String jobName) {
public String getJobClassName(final ElasticJob elasticJob, final String jobName) {
return AopUtils.isAopProxy(elasticJob) ? AopTargetUtils.getTarget(elasticJob).getClass().getName() : elasticJob.getClass().getName();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@
# limitations under the License.
#

org.apache.shardingsphere.elasticjob.lite.spring.setup.JobClassNameBySpringIdentificationStrategy
org.apache.shardingsphere.elasticjob.lite.spring.setup.SpringProxyJobClassNameProvider
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@

package org.apache.shardingsphere.elasticjob.lite.spring.setup;

import org.apache.shardingsphere.elasticjob.lite.internal.setup.JobIdentificationStrategyFactory;
import org.apache.shardingsphere.elasticjob.lite.internal.setup.JobClassNameProviderFactory;
import org.junit.Test;

import static org.hamcrest.CoreMatchers.instanceOf;
import static org.junit.Assert.assertThat;

public class JobIdentificationStrategyFactoryTest {
public final class JobClassNameProviderFactoryTest {

@Test
public void assertGetStrategy() {
assertThat(JobIdentificationStrategyFactory.getStrategy(), instanceOf(JobClassNameBySpringIdentificationStrategy.class));
assertThat(JobClassNameProviderFactory.getProvider(), instanceOf(SpringProxyJobClassNameProvider.class));
}
}

0 comments on commit e9a736e

Please sign in to comment.