03 xxljob-springboot

1环境说明

  1. springBoot的版本
<dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-dependencies</artifactId>
     <version>2.3.2.RELEASE</version>
     <scope>import</scope>
     <type>pom</type>
 </dependency>
  1. xxl-job的版本
<dependency>
    <groupId>com.xuxueli</groupId>
    <artifactId>xxl-job-core</artifactId>
    <version>2.2.0</version>
</dependency>

2 示例程序

2.1 Properties类

package study.wyy.job.xxl.properties;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.NestedConfigurationProperty;

/**
 * @author wyaoyao
 * @description
 * @date 2021/1/6 9:43
 */
@Data
@ConfigurationProperties(prefix = "xxl.job")
public class XxlJobProperties {
    @NestedConfigurationProperty
    private XxlJobAdminProperties admin;
    @NestedConfigurationProperty
    private XxlJobExecutorProperties executor;

}
package study.wyy.job.xxl.properties;

import lombok.Data;

/**
 * @author wyaoyao
 * @description
 * @date 2021/1/6 9:50
 */
@Data
public class XxlJobAdminProperties {
    private String address;
}

package study.wyy.job.xxl.properties;

import lombok.Data;

/**
 * @author wyaoyao
 * @description xxl 执行器配置
 * @date 2021/1/6 9:50
 */
@Data
public class XxlJobExecutorProperties {
    private String ip;
    private Integer port;
    private String appName;
    private String logPath;
    private Integer logRetentionDays;
}

2.2 xxlJob配置类

注意这里注入的执行器:XxlJobSpringExecutor

package study.wyy.job.xxl.config;

import com.xxl.job.core.executor.XxlJobExecutor;
import com.xxl.job.core.executor.impl.XxlJobSpringExecutor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;
import study.wyy.job.xxl.properties.XxlJobProperties;

/**
 * @author wyaoyao
 * @description
 * @date 2021/1/6 10:31
 */
@Configuration
// 启用XxlJobProperties
@EnableConfigurationProperties(XxlJobProperties.class)
public class XxlJobConfig {

    private XxlJobProperties xxlJobProperties;

    @Autowired
    public XxlJobConfig(XxlJobProperties xxlJobProperties) {
        this.xxlJobProperties = xxlJobProperties;
    }

    @Bean
    public XxlJobExecutor xxlJobExecutor(){
        XxlJobExecutor xxlJobExecutor = new XxlJobSpringExecutor();
        // 1 设置调度中心的地址
        xxlJobExecutor.setAdminAddresses(xxlJobProperties.getAdmin().getAddress());
        // 2 设置执行器ip
        if(StringUtils.hasText(xxlJobProperties.getExecutor().getIp())){
            xxlJobExecutor.setIp(xxlJobProperties.getExecutor().getIp());
        }
        // 3 设置端口
        xxlJobExecutor.setPort(xxlJobProperties.getExecutor().getPort());
        // 设置appname
        xxlJobExecutor.setAppname(xxlJobProperties.getExecutor().getAppName());
        // 4 设置日志
        xxlJobExecutor.setLogPath(xxlJobProperties.getExecutor().getLogPath());
        xxlJobExecutor.setLogRetentionDays(xxlJobProperties.getExecutor().getLogRetentionDays());
        return xxlJobExecutor;
    }
}

2.3 启动类和配置文件

  1. 启动类
package study.wyy.job.xxl;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * @author wyaoyao
 * @description
 * @date 2021/1/6 9:28
 */
@SpringBootApplication
public class XxlJobApplication {
    public static void main(String[] args) {
        SpringApplication.run(XxlJobApplication.class, args);
    }
}
server:
  port: 8081
spring:
  application:
    name: xxl-job-boot
xxl:
  job:
    admin:
      address: http://localhost:1111/xxl-job-admin
    executor:
      app-name: ${spring.application.name}
      log-retention-days: 4
      port: 9999
      log-path: D:\log\xxljob\executor


2.4 开发一个job

  1. 保证注入到spring容器中
  2. 使用@XxlJob注解标注任务逻辑方法
package study.wyy.job.xxl.jobhandler;

import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import com.xxl.job.core.log.XxlJobLogger;
import org.springframework.stereotype.Component;


/**
 * @author wyaoyao
 * @description
 * @date 2021/1/6 10:54
 */
@Component
public class DemoSpringJob {

    @XxlJob("springDemo1")
    public ReturnT<String> execute1(String param) throws Exception {
        // 执行日志:需要通过 "XxlJobLogger.log" 打印执行日志
        XxlJobLogger.log("springDemo1 job start.... param is: "+param);
        XxlJobLogger.log("springDemo1 job end.... param is: "+param);
        return ReturnT.SUCCESS;
    }
	@XxlJob("springDemo2")
    public ReturnT<String> execute2(String param) throws Exception {
        // 执行日志:需要通过 "XxlJobLogger.log" 打印执行日志
        XxlJobLogger.log("springDemo2 job start.... param is: "+param);
        XxlJobLogger.log("springDemo2 job end.... param is: "+param);
        return ReturnT.SUCCESS;
    }

}

这里使用的就是所谓的Bean模式的方法形式,使用这种方法必须依赖spring环境

2.5 新建执行器和任务

  1. 新建执行器
    在这里插入图片描述

  2. 新建任务
    将我们定义的两个人在调度中心中创建,选择刚刚创建的执行器
    在这里插入图片描述
    在这里插入图片描述

2.6 测试

现在就可以测试了,在调度中心执行这两个任务即可。

3 简单封装一个springBoot的starter

3.1 依赖

这里我没有声明版本,因为我前面在复工程统一进行了版本管理

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>
    <!--导入配置文件处理器,配置文件进行绑定就会有提示-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-configuration-processor</artifactId>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>com.xuxueli</groupId>
        <artifactId>xxl-job-core</artifactId>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
    </dependency>
</dependencies>

3.2 封装xxlJob的配置参数

刚刚前面的入门程序已经有了,可以直接复制过来使用,我这里只增加了一项配置: 是否启用,默认是true启用

package study.wyy.boot.xxljob.starter.properties;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.NestedConfigurationProperty;

/**
 * @author wyaoyao
 * @description
 * @date 2021/1/6 9:43
 */
@Data
@ConfigurationProperties(prefix = "xxl.job")
public class XxlJobProperties {
    private Boolean enabled = Boolean.TRUE;
    @NestedConfigurationProperty
    private XxlJobAdminProperties admin;
    @NestedConfigurationProperty
    private XxlJobExecutorProperties executor;

}

3.3 自动配置类

package study.wyy.boot.xxljob.starter.config;

import com.xxl.job.core.executor.XxlJobExecutor;
import com.xxl.job.core.executor.impl.XxlJobSpringExecutor;
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.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;
import study.wyy.boot.xxljob.starter.properties.XxlJobProperties;

/**
 * @author wyaoyao
 * @description
 * @date 2021/1/6 10:31
 */
@Configuration
// 启用XxlJobProperties
@EnableConfigurationProperties(XxlJobProperties.class)
@ConditionalOnProperty(prefix = "xxl.job", value = "enabled", matchIfMissing = true)
public class XxlJobAutoConfig {

    private XxlJobProperties xxlJobProperties;

    @Autowired
    public XxlJobAutoConfig(XxlJobProperties xxlJobProperties) {
        this.xxlJobProperties = xxlJobProperties;
    }

    @Bean
    public XxlJobExecutor xxlJobExecutor(){
        XxlJobExecutor xxlJobExecutor = new XxlJobSpringExecutor();
        // 1 设置调度中心的地址
        xxlJobExecutor.setAdminAddresses(xxlJobProperties.getAdmin().getAddress());
        // 2 设置执行器ip
        if(StringUtils.hasText(xxlJobProperties.getExecutor().getIp())){
            xxlJobExecutor.setIp(xxlJobProperties.getExecutor().getIp());
        }
        // 3 设置端口
        xxlJobExecutor.setPort(xxlJobProperties.getExecutor().getPort());
        // 设置appname
        xxlJobExecutor.setAppname(xxlJobProperties.getExecutor().getAppName());
        // 4 设置日志
        xxlJobExecutor.setLogPath(xxlJobProperties.getExecutor().getLogPath());
        xxlJobExecutor.setLogRetentionDays(xxlJobProperties.getExecutor().getLogRetentionDays());
        return xxlJobExecutor;
    }
}

创建spring.factories文件:放在类路径下META-INF目录中,maven项目就在resouce目录下创建META-INF目录:

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
  study.wyy.boot.xxljob.starter.config.XxlJobAutoConfig

3.4 如何使用

  1. 项目中引入starter的依赖
  2. 配置文件中配置如下内容即可
server:
  port: 8081
spring:
  application:
    name: xxl-job-boot
xxl:
  job:
    admin:
      address: http://localhost:1111/xxl-job-admin
    executor:
      app-name: ${spring.application.name}
      log-retention-days: 4
      port: 9998
      log-path: D:\log\xxljob\executor
  1. 提供job即可,如:必须保证被加入到spring容器中
package study.wyy.xxljob.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;

/**
 * @author wyaoyao
 * @description
 * @date 2021/1/6 15:21
 */
@Slf4j
@Component
public class MyJob {

    @XxlJob("myJob")
    public ReturnT<String> execute(String param){
        log.info("myJob is start....");
        return ReturnT.SUCCESS;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值