SpringBoot集成Xxl-job
Setup 1: XXL-JOB服务
URL: https://xxxxxxxxxxxxxx/xxl-job-admin/
USERNAME: admin
PASSWORD: 123456
Setup 2: pom.xml
<dependency>
<groupId>com.xuxueli</groupId>
<artifactId>xxl-job-core</artifactId>
<version>2.1.2</version>
</dependency>
Setup 3: application.yml
xxl:
job:
executor:
logpath: /data/applogs/xxl-job/jobhandler
appname: demo-xxl-job
port: -1
logretentiondays: 7
ip: ""或者本地IP
admin:
addresses: https://xxxxxxxxx/xxl-job-admin
accessToken: admin123
Setup 4: XxljobConfig.java
package com.csot.itsm.config;
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.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;
@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;
}
}
Setup 5: 示例代码.java
import com.csot.itsm.facade.request.HelpDeskInfoConfigFacade;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Slf4j
@Component
public class DemoTask {
@Autowired
private DemoService demoService;
@XxlJob(value = "demoTask")
public ReturnT<String> execute(String params) {
try {
log.info("【定时任务】开始>>>>");
demoTask.execute();
log.info("【定时任务】结束<<<<");
return ReturnT.SUCCESS;
} catch (Exception e) {
log.error("【定时任务】异常-->" + e.getMessage());
return ReturnT.FAIL;
}
}
}