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);
    }
}

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. 新建执行器
    03 xxljob-springboot

  2. 新建任务
    将我们定义的两个人在调度中心中创建,选择刚刚创建的执行器
    03 xxljob-springboot
    03 xxljob-springboot

2.6 测试

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

上一篇:drew


下一篇:分布式任务调度平台XXL-JOB本地配置可能遇到的问题和解决方案