一 案例操作
1.1 新建工程
简单建一个springboot的工程,如下图结构
1.2 工程的配置
1.pom中依赖配置
2)代码
<!-- xxl-job -->
<dependency>
<groupId>com.xuxueli</groupId>
<artifactId>xxl-job-core</artifactId>
<version>2.3.0</version>
</dependency>
2.application的配置
server.port=8083
#----------xxl-job配置--------------
logging.config=classpath:logback.xml
#调度中心部署地址,多个配置逗号分隔 "http://address01,http://address02"
xxl.job.admin.addresses=http://127.0.0.1:8080/xxl-job-admin
#执行器token,非空时启用 xxl-job, access token
xxl.job.accessToken=jurf-xxl-job
# 执行器app名称,和控制台那边配置一样的名称,不然注册不上去
xxl.job.executor.appname=xxl-demo-dingshi
# [选填]执行器注册:优先使用该配置作为注册地址,为空时使用内嵌服务 ”IP:PORT“ 作为注册地址。
#从而更灵活的支持容器类型执行器动态IP和动态映射端口问题。
xxl.job.executor.address=
#[选填]执行器IP :默认为空表示自动获取IP(即springboot容器的ip和端口,可以自动获取,也可以指定),多网卡时可手动设置指定IP,该IP不会绑定Host仅作为通讯实用;地址信息用于 "执行器注册" 和 "调度中心请求并触发任务",
xxl.job.executor.ip=
# [选填]执行器端口号:小于等于0则自动获取;默认端口为9999,单机部署多个执行器时,注意要配置不同执行器端口;
xxl.job.executor.port=9995
#执行器日志文件存储路径,需要对该路径拥有读写权限;为空则使用默认路径
xxl.job.executor.logpath=./data/logs/xxl-job/executor
#执行器日志保存天数
xxl.job.executor.logretentiondays=30
3.logback的配置
logback的内容:
<?xml version="1.0" encoding="UTF-8"?>
<configuration debug="false" scan="true" scanPeriod="1 seconds">
<contextName>logback</contextName>
<property name="log.path" value="./data/logs/xxl-job/app.log"/>
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} %contextName [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<appender name="file" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${log.path}</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${log.path}.%d{yyyy-MM-dd}.zip</fileNamePattern>
</rollingPolicy>
<encoder>
<pattern>%date %level [%thread] %logger{36} [%file : %line] %msg%n
</pattern>
</encoder>
</appender>
<root level="info">
<appender-ref ref="console"/>
<appender-ref ref="file"/>
</root>
</configuration>
4.配置信息初始化
将配置文件中的信息加载获取,服务启动时候,进行初始化
package com.ljf.xxl.job.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;
/**
* @Version 1.0
**/
@Configuration
public class XxlJobConfig {
private Logger log = 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;
//旧版的有bug
//@Bean(initMethod = "start", destroyMethod = "destroy")
@Bean
public XxlJobSpringExecutor xxlJobExecutor() {
log.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;
}
}
5.编写定时任务
@XxlJob(value = "demoJobHandler", init = "init", destroy = "destroy")
此注解的value名称要和调度页面配置的一样。
1.3 调度页面配置
1.新增执行器
2.编写调度任务
3.启动服务,设置定时
1)启动服务
2)设置服务定时执行
3)看看console日志: 按照每隔5s进行执行。
二 案例2 一个执行器,多个定时任务
2.1 新增一个定时天气预报的定时任务
代码
package com.ljf.xxl.job.task;
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;
import java.time.LocalDateTime;
/**
* @ClassName: WeatherReportTask
* @Description: TODO
* @Author: liujianfu
* @Date: 2022/12/17 11:38:32
* @Version: V1.0
**/
@Component
public class WeatherReportTask {
private Logger log = LoggerFactory.getLogger(MyTask.class);
@XxlJob(value = "txHandler", init = "init", destroy = "destroy")
public void execute() {
String param = XxlJobHelper.getJobParam();
System.out.println("v2 param=" + param);
log.info("定时天气预报:" + LocalDateTime.now()+" 此刻为晴天....");
XxlJobHelper.log("v2这个是执行日志:param=" + param);
//XxlJobHelper.handleFail("自定义错误,任务执行失败");
XxlJobHelper.handleSuccess("任务执行成功");
//return ReturnT.SUCCESS;
}
private void init() {
log.info("init 方法调用成功");
}
private void destroy() {
log.info("destroy 方法调用成功");
}
}
3.将其帮到同一个执行器中
4.启动服务,设置定时启动
1启动工程服务
2.启动天气预报定时任务
3.查看console日志