1. POM is introduced. Note: the version number should be consistent with XXL job admin.
<dependency>
<groupId>com.xuxueli</groupId>
<artifactId>xxl-job-core</artifactId>
<version>1.9.2-SNAPSHOT</version>
</dependency>
2、application.yml
xxl:
job:
admin:
addresses: http://47.102.168.36:9500
executor:
log-path: ${catalina.home:/usr}/logs/job
log-retention-days: 30
ip: 127.0.0.1
port: 10001
3. Configure executor bean
import com.xxl.job.core.executor.XxlJobExecutor;
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;
/**
* xxl-job config
*/
@Configuration
public class XxlJobConfig {
private Logger logger = LoggerFactory.getLogger(XxlJobConfig.class);
@Value("${xxl.job.admin.addresses}")
private String adminAddresses;
@Value("${spring.application.name}")
private String appName;
@Value("${xxl.job.executor.ip}")
private String ip;
@Value("${xxl.job.executor.port}")
private Integer port;
@Value("${xxl.job.executor.log-path}")
private String logPath;
@Value("${xxl.job.executor.log-retention-days}")
private int logRetentionDays;
@Bean(initMethod = "start", destroyMethod = "destroy")
public XxlJobExecutor xxlJobExecutor() {
logger.info(">>>>>>>>>>> xxl-job config init.");
XxlJobExecutor xxlJobExecutor = new XxlJobExecutor();
xxlJobExecutor.setAdminAddresses(adminAddresses);
xxlJobExecutor.setAppName(appName);
xxlJobExecutor.setIp(ip);
xxlJobExecutor.setPort(port);
xxlJobExecutor.setPort(-1);
xxlJobExecutor.setAccessToken(null);
xxlJobExecutor.setLogPath(logPath);
xxlJobExecutor.setLogRetentionDays(logRetentionDays);
return xxlJobExecutor;
}
}
4. Task code
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.IJobHandler;
import com.xxl.job.core.handler.annotation.JobHandler;
import com.xxl.job.core.log.XxlJobLogger;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
/**
* JOB
*/
@JobHandler(value = "iFlyTenantJob")
@Component
public class IFlyTenantJob extends IJobHandler {
private static final Logger log = LoggerFactory.getLogger(IFlyTenantJob.class);
@Override
public ReturnT<String> execute(String s) {
XxlJobLogger.log("The mission begins...") ;
log.info("Task started...");
iFlyAiService.queryTenants();
try {
Thread.sleep(6000);
//int a = 1/0;
} catch (InterruptedException e) {
e.printStackTrace();
}
XxlJobLogger.log("End of mission...");
log.info("End of mission...");
return ReturnT.SUCCESS;
}
}
Read More:
- [Solved] ssm Error: Error querying database. Cause: org.apache.ibatis.executor.ExecutorException: Executor was closed
- [Solved] org.apache.ibatis.executor.ExecutorException: No constructor found in void matching [java.lang.String]
- The number of control threads and concurrency number of concurrent executor of Java starting gun
- Java callback function implementation case
- [Solved] In case of “transactionmanager” while setting bean property “transactionmanager” error
- C++ String case conversion and transform, tower, upper, usage
- Asynchronous callback case of Java callback function
- mybatis “case when” Error: Failed to process, please exclude the tableName or statementId.
- Idea unified setting code to UTF-8 code and solution of Tomcat garbled code
- [Solved] The main method caused an error: Could not deploy Yarn job cluster.
- [Solved] Hadoop Error: Exception in thread “main“ java.io.IOException: Error opening job jar: /usr/local/hadoop-2.
- OpenFeign Error: {“code“:1,“msg“:“JSON parse error: Illegal character ((CTRL-CHAR, code 31))
- How to Solve SSM JSON Chinese Messy Code Issue
- How to Solve IBM MQ Connect Error Code 2035
- How to Download File via Response (Example Code)
- JAVA Code Start Exception Error View Does Not Exsit [Solved]
- [Solved] spring source code compile error: target package does not exist
- [Solved] Gurobi–Error code: 10005. Unable to retrieve attribute solved ‘Pi‘
- Flutter: How to Fix Android module can’t recognize flutter SDK source code
- [Solved] Feign Call Port Error: HttpMessageNotReadableException:JSON parse error:Illegal character ((CTRL-CHAR, code 31)