1 任务调度中心搭建、部署、任务管理与监控[1]

2 SpringBoot 集成 xxl-job:创建调度任务
maven项目pom.xml引入依赖:
<dependency><groupId>com.xuxueli</groupId><artifactId>xxl-job-core</artifactId><version>2.2.0</version>
</dependency>
application.properties配置
### xxl-job 任务调度管理中心:web地址
#xxl.job.admin.addresses=http://192.168.4.20:8081/xxl-job-admin
### xxl-job 任务调度管理中心:登录用户名密码
xxl.job.login.username=admin
xxl.job.login.password=123456
### xxl-job 任务调度管理中心:通讯TOKEN,非空时启用
xxl.job.accessToken=
### xxl-job 任务调度管理中心:国际化设置,默认为中文版本,值设置为“en”时切换为英文版本 xxl-job, i18n (default empty as chinese, "en" as english)
xxl.job.i18n=### xxl-job 执行期配置
xxl.job.executor.appname=xxl-job
### xxl-job executor registry-address: default use address to registry , otherwise use ip:port if address is null
xxl.job.executor.address=
### xxl-job executor server-info
xxl.job.executor.ip=
xxl.job.executor.port=-1
### xxl-job executor log-path
xxl.job.executor.logpath=/XApp/logs/xxl-job/jobhandler
### xxl-job executor log-retention-days
xxl.job.executor.logretentiondays=30
Java配置文件:
package org.lyy.job.config;import com.xxl.job.core.executor.impl.XxlJobSpringExecutor;import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
import lombok.extern.slf4j.Slf4j;/*** Description: xxl-job 配置类 <br/>* author: lyy <br/>* version: 1.0*/
@Slf4j
@Configuration
public class XxlJobConfig {//private Logger logger = LoggerFactory.getLogger(XxlJobConfig.class);@Value("${xxl.job.admin.addresses}")private String adminAddresses;@Value("${xxl.job.accessToken}")private String accessToken;@Value("${xxl.job.executor.appname}")private String appname;@Value("${xxl.job.executor.address}")private String address;@Value("${xxl.job.executor.ip}")private String ip;@Value("${xxl.job.executor.port}")private int port;@Value("${xxl.job.executor.logpath}")private String logPath;@Value("${xxl.job.executor.logretentiondays}")private int logRetentionDays;@Beanpublic XxlJobSpringExecutor xxlJobExecutor() {logger.info("xxl-job config init ======>");XxlJobSpringExecutor xxlJobSpringExecutor = new XxlJobSpringExecutor();xxlJobSpringExecutor.setAdminAddresses(adminAddresses);xxlJobSpringExecutor.setAppname(appname);xxlJobSpringExecutor.setAddress(address);xxlJobSpringExecutor.setIp(ip);xxlJobSpringExecutor.setPort(port);xxlJobSpringExecutor.setAccessToken(accessToken);xxlJobSpringExecutor.setLogPath(logPath);xxlJobSpringExecutor.setLogRetentionDays(logRetentionDays);return xxlJobSpringExecutor;}
}
调度中心定义调度任务

代码编写调度任务
package org.lyy.job.handler;import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Component;import com.lyy.base.util.StringUtils;
import com.lyy.data.rpc.service.RpcStuService;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;import lombok.extern.slf4j.Slf4j;
/*** * 学情数据任务* 定时编辑工具:https://www.matools.com/cron/* @author lyy*/
@Slf4j
@Component
public class StuDataJob {private final SimpleDateFormat DATA_FORMAT = new SimpleDateFormat("yyyy-MM-dd ");@Reference(version = "${service.version}")private RpcStuService rpcStuService;/*** 每天 00:00:01 执行一次* @param date* @return* @throws Exception*/@XxlJob("stuJobHandler")public ReturnT<String> stuJobHandler(String date) throws Exception {log.info("enter:{}", date);Date filterDate;if (StringUtils.isNotEmpty(date)) {filterDate = DATA_FORMAT.parse(date);} else {Calendar cal = Calendar.getInstance();cal.add(Calendar.DATE, -1);filterDate = cal.getTime();}log.info("filterDate={}", DATA_FORMAT.format(filterDate));boolean flag = rpcStuService.batchSaveStuQuesOfDay(filterDate);log.info("return:{}", flag);return flag? ReturnT.SUCCESS : ReturnT.FAIL;}
}
参考
- ^https://www.cnblogs.com/liconglong/p/11753147.html