一、介绍
首先我们是已经在其他的项目启动了xxl-job 我们只是在新项目中引用xxl-job的简单流程
二、最佳实践
2.1、引入依赖
<!-- xxljob start-->
<dependency>
<groupId>com.xuxueli</groupId>
<artifactId>xxl-job-core</artifactId>
<version>2.2.0</version>
</dependency>
2.2、application 加配置
#-----------------------------------xxlJob 配置-------------------
xxl.job.accessToken =
xxl.job.executor.appname = name
#xxl.job.executor.address=
xxl.job.executor.port = 10100
xxl.job.executor.logpath = /data/applogs/xxl-job/jobhandler
xxl.job.executor.logretentiondays = 30
#-----------------------12.xxl-job-admin调度中心地址----------------------------------------------------#
xxl.job.admin.addresses = http://xxx:8184/xxl-job-admin
#xxl.job.executor.ip=xxx xxx
xxl.job.executor.ip = xxx
2.3、加配置文件
package com.qc.platform.service.biz.xxlJob.config;
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.Configuration;
/**
* xxl-job config
*
* @author xuxueli 2017-04-28
*/
@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;
@Bean
public 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;
}
/**
* 针对多网卡、容器内部署等情况,可借助 "spring-cloud-commons" 提供的 "InetUtils" 组件灵活定制注册IP;
*
* 1、引入依赖:
* <dependency>
* <groupId>org.springframework.cloud</groupId>
* <artifactId>spring-cloud-commons</artifactId>
* <version>${version}</version>
* </dependency>
*
* 2、配置文件,或者容器启动变量
* spring.cloud.inetutils.preferred-networks: 'xxx.xxx.xxx.'
*
* 3、获取IP
* String ip_ = inetUtils.findFirstNonLoopbackHostInfo().getIpAddress();
*/
}
2.4、BEAN模式(方法形式) 编写调度任务
package com.qc.schedule.provider.config.xxl;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import com.xxl.job.core.log.XxlJobLogger;
import org.springframework.stereotype.Component;
/**
* @Classname DataHandlerJob
* @Description data的任务
* @Date 2021/7/30 17:10
* @Created by dongzhiwei
*/
@Component
public class DataHandlerJob {
@XxlJob("testJob")
public ReturnT<String> myFirstTask(String param) throws Exception {
XxlJobLogger.log("myFirstTsk");
System.out.println("定时任务开始------");
System.out.println(param);
return ReturnT.SUCCESS;
}
}
2.5、添加job
三、总结
其实只是在新项目中引入还是挺简单的,这里只是记录一下。