1、找到XXL-job官网去下代码
https://github.com/xuxueli/xxl-job
2、下载下来用IDEA打开,你会得到这样子的目录结构
3、打开doc目录下面有个db,在你数据库里面创建对应的数据库
4、运行服务端 xxl-job-admin
登录进去账号admin 密码123456
好了,现在我们服务端启动好了
5、创建一个springboot项目
application.properties
# \u5E94\u7528\u670D\u52A1 WEB \u8BBF\u95EE\u7AEF\u53E3
server.port=8089
#xx-job \u914D\u7F6E\u4FE1\u606F 这你放你的服务端地址
xxl.job.admin.addresses=http://localhost:8080/xxl-job-admin
### xxl-job, access token
xxl.job.accessToken=default_token
### xxl-job executor appname 这个是你创建执行器的名字
xxl.job.executor.appname=test
### 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=9999
### xxl-job executor log-path
xxl.job.executor.logpath=/data/applogs/xxl-job/jobhandler
### xxl-job executor log-retention-days
xxl.job.executor.logretentiondays=30
xxjobconfig
package com.xxjob.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 XxjobConfig {
private Logger logger = LoggerFactory.getLogger(XxjobConfig.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();
*/
}
controller
package com.xxjob.controller;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class jobController {
@XxlJob("MyJobHandler")
public void execute() throws Exception {
System.out.println("我成功的执行了xxjob定时任务!!!!");
}
}
目录结构为这样
6、回到我们刚才的页面
去新增一个执行器管理
APPName填写你application里面的那个appnanme
名称是一个类似于备注
注册选择自动注册
机器地址可以不填写
7、创建好之后
选择你刚刚创建的那个执行器,然后新增任务
8、新增任务
选择你的执行器,任务描述,负责人填写
然后Cron表达式,表示任务执行的时间
重点是JobHandler对应后台 @XxlJob("MyJobHandler")里面手写的这个handler
然后默认保存
9、操作
执行一次,就能在你控制台就看到输出
启动就是按照你设置的cron表达式去执行这个任务。