1、配置
1.1、pom文件 引入依赖
<dependency>
<groupId>com.xuxueli</groupId>
<artifactId>xxl-job-core</artifactId>
</dependency>
1.2、bootstrap.yml 增加xxljob读取配置
common-xxljob.yaml 内容如下:
xxl:
job:
admin:
addresses: http://my-xxl-job/xxl-job-admin
accessToken: c5c633666-244cdcds-cdsa-cds-cd-004
executor:
appname: ${spring.application.name}
1.3、增加XxlJobConfig
@Configuration
public class XxlJobConfig {
public XxlJobConfig(){
}
private static final Logger log = LoggerFactory.getLogger(XxlJobConfig.class);
@Value("${xxl.job.admin.addresses}")
private String adminAddresses;
@Value("${xxl.job.accessToken}")
private String accessToken;
@Value("${spring.application.name}")
private String appname;
@Bean
public XxlJobSpringExecutor xxlJobExecutor() {
XxlJobHelper.log(">>>>>>>>>>> xxl-job config init.", new Object[0]);
XxlJobSpringExecutor xxlJobSpringExecutor = new XxlJobSpringExecutor();
xxlJobSpringExecutor.setAdminAddresses(this.adminAddresses);
xxlJobSpringExecutor.setAppname(this.appname);
xxlJobSpringExecutor.setAccessToken(this.accessToken);
return xxlJobSpringExecutor;
}
@Bean
public JobService jobService() {
return new JobServiceImpl(this.xxlJobExecutor());
}
}
@Configuration
public class XxlJobConfig {
public XxlJobConfig(){
}
private static final Logger log = LoggerFactory.getLogger(XxlJobConfig.class);
@Value("${xxl.job.admin.addresses}")
private String adminAddresses;
@Value("${xxl.job.accessToken}")
private String accessToken;
@Value("${spring.application.name}")
private String appname;
@Bean
public XxlJobSpringExecutor xxlJobExecutor() {
XxlJobHelper.log(">>>>>>>>>>> xxl-job config init.", new Object[0]);
XxlJobSpringExecutor xxlJobSpringExecutor = new XxlJobSpringExecutor();
xxlJobSpringExecutor.setAdminAddresses(this.adminAddresses);
xxlJobSpringExecutor.setAppname(this.appname);
xxlJobSpringExecutor.setAccessToken(this.accessToken);
return xxlJobSpringExecutor;
}
@Bean
public JobService jobService() {
return new JobServiceImpl(this.xxlJobExecutor());
}
}
2、业务代码,试用@XxlJob注解,监听xxljob下发调度
3、xxljob建立调度
3.1 xxljob控制台可视化页面
3.2 增加调度器
3.3 创建调度
3.4 保存脚本,用于之后发布到别的环境
INSERT INTO `xxl_job_info` (`job_group`, `job_desc`, `add_time`, `update_time`, `author`, `alarm_email`, `schedule_type`, `schedule_conf`, `misfire_strategy`,
`executor_route_strategy`, `executor_handler`, `executor_param`, `executor_block_strategy`, `executor_timeout`,
`executor_fail_retry_count`, `glue_type`, `glue_source`, `glue_remark`, `glue_updatetime`, `child_jobid`, `trigger_status`,
`trigger_last_time`, `trigger_next_time`) VALUES ( (select id from xxl_job_group where app_name='dcm-app'),
'xxx生产费用-自动生成规则', '2023-03-13 09:57:24', '2023-03-13 10:17:05', 'admin', '', 'CRON', '* * * 1/1 * ?', 'DO_NOTHING',
'FIRST', 'DCM_PROJ_SPC', '', 'SERIAL_EXECUTION', '0', '0', 'BEAN', '', 'GLUE代码初始化', '2023-03-13 09:57:24', '', '0', '0', '0');
INSERT INTO `xxl_job_info` (`job_group`, `job_desc`, `add_time`, `update_time`, `author`, `alarm_email`, `schedule_type`, `schedule_conf`, `misfire_strategy`, `executor_route_strategy`, `executor_handler`, `executor_param`, `executor_block_strategy`, `executor_timeout`, `executor_fail_retry_count`, `glue_type`, `glue_source`, `glue_remark`, `glue_updatetime`, `child_jobid`, `trigger_status`, `trigger_last_time`, `trigger_next_time`) VALUES ( (select id from xxl_job_group where app_name='dcm-app'), '安全生产费用-自动生成规则', '2023-03-13 09:57:24', '2023-03-13 10:17:05', 'admin', '', 'CRON', '* * * 1/1 * ?', 'DO_NOTHING', 'FIRST', 'DCM_PROJ_SPC', '', 'SERIAL_EXECUTION', '0', '0', 'BEAN', '', 'GLUE代码初始化', '2023-03-13 09:57:24', '', '0', '0', '0');