文档
上github官方说明文档 https://github.com/xuxueli/xxl-job
需要准备的环境
Maven3+ Jdk1.8+ Mysql5.7+
下载安装包
解压后获取初始化sql
初始化数据库和表
解压后用idea打开
配置调度中心
改下xxl-job-admin模块的数据库地址和tomcat端口,然后logback日志路径可以自定义修改
然后打成jar包,放到服务上java -jar跑起来就行了 默认登录账号 “admin/123456”
配置xxl-job包里自带的执行器,一会可以做测试,已有项目的配置在下面
依旧打成jar包,然后跑起来 这个属性和配置执行器有关
xxl.job.executor.appname=xxl-job-executor-sample
已有项目配置xxl-job
引入依赖,和当前调度器执行器版本一致
<dependency>
<groupId>com.xuxueli</groupId>
<artifactId>xxl-job-core</artifactId>
<version>2.3.0</version>
</dependency>
配置XxlJobSpringExecutor参数
package com.fchan.springcloudstream.xxl;
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;
@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;
}
}
参数demo
xxl:
job:
accessToken: ''
admin:
addresses: http://xxx.xxx.xxx.xxx:30020/xxl-job-admin
executor:
ip: ''
address: ''
appname: xxl-job-executor-sample
logpath: /home/ubuntu/app/xxljob/logs/jobhandler
logretentiondays: 30
port: 30022
配置一个自定义任务
package com.fchan.springcloudstream.xxl;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
@Component
public class MyJob {
private Logger logger = LoggerFactory.getLogger(MyJob.class);
@XxlJob(value = "test1JobHandler")
public void test1(){
XxlJobHelper.log("my-JOB, Hello World.");
String command = XxlJobHelper.getJobParam();
XxlJobHelper.log("传入的参数");
logger.info("command:" + command);
}
}
配置执行器
配置任务
执行一波测试 成功~
|