springboot集成及封装xxl-job

springboot集成及封装xxl-job,第1张

现有项目都是分布式部署,一个项目模块也许需要多个实例运行。定时任务在项目中一般是不可缺少的,springboot自带的@Scheduled注解单节点运行是没有问题的,但是多实例运行会导致同一个定时任务运行多次,结果不是预期的,虽然可以通过分布式锁,让多实例上只一个节点运行。在定位任务选型中,本人选型使用xxl-job,优点不多说,直接上代码。

1、pom.xml

 
            com.xuxueli
            xxl-job-core
            2.2.0
 

2、yml文件配置

grandhonor:
  base:
    tools:
      xxljob:
        switch: true
        adminAddresses: http://127.0.0.1:9980/xxl-job-admin
        appname: iot-user
        ip:
        port: 10002
        accessToken:
        logPath: xxl-job/jobhandler
        logRetentionDays: 30

说明:因为公司需要对常用中间件稍微做了封装, 可以根据自己项目的实际需要,更改配置前缀

3、XxlJobProperties.java

package net.grandhonor.framework.boot.tools.xxljob;

import lombok.Data;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;

/**
 * xxl job属性配置
 *
 * @author wangfenglei
 */
@Data
@ConditionalOnProperty(value = "grandhonor.base.tools.xxljob.switch", havingValue = "true", matchIfMissing = false)
@ConfigurationProperties(prefix = "grandhonor.base.tools.xxljob")
public class XxlJobProperties {
    private String adminAddresses;

    private String appname;

    private String ip;

    private int port;

    private String accessToken;

    private String logPath;

    private int logRetentionDays;
}

XxlJobConfig.java

package net.grandhonor.framework.boot.tools.xxljob;

import com.xxl.job.core.executor.impl.XxlJobSpringExecutor;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * xxlJob配置
 * @author wangfenglei
 */
@Configuration
@EnableConfigurationProperties(value = XxlJobProperties.class)
@ConditionalOnProperty(value = "grandhonor.base.tools.xxljob.switch", havingValue = "true", matchIfMissing = false)
@Log4j2
public class XxlJobConfig {
    @Autowired
    private XxlJobProperties xxlJobProperties;

    /**
     * 创建xxl执行器
     *
     * @return 执行器
     */
    @Bean
    public XxlJobSpringExecutor xxlJobExecutor() {
        log.info(">>>>>>>>>>> xxl-job xxlJobExecutor init<<<<<<<<<<<<<<<<<<<<<");
        XxlJobSpringExecutor xxlJobSpringExecutor = new XxlJobSpringExecutor();
        xxlJobSpringExecutor.setAdminAddresses(xxlJobProperties.getAdminAddresses());
        xxlJobSpringExecutor.setAppname(xxlJobProperties.getAppname());
        xxlJobSpringExecutor.setIp(xxlJobProperties.getIp());
        xxlJobSpringExecutor.setPort(xxlJobProperties.getPort());
        xxlJobSpringExecutor.setAccessToken(xxlJobProperties.getAccessToken());
        xxlJobSpringExecutor.setLogPath(xxlJobProperties.getLogPath());
        xxlJobSpringExecutor.setLogRetentionDays(xxlJobProperties.getLogRetentionDays());
        return xxlJobSpringExecutor;
    }
}

4、项目中使用 

package net.grandhonor.user.biz.modules.system.job;

import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;

import lombok.extern.slf4j.Slf4j;

import org.springframework.stereotype.Component;



/**
 * OTA定时升级处理器
 *
 * @author wangfenglei
 */
@Slf4j
@Component
public class OtaUpgradeHandler {

    /**
     * ota定时升级
     *
     * @param param 参数
     * @return 成功标志
     */
    @XxlJob("otaUpgradeHandler")
    public ReturnT otaUpgrade(String param) {
        XxlJobLogger.log("+++++++The upgrade queue task is beginning";

        /**
         * todo
         * 编写业务代码
         */
        return ReturnT.SUCCESS;
    }
}

说明:打印日志使用XxlJobLogger,可以在xxljob服务端的调度日志,查看日志。根据java的单一职责要求,建议每个handler创建一个类。

5、xxl-job服务端配置 

配置执行器配置

说明:AppName需要跟yml文件里面的配置对应起来,机器地址会在业务系统启动时自动注册上来,也可以手动录入,需要注意的是多个项目,ip+端口号必须唯一 

 任务管理配置

说明:JobHandler需要跟业务系统里面的 @XxlJob("otaUpgradeHandler")注解名称对应起来;路由策略和阻塞处理策略根据项目实际需要选择。

6、运行验证

说明:可以通过调度日志,查看运行情况,及运行日期 

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/736092.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-28
下一篇 2022-04-28

发表评论

登录后才能评论

评论列表(0条)

保存