自己动手,搭建
1 依赖
<!– xxl-job-core –>
<dependency>
<groupId>com.xuxueli</groupId>
<artifactId>xxl-job-core</artifactId>
</dependency>
2 代码
config配置类 XxlJobConfig
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;
/**
* xxl-job config
*
*/
@Configuration
@Slf4j
public class XxlJobConfig {
@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() {
log.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;
}
}
新建测试类 XxlJobTasks
import cn.xxxxx.monitor.server.GlobalUtils;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import com.xxl.job.core.log.XxlJobLogger;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
/**
* 定时任务
* @author itwolf
* @date 2021-11-17
*/
@Component
@Slf4j
public class XxlJobTasks {
@XxlJob(“helloWorldJobHandler”)
public ReturnT<String> execute(String param) throws Exception {
String info = “xxl-job, Hello World,” + GlobalUtils.PROJECT_NAME;
log.info(info);
XxlJobLogger.log(info);
return ReturnT.SUCCESS;
}
}
3 配置
nacos的配置
xxxxx-infinite-monitor-rest.yaml
xxl:
job:
accessToken: DCA761C3D5F711EA85A8246E96755CF8
admin:
addresses: http://localhost:8006/xxl-job-admin
executor:
address:
appname: xxxxx-infinite-monitor-rest
ip:
logpath: log/xxl-job/jobhandler
logretentiondays: 30
port: 9999
4 搭建xxljob
官网文档:https://www.xuxueli.com/xxl-job/
下载地址:
https://github.com/xuxueli/xxl-job.git
https://gitee.com/xuxueli0323/xxl-job.git
xxl-job-admin的配置文件
application.properties
### web server.port=8006 server.servlet.context-path=/xxl-job-admin ### actuator management.server.servlet.context-path=/actuator management.health.mail.enabled=false ### resources spring.mvc.servlet.load-on-startup=0 spring.mvc.static-path-pattern=/static/** spring.resources.static-locations=classpath:/static/ ### freemarker spring.freemarker.templateLoaderPath=classpath:/templates/ spring.freemarker.suffix=.ftl spring.freemarker.charset=UTF-8 spring.freemarker.request-context-attribute=request spring.freemarker.settings.number_format=0.########## ### mybatis mybatis.mapper-locations=classpath:/mybatis-mapper/*Mapper.xml #mybatis.type-aliases-package=com.xxl.job.admin.core.model ### xxl-job, datasource 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=123456 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver ### datasource-pool spring.datasource.type=com.zaxxer.hikari.HikariDataSource spring.datasource.hikari.minimum-idle=10 spring.datasource.hikari.maximum-pool-size=30 spring.datasource.hikari.auto-commit=true spring.datasource.hikari.idle-timeout=30000 spring.datasource.hikari.pool-name=HikariCP spring.datasource.hikari.max-lifetime=900000 spring.datasource.hikari.connection-timeout=10000 spring.datasource.hikari.connection-test-query=SELECT 1 spring.datasource.hikari.validation-timeout=1000 ### xxl-job, email spring.mail.host=smtp.qq.com spring.mail.port=25 spring.mail.username=xxx@qq.com spring.mail.from=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, access token xxl.job.accessToken=DCA761C3D5F711EA85A8246E96755CF8 ### xxl-job, i18n (default is zh_CN, and you can choose "zh_CN", "zh_TC" and "en") xxl.job.i18n=zh_CN ## xxl-job, triggerpool max size xxl.job.triggerpool.fast.max=200 xxl.job.triggerpool.slow.max=100 ### xxl-job, log retention days xxl.job.logretentiondays=30
5 启动xxl-job服务,登陆xxjob地址,比如本地地址:http://localhost:8006/xxl-job-admin
添加执行器管理,再添加任务管理,helloWorldJobHandler以及其他参数
检查配置和代码有没有问题
然后在启动xxxxx-infinite-monitor-rest程序,看控制台日志
这是xxl-job 启动日志
看xxxxx-infinite-monitor-rest启动,是否注册成功
**************部分日志**************
2021-11-16 20:50:53,821 []-[]-[]-[]-[xxl-job, executor ExecutorRegistryThread] [com.xxl.job.core.thread.ExecutorRegistryThread] [INFO] (ExecutorRegistryThread.java:87)- >>>>>>>>>>> xxl-job registry-remove success, registryParam:RegistryParam{registryGroup=’EXECUTOR’, registryKey=’xxxxx-infinite-monitor-server’, registryValue=’http://10.5.7.132:9999/’}, registryResult:ReturnT [code=200, msg=null, content=null]
2021-11-16 20:50:53,822 []-[]-[]-[]-[xxl-job, executor ExecutorRegistryThread] [com.xxl.job.core.thread.ExecutorRegistryThread] [INFO] (ExecutorRegistryThread.java:105)- >>>>>>>>>>> xxl-job, executor registry thread destory.
2021-11-16 20:50:53,822 []-[]-[]-[]-[Thread-38] [com.xxl.job.core.server.EmbedServer] [INFO] (EmbedServer.java:125)- >>>>>>>>>>> xxl-job remoting server destroy success.