首先这个是基于docker的所以需要进行docker配置
1、先导入官方提供的SQL到虚拟机中mysql中
2、创建容器
docker run -e PARAMS="--spring.datasource.url=jdbc:mysql://192.168.211.136:3306/xxl_job?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&serverTimezone=Asia/Shanghai --spring.datasource.username=root --spring.datasource.password=123456 --spring.datasource.driver-class-name=com.mysql.jdbc.Driver" -p 8080:8080 -v /tmp:/data/applogs --name xxl-job-admin -d --restart=always xuxueli/xxl-job-admin:2.1.2
3、在你的微服务中添加依赖
<dependency>
<groupId>com.xuxueli</groupId>
<artifactId>xxl-job-core</artifactId>
<version>2.1.2</version>
</dependency>
4、在你的微服务的yml中和spring平级添加如下内容:
xxl:
job:
accessToken: ''
admin:
addresses: http://192.168.211.136:8080/xxl-job-admin
executor:
appname: leadnews-admin # 本服务的名称
ip: '' # 不写会自动确定
logretentiondays: 30
port: -1 # 不写会自动找一个端口
5、在你的微服务中添加配置(和pojo、mapper目录平级),新建一个config目录,其中存放如下内容:
package com.jjw.admin.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;
@Configuration
public class XxlJobConfig {
private Logger logger = 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:abc}")
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.setIp(ip);
xxlJobSpringExecutor.setPort(port);
xxlJobSpringExecutor.setAccessToken(accessToken);
xxlJobSpringExecutor.setLogPath(logPath);
xxlJobSpringExecutor.setLogRetentionDays(logRetentionDays);
return xxlJobSpringExecutor;
}
}
6、在你的微服务中和config目录平级创建task目录,内容为:
package com.jjw.admin.task;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.springframework.stereotype.Component;
import java.util.Date;
@Component
public class TestTask {
@XxlJob(value="teskTask")
public ReturnT<String> teskTask(String param){
System.out.println("执行任务======="+new Date());
//XxlJobLogger.log("XXL-JOB, Hello World.demo1:"+param);
//System.out.println("参数打印==="+param);
//
//System.out.println("now:end:"+new Date());
return ReturnT.SUCCESS;
}
}
7、在浏览器中打开 http://192.168.211.136:8080/xxl-job-admin/
8、新增一个执行器
9、新建任务
10、运行你的微服务
11、启动定时器
文章来源:https://www.toymoban.com/news/detail-507533.html
12、结果展示
文章来源地址https://www.toymoban.com/news/detail-507533.html
到了这里,关于java中的xxl-job-core完成定时任务的步骤的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!