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:
- SpringCloud Use openFeign Multipartfile to Upload Files Error: Current request is not a multipart request
- JAVA: Controller exception handling
- [Solved] Consider defining a bean of type ‘org.springframework.data.redis.core.RedisTemplate‘ in your configu
- Springboot controls the startup of rabbitmq through configuration files
- [How to Fix]Spring 3.0 could not find acceptable representation
- OpenFeignClient Use Object to Receive text/plain Type Return Error
- Spring boot uses thread pool to realize asynchronous processing without return and asynchronous processing with return
- How to Solve the Primary Key of mybatisPlus Inserted Data is too Large Issue
- Spring interdependence error: beancurrentyincreationexception unsatisfieddependencyexception
- Initializingbean Interface & Applicationcontextaware Interface in Springboot
- The thread implementation of timer in Java
- Asynchronous processing of HTTP request by Java_ Method 2: through deferredresult
- Spring Project Error: Error creating bean with name [How to Solve]
- Implementation of retrial mechanism in Java
- Error starting ApplicationContext. To display the conditions report re-run your application with ‘de
- Springboot startup error: Field elasticsearchRestTemplate in cn.lili.modules.material.serviceImpl.QrMaterialServiceImpl required a bean of type
- Springboot startup error: err config is disabled command (Redis Disables Config command)
- RSA Decryption Error: java.security.InvalidKeyException: IOException : algid parse error, not a sequence
- [Solved] ssm Error: Error creating bean with name ‘employeeController’: Injection of autowired dependencies failed
- [Solved] Springboot2.x ElasticSearch Error: availableProcessors is already set to [4], rejecting [4]