springboot之定时任务(elaticjob)
springboot之定时任务(elaticjob)
1.导入所依赖的jar包
<dependency>
<groupId>com.dangdang</groupId>
<artifactId>elastic-job-lite-spring</artifactId>
<version>2.1.5</version>
</exclusions>
</dependency>
2.在配置文件(application.yml)中设置定时任务执行策略
elaticjob:
daycron : 0 */30 * 1 * ?
monthcron : 0 */30 * 1 * ?
transferCron : 0 */30 * 1 * ?
shardingTotalCount : 1
shardingItemParameters : 0=A
dayJobName: demo-etl-day-task
monthJobName: demo-etl-month-task
riskOriginalHisJobName:demo-roiginalHis-day-task
zookeeper:
namespace: job-demo-local
server-lists: 127.0.0.1:8080
sessiontimeout: 30000
3.配置监听器
/**
* ElasticJob监听器配置类
*/
@Slf4j
public class EasticJobListenerDemo implements ElasticJobListener {
@Override
public void beforeJobExecuted(ShardingContexts shardingContexts) {
long beginTime = System.currentTimeMillis();
log.info("===>{} JOB BEGIN TIME: {} <===", shardingContexts.getJobName(),
ToolUtils.timeStamp2Date(beginTime + "", ""));
}
@Override
public void afterJobExecuted(ShardingContexts shardingContexts) {
long endTime = System.currentTimeMillis();
log.info("===>{} JOB END TIME: {},TOTAL CAST: {} <===", shardingContexts.getJobName(),
ToolUtils.timeStamp2Date(endTime + "", ""));
}
}
4.配置ElasticJobConfig,设置监听器
/**
* ElasticJob定时任务配置类
*/
@Configuration
public class ElasticJobConfig {
@Value("${elaticjob.dayJobName}")
private String dayJobName;
@Value("${elaticjob.monthJobName}")
private String monthJobName;
@Value("${elaticjob.riskOriginalHisJobName}")
private String riskOriginalHisJobName;
@Inject
private ZookeeperRegistryCenter regCenter;
/**
* 配置任务监听器
*
* @return
*/
@Bean
public ElasticJobListener elasticJobListener() {
return new EasticJobListenerDemo ();
}
/**
* 配置任务详细信息
*
* @param jobClass
* @param cron
* @param shardingTotalCount
* @param shardingItemParameters
* @return
*/
private LiteJobConfiguration getLiteJobConfiguration(final Class<? extends SimpleJob> jobClass,
final String cron,
final int shardingTotalCount,
final String shardingItemParameters,
final String jobName) {
return LiteJobConfiguration.newBuilder(new SimpleJobConfiguration(
JobCoreConfiguration.newBuilder(jobName, cron, shardingTotalCount)
.shardingItemParameters(shardingItemParameters).build(),
jobClass.getCanonicalName())).overwrite(true).build();
}
@Bean(initMethod = "init")
public JobScheduler simpleJobScheduler(final CountryRiskIndexDayJob simpleJob,
@Value("${elaticjob.daycron}") final String cron,
@Value("${elaticjob.shardingTotalCount}") final int shardingTotalCount,
@Value("${elaticjob.shardingItemParameters}") final String shardingItemParameters) {
EasticJobListenerDemo elasticJobListener = new EasticJobListenerDemo ();
return new SpringJobScheduler(simpleJob, regCenter,
getLiteJobConfiguration(CountryRiskIndexDayJob.class, cron, shardingTotalCount, shardingItemParameters,
dayJobName),
elasticJobListener);
}
@Bean(initMethod = "init")
public JobScheduler simpleJobScheduler2(final CountryRiskIndexMonthJob simpleJob,
@Value("${elaticjob.monthcron}") final String cron,
@Value("${elaticjob.shardingTotalCount}") final int shardingTotalCount,
@Value("${elaticjob.shardingItemParameters}") final String shardingItemParameters) {
EasticJobListenerDemo elasticJobListener = new EasticJobListenerDemo ();
return new SpringJobScheduler(simpleJob, regCenter,
getLiteJobConfiguration(CountryRiskIndexMonthJob.class, cron, shardingTotalCount,
shardingItemParameters,
monthJobName),
elasticJobListener);
}
@Bean(initMethod = "init")
public JobScheduler simpleJobScheduler3(final RiskOriginalDataHisJob simpleJob,
@Value("${elaticjob.transferCron}") final String cron,
@Value("${elaticjob.shardingTotalCount}") final int shardingTotalCount,
@Value("${elaticjob.shardingItemParameters}") final String shardingItemParameters) {
EasticJobListenerDemo elasticJobListener = new EasticJobListenerDemo ();
return new SpringJobScheduler(simpleJob, regCenter,
getLiteJobConfiguration(RiskOriginalDataHisJob.class, cron, shardingTotalCount, shardingItemParameters,
riskOriginalHisJobName),
elasticJobListener);
}
}
本文地址:https://blog.csdn.net/weixin_38003467/article/details/113971028