步骤一下:jar 包
第二步 继承 IJobHandler
package com.example.job.task;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.IJobHandler;
import com.xxl.job.core.handler.annotation.JobHandler;
import com.xxl.job.core.log.XxlJobLogger;
import com.xxl.job.core.util.ShardingUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
/**
* 任务Handler示例(Bean模式)
*
* 开发步骤:
* 1、继承"IJobHandler":“com.xxl.job.core.handler.IJobHandler”;
* 2、注册到Spring容器:添加“@Component”注解,被Spring容器扫描为Bean实例;
* 3、注册到执行器工厂:添加“@JobHandler(value="自定义jobhandler名称")”注解,注解value值对应的是调度中心新建任务的JobHandler属性的值。
* 4、执行日志:需要通过 "XxlJobLogger.log" 打印执行日志;
*
* @author wangkc4 2019-2-26 14:49:38
*/
@JobHandler("testJobHandler")
@Component
public class TestJobHandler extends IJobHandler {
private final Logger logger = LoggerFactory.getLogger(getClass());
@Override
public ReturnT
// 分片参数
ShardingUtil.ShardingVO shardingVO = ShardingUtil.getShardingVo();
int index = shardingVO.getIndex();
int total = shardingVO.getTotal();
// xxl 日志
XxlJobLogger.log("分片参数:当前分片序号 = {}, 总分片数 = {}, 任务参数 = {}", index, total, param);
// spring 日志
logger.info("分片参数:当前分片序号 = {}, 总分片数 = {}, 任务参数 = {}", index, total, param);
// TODO 自己的业务
logger.info("【定时任务】开始,ing...");
// return new ReturnT<>(500, "参数不符合要求");
// return new ReturnT<>(200, "成功处理 " + count + " 条数据!");
return SUCCESS;
}
}
第三步 创建 XxlJobConfig 获取地址
package com.example.job.task.handler;
import com.xxl.job.core.executor.impl.XxlJobSpringExecutor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan(basePackages = "com.example.job.task")
public class XxlJobConfig {
private Logger logger = LoggerFactory.getLogger(XxlJobConfig.class);
@Value("${xxl.job.admin.addresses}")
private String adminAddresses;
@Value("${xxl.job.executor.appname}")
private String appName;
@Value("${xxl.job.executor.ip}")
private String ip;
@Value("${xxl.job.executor.port}")
private int port;
@Value("${xxl.job.accessToken}")
private String accessToken;
@Value("${xxl.job.executor.logpath}")
private String logPath;
@Value("${xxl.job.executor.logretentiondays}")
private int logRetentionDays;
@Bean(initMethod = "start", destroyMethod = "destroy")
public XxlJobSpringExecutor xxlJobExecutor() {
logger.info(">>>>>>>>>>> xxl-job config init.");
XxlJobSpringExecutor xxlJobSpringExecutor = new XxlJobSpringExecutor();
xxlJobSpringExecutor.setAdminAddresses(adminAddresses);
xxlJobSpringExecutor.setAppName(appName);
xxlJobSpringExecutor.setIp(ip);
xxlJobSpringExecutor.setPort(port);
xxlJobSpringExecutor.setAccessToken(accessToken);
xxlJobSpringExecutor.setLogPath(logPath);
xxlJobSpringExecutor.setLogRetentionDays(logRetentionDays);
return xxlJobSpringExecutor;
}
/**
* 针对多网卡、容器内部署等情况,可借助 "spring-cloud-commons" 提供的 "InetUtils" 组件灵活定制注册IP;
*
* 1、引入依赖:
*
*
*
*
*
*
* 2、配置文件,或者容器启动变量
* spring.cloud.inetutils.preferred-networks: 'xxx.xxx.xxx.'
*
* 3、获取IP
* String ip_ = inetUtils.findFirstNonLoopbackHostInfo().getIpAddress();
*/
}
第四步 配置文件
#redis 不配置就报错,在main类排除配置也没用(不配置也可以)
spring.redis.host: 10.16.88.34
spring.redis.port: 6380
spring.redis.database: 0
spring.redis.timeout: 5000
spring.redis.password: 123456
spring.redis.pool.max-idle: 0
spring.redis.pool.max-wait: -1
#任务调度
#任务调度服务地址以及端口
xxl.job.admin.addresses=http://10.16.87.104:9001
#执行名称
xxl.job.executor.appname=logistics-appName-job
#自动获取
xxl.job.executor.ip=
#默认端口9999
xxl.job.executor.port=9999
xxl.job.accessToken=
#日志输出地址
xxl.job.executor.logpath=./xxl-job-log
#日志保存天数:值大于3时生效,启用执行器Log文件定期清理功能,否则不生效
xxl.job.executor.logretentiondays=-1
第五步 运行 application
浏览器运行: http://10.16.87.104:9001
如下图: