《官网下载xxl-job源码》
首先去官网下载2.3.0源码
用idea打开,maven加载依赖。
创建xxl_job数据库并导入数据
修改application.properties
server.port=84
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=x
spring.datasource.password=x
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
xxl-job启动起来先不用管
自己的项目引入xxl-job依赖
pom.xml
<dependency>
<groupId>com.xuxueli</groupId>
<artifactId>xxl-job-core</artifactId>
<version>2.3.0</version>
</dependency>
application.yml
xxl:
job:
admin:
addresses: http://127.0.0.1:84/xxl-job-admin
executor:
appname: xxl-job-invoice-api
logretentiondays: 30
port: 9999
XxlJobConfig.java 配置文件
import com.xxl.job.core.executor.impl.XxlJobSpringExecutor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Slf4j
@Configuration
public class XxlJobConfig {
@Value("${xxl.job.admin.addresses}")
private String adminAddresses;
@Value("${xxl.job.executor.appname}")
private String appname;
@Value("${xxl.job.executor.port}")
private int port;
@Value("${xxl.job.executor.logretentiondays}")
private int logRetentionDays;
@Bean
public XxlJobSpringExecutor xxlJobExecutor() {
XxlJobSpringExecutor xxlJobSpringExecutor = new XxlJobSpringExecutor();
xxlJobSpringExecutor.setAdminAddresses(adminAddresses);
xxlJobSpringExecutor.setAppname(appname);
xxlJobSpringExecutor.setPort(port);
xxlJobSpringExecutor.setLogRetentionDays(logRetentionDays);
return xxlJobSpringExecutor;
}
}
定时任务
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
@Slf4j
@Component
public class DemoXxlJob {
@XxlJob("demoXxlJob")
public void demoXxlJob(){
log.info("这是一个由Xxl-job调度的定时任务");
}
}
启动自己的项目
访问localhost:84/xxl-job-admin
账号:admin 密码:123456
创建执行器
创建任务管理
启动
demo项目控制台会每隔5秒打印一次
|