Commit 8a4e34ae authored by 杨锐's avatar 杨锐

解决elastic job重复执行问题;

ElasticJobAspect 新增log;
parent b6fa946b
...@@ -425,11 +425,14 @@ public class BraveAutoConfiguration { ...@@ -425,11 +425,14 @@ public class BraveAutoConfiguration {
@Around("target(com.dangdang.ddframe.job.api.ElasticJob)") @Around("target(com.dangdang.ddframe.job.api.ElasticJob)")
public Object dynamicTrace(ProceedingJoinPoint pjp) throws Throwable { public Object dynamicTrace(ProceedingJoinPoint pjp) throws Throwable {
log.info("ElasticJobAspect dynamicTrace start.");
Tracer tracer = tracing().tracer(); Tracer tracer = tracing().tracer();
/* 防止应用层非法使用(上下文中存在span),不生成root span。 */ /* 防止应用层非法使用(上下文中存在span),不生成root span。 */
if (tracer.currentSpan() != null) { if (tracer.currentSpan() != null) {
log.info("应用层上下文中存在span.");
return pjp.proceed(); return pjp.proceed();
} }
log.info("应用层上下文中不存在span.");
brave.Span span = tracer.newTrace(); brave.Span span = tracer.newTrace();
try (Tracer.SpanInScope ws = tracer.withSpanInScope(span)) { try (Tracer.SpanInScope ws = tracer.withSpanInScope(span)) {
// note: try-with-resources closes the scope *before* the catch block // note: try-with-resources closes the scope *before* the catch block
......
...@@ -47,25 +47,26 @@ public class ElasticJobAutoConfiguration { ...@@ -47,25 +47,26 @@ public class ElasticJobAutoConfiguration {
for (Map.Entry<String, SimpleJob> entry : map.entrySet()) { for (Map.Entry<String, SimpleJob> entry : map.entrySet()) {
SimpleJob simpleJob = entry.getValue(); SimpleJob simpleJob = entry.getValue();
ElasticSimpleJob elasticSimpleJobAnnotation = AopUtils.getTargetClass(simpleJob).getAnnotation(ElasticSimpleJob.class); ElasticSimpleJob elasticSimpleJobAnnotation = AopUtils.getTargetClass(simpleJob).getAnnotation(ElasticSimpleJob.class);
if(elasticSimpleJobAnnotation != null){
String cron = StringUtils.defaultIfBlank(elasticSimpleJobAnnotation.cron(), elasticSimpleJobAnnotation.value());
SimpleJobConfiguration simpleJobConfiguration = new SimpleJobConfiguration(JobCoreConfiguration.newBuilder(elasticSimpleJobAnnotation.jobName(), cron, elasticSimpleJobAnnotation.shardingTotalCount()).shardingItemParameters(elasticSimpleJobAnnotation.shardingItemParameters()).build(), elasticSimpleJobAnnotation.jobName());
LiteJobConfiguration liteJobConfiguration = LiteJobConfiguration.newBuilder(simpleJobConfiguration).overwrite(true).build();
String cron = StringUtils.defaultIfBlank(elasticSimpleJobAnnotation.cron(), elasticSimpleJobAnnotation.value()); String dataSourceRef = elasticSimpleJobAnnotation.dataSource();
SimpleJobConfiguration simpleJobConfiguration = new SimpleJobConfiguration(JobCoreConfiguration.newBuilder(simpleJob.getClass().getName(), cron, elasticSimpleJobAnnotation.shardingTotalCount()).shardingItemParameters(elasticSimpleJobAnnotation.shardingItemParameters()).build(), simpleJob.getClass().getCanonicalName()); if (StringUtils.isNotBlank(dataSourceRef)) {
LiteJobConfiguration liteJobConfiguration = LiteJobConfiguration.newBuilder(simpleJobConfiguration).overwrite(true).build();
String dataSourceRef = elasticSimpleJobAnnotation.dataSource(); if (!applicationContext.containsBean(dataSourceRef)) {
if (StringUtils.isNotBlank(dataSourceRef)) { throw new RuntimeException("not exist datasource [" + dataSourceRef + "] !");
}
if (!applicationContext.containsBean(dataSourceRef)) { DataSource dataSource = (DataSource) applicationContext.getBean(dataSourceRef);
throw new RuntimeException("not exist datasource [" + dataSourceRef + "] !"); JobEventRdbConfiguration jobEventRdbConfiguration = new JobEventRdbConfiguration(dataSource);
SpringJobScheduler jobScheduler = new SpringJobScheduler(simpleJob, regCenter, liteJobConfiguration, jobEventRdbConfiguration);
jobScheduler.init();
} else {
SpringJobScheduler jobScheduler = new SpringJobScheduler(simpleJob, regCenter, liteJobConfiguration);
jobScheduler.init();
} }
DataSource dataSource = (DataSource) applicationContext.getBean(dataSourceRef);
JobEventRdbConfiguration jobEventRdbConfiguration = new JobEventRdbConfiguration(dataSource);
SpringJobScheduler jobScheduler = new SpringJobScheduler(simpleJob, regCenter, liteJobConfiguration, jobEventRdbConfiguration);
jobScheduler.init();
} else {
SpringJobScheduler jobScheduler = new SpringJobScheduler(simpleJob, regCenter, liteJobConfiguration);
jobScheduler.init();
} }
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment