文章目录
- springboot集成xxl-job详解
- 1、springboot集成xxl-job:
- (1)pom文件里引入xxl-job依赖
- (2)application.properties配置文件:
- (3)在你的项目里新建文件结构如下:
- XxlJobConfig.java文件内容如下:
- DevUserHandler.java文件内容如下:
- (4)登录xxl-job-admin新建执行器和新建任务:
- (5)打印结果:
springboot集成xxl-job详解
1、springboot集成xxl-job:
(1)pom文件里引入xxl-job依赖
<!-- xxl-job-core -->
<dependency><groupId>com.xuxueli</groupId><artifactId>xxl-job-core</artifactId><version>2.4.0</version>
</dependency>
(2)application.properties配置文件:
# xxl-job
xxl.job.admin.addresses=http://127.0.0.1:8080/xxl-job-admin### xxl-job, access token
xxl.job.accessToken=default_token### xxl-job executor appname
xxl.job.executor.appname=xj-store
### xxl-job executor registry-address: default use address to registry , otherwise use ip:port if address is null
xxl.job.executor.address=
### xxl-job executor server-info
xxl.job.executor.ip=
xxl.job.executor.port=9999
### xxl-job executor log-path
xxl.job.executor.logpath=/data/applogs/xxl-job/jobhandler
### xxl-job executor log-retention-days
xxl.job.executor.logretentiondays=30
(3)在你的项目里新建文件结构如下:
|
|______BizApplication.java # 启动类
|______biz
|_______|____controller
|_______|____job
|_______|______|____config
|_______|______|________|____XxlJobConfig.java
|_______|______|____handle
|_______|______|________|____DevUserHandler.java
XxlJobConfig.java文件内容如下:
package com.unicom.mark.job.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;/*** xxl-job config** @author xuxueli 2017-04-28*/
@Configuration
public class XxlJobConfig {private Logger logger = LoggerFactory.getLogger(XxlJobConfig.class);@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;@Beanpublic XxlJobSpringExecutor xxlJobExecutor() {logger.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;}/*** 针对多网卡、容器内部署等情况,可借助 "spring-cloud-commons" 提供的 "InetUtils" 组件灵活定制注册IP;** 1、引入依赖:* <dependency>* <groupId>org.springframework.cloud</groupId>* <artifactId>spring-cloud-commons</artifactId>* <version>${version}</version>* </dependency>** 2、配置文件,或者容器启动变量* spring.cloud.inetutils.preferred-networks: 'xxx.xxx.xxx.'** 3、获取IP* String ip_ = inetUtils.findFirstNonLoopbackHostInfo().getIpAddress();*/}
DevUserHandler.java文件内容如下:
package com.unicom.mark.job.handler;import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.unicom.framework.utils.StringUtils;
import com.unicom.mark.dao.mapper.CertificationUserMapper;
import com.unicom.mark.dao.mapper.DevUserMapper;
import com.unicom.mark.dao.mapper.EntUserMapper;
import com.unicom.mark.utils.Constants;
import com.unicom.mark.vo.DevUserVo;
import com.unicom.mark.vo.EntUserVo;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;/*** XxlJob开发示例(Bean模式)** 开发步骤:* 1、在Spring Bean实例中,开发Job方法,方式格式要求为 "public ReturnT<String> execute(String param)"* 2、为Job方法添加注解 "@XxlJob(value="自定义jobhandler名称", init = "JobHandler初始化方法", destroy = "JobHandler销毁方法")",注解value值对应的是调度中心新建任务的JobHandler属性的值。* 3、执行日志:需要通过 "XxlJobLogger.log" 打印执行日志;** @author xuxueli 2019-12-11 21:52:51*/
@Component
public class DevUserHandler {private static Logger logger = LoggerFactory.getLogger(DevUserHandler.class);@Resourceprivate CertificationUserMapper certificationUserMapper;@Resourceprivate DevUserMapper devUserMapper;@Resourceprivate EntUserMapper entUserMapper;/*** 1、简单任务示例*/@XxlJob("demoJobHandler")public ReturnT<String> demoJobHandler(String param) throws Exception {XxlJobHelper.log("XXL-JOB, Hello World.");JSONObject jsonObject = JSON.parseObject(param);System.out.println(param);for (int i = 0; i < 5; i++) {XxlJobHelper.log("beat at:" + i);System.out.printf("beat at:%d \n", i);TimeUnit.SECONDS.sleep(2);}// default successreturn ReturnT.SUCCESS;}/*** 2、同步开发方账号*/@XxlJob("syncDevUser")public ReturnT<String> syncDevUser() throws Exception {List<DevUserVo> userList = certificationUserMapper.getDeveloperUser();if (userList.size()!=0) {List<String> idList = new ArrayList<>();for (DevUserVo userVo : userList) {// 处理devname字段为空的情况if(StringUtils.isBlank( userVo.getEntNameCn() )){userVo.setEntNameCn("开发者");}int i = devUserMapper.insertUser(userVo, Constants.ENT_PASSWORD, Constants.DEV_ROLE);if (i == 1) {idList.add(String.valueOf(userVo.getId()));}}String ids = String.join(",", idList);certificationUserMapper.updateDevelopPass(ids);}return ReturnT.SUCCESS;}}
(4)登录xxl-job-admin新建执行器和新建任务:
新建执行器:
新建任务: