源码下载地址
https://gitee.com/xuxueli0323/xxl-job
xxl开元社区地址
https://www.xuxueli.com/page/projects.html
项目模块介绍
架构思想
admin后台负责管理执行器和定时任务的增删改查 ,执行器管理会保存执行器的地址,可以自动录入,即执行器启动时会自动向后台注册,也可以手动录入 执行器即具体的逻辑代码部分,需要引入核心代码依赖,且需要把任务代码逻辑的类放入spring容器 ,执行器配置会保存后台的地址,
部署项目
后台管理部署
将xxl-job-admin 模块打包成jar包启动即可 application.properties 配置文件解释
server.port=8080
server.servlet.context-path=/xxl-job-admin
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/xxl_job?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&serverTimezone=Asia/Shanghai
spring.datasource.username=root
spring.datasource.password=root_pwd
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.mail.host=smtp.qq.com
spring.mail.port=25
spring.mail.username=xxx@qq.com
spring.mail.password=xxx
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true
spring.mail.properties.mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory
xxl.job.accessToken=
xxl.job.i18n=zh_CN
xxl.job.triggerpool.fast.max=200
xxl.job.triggerpool.slow.max=100
xxl.job.logretentiondays=30
调度中心访问地址:http://localhost:8080/xxl-job-admin (该地址执行器将会使用到,作为回调地址) 默认登录账号 “admin/123456 ”, 登录后运行界面如下图所示,修改密码 代码DigestUtils.md5DigestAsHex(xxlJobUser.getPassword().getBytes())
执行器管理
新增配置解读
- AppName: 为下面执行器配置的xxl.job.executor.appname
- 名称: 随意
- 注册方式 : 可以自动,也可以手动
- 机器地址: 执行器的地址,如果自动没有注册到,就改改为手动填写http://127.0.0.1:9999/,ip和端口分别对应下面执行器配置的
xxl.job.executor.ip和xxl.job.executor.port
任务管理
- 执行器: 即执行器管理添加的执行器
- 任务描述、负责人、报警邮件等:根据需要填写
- 调度类型:选CRON接口
- Cron:定时任务的七子表达式
- 运行模式:选择BEAN即可,任务以JobHandler方式维护在执行器端;需要结合 “JobHandler” 属性匹配执行器中任务;
- JobHandler: 任务执行逻辑,下面执行器中执行类的@XxlJob(“testQuartz”)注解的值testQuartz
部署执行器(需要集成到任务代码的项目中)
需要引入依赖
<dependency>
<groupId>com.xuxueli</groupId>
<artifactId>xxl-job-core</artifactId>
<version>2.3.0</version>
</dependency>
需要添加的配置文件内容
xxl.job.admin.addresses=http://127.0.0.1:8080/xxl-job-admin
xxl.job.accessToken=
xxl.job.executor.appname=xxl-job-executor-sample
xxl.job.executor.address=http://localhost:8080/xxl-job-admin
xxl.job.executor.ip=
xxl.job.executor.port=9999
xxl.job.executor.logpath=/data/applogs/xxl-job/jobhandler
xxl.job.executor.logretentiondays=30
配置类
packa quartz;
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;
}
}
定时任务代码逻辑类
package quartz;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.springframework.stereotype.Component;
@Component
public class ExecutorTest {
@XxlJob("testQuartz") // 值为在后台新增定时任务时的JobHandler
public void testQuartz(){
System.out.println("执行的定时任务");
}
}
|