分享程序网
首页
  • java
微服务
微前端
环境搭建
数据库
设计模式
算法
软件
解决问题
链接
首页
  • java
微服务
微前端
环境搭建
数据库
设计模式
算法
软件
解决问题
链接
  • 微服务

    • 介绍
  • 微服务搭建

    • 初步搭建
  • 服务发现

    • Eureka
    • nacos
  • 网关

    • zuul
    • 网关配置
    • 过滤器
    • 动态加载
  • 认证(Oauth)

    • 初始化项目
    • Oauth2配置
    • 对外接口
  • 通用服务

    • 通用功能父模块
    • redis
  • 任务调度

    • 任务调度服务
    • xxl-job示例
  • 业务服务

    • 业务设计

xxl-job示例

引入依赖

<dependency>
    <groupId>com.xuxueli</groupId>
    <artifactId>xxl-job-core</artifactId>
    <version>2.3.0</version>
</dependency>

配置

application.yml中添加

xxl:
  job:
### 调度中心部署跟地址 [选填]:如调度中心集群部署存在多个地址则用逗号分隔。执行器将会使用该地址进行"执行器心跳注册"和"任务结果回调";为空则关闭自动注册;
    admin-addresses: http://10.176.125.92:8088/xxl-job
### 执行器通讯TOKEN [选填]:非空时启用;
    access-token: 
    executor:
### 执行器AppName [选填]:执行器心跳注册分组依据;为空则关闭自动注册
      appname: xxl-job-executor-sample1
### 执行器注册 [选填]:优先使用该配置作为注册地址,为空时使用内嵌服务 ”IP:PORT“ 作为注册地址。从而更灵活的支持容器类型执行器动态IP和动态映射端口问题。
      address: 
### 执行器IP [选填]:默认为空表示自动获取IP,多网卡时可手动设置指定IP,该IP不会绑定Host仅作为通讯实用;地址信息用于 "执行器注册" 和 "调度中心请求并触发任务";
      ip: 
### 执行器端口号 [选填]:小于等于0则自动获取;默认端口为9999,单机部署多个执行器时,注意要配置不同执行器端口;
      port: 9999
### 执行器运行日志文件存储磁盘路径 [选填] :需要对该路径拥有读写权限;为空则使用默认路径;
      log-path: /data/applogs/xxl-job/jobhandler
### 执行器日志文件保存天数 [选填] : 过期日志自动清理, 限制值大于等于3时生效; 否则, 如-1, 关闭自动清理功能;
      log-retention-days: 30

添加对应的属性类:

package com.shareprog.exam.configuration.prop;

import org.springframework.boot.context.properties.ConfigurationProperties;

import lombok.Data;

@ConfigurationProperties(prefix = "xxl.job")
@Data
public class XxlJobProperties {
	
	public static final String PREFIX = "xxl.job";
	
	/** 
	 * 调度中心部署根地址 [选填]:如调度中心集群部署存在多个地址则用逗号分隔。
	 * 执行器将会使用该地址进行"执行器心跳注册"和"任务结果回调";
	 * 为空则关闭自动注册;
	 */
	private String adminAddresses;
	
	/**
	 * 执行器通讯TOKEN [选填]:非空时启用;
	 */
	private String accessToken;
	
	private Executor executor;
	
	@Data
	public static class Executor {
		/**
		 * 执行器AppName [选填]:执行器心跳注册分组依据;为空则关闭自动注册
		 */
		private String appname;
		/**
		 * 执行器注册 [选填]:优先使用该配置作为注册地址,为空时使用内嵌服务 “IP:PORT” 作为注册地址。
		 * 从而更灵活的支持容器类型执行器动态IP和动态映射端口问题。
		 */
		private String address;
		/**
		 * 执行器IP [选填]:默认为空表示自动获取IP,多网卡时可手动设置指定IP,该IP不会绑定Host仅作为通讯实用;
		 * 地址信息用于 "执行器注册" 和 "调度中心请求并触发任务";
		 */
		private String ip;
		/**
		 * 执行器端口号 [选填]:小于等于0则自动获取;
		 * 默认端口为9999,单机部署多个执行器时,注意要配置不同执行器端口;
		 */
		private int port;
		/**
		 * 执行器运行日志文件存储磁盘路径 [选填]:
		 * 需要对该路径拥有读写权限;为空则使用默认路径;
		 */
		private String logPath;
		/**
		 * 执行器日志文件保存天数 [选填] : 过期日志自动清理, 限制值大于等于3时生效; 
		 * 否则, 如-1, 关闭自动清理功能;
		 */
		private int logRetentionDays;
	}

}

进行注入bean

package com.shareprog.exam.configuration;

import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.shareprog.exam.configuration.prop.XxlJobProperties;
import com.xxl.job.core.executor.impl.XxlJobSpringExecutor;

import lombok.extern.slf4j.Slf4j;

@Configuration
@Slf4j
@EnableConfigurationProperties({XxlJobProperties.class})
public class Jobconfiguration {

	@Bean
	public XxlJobSpringExecutor xxlJobSpringExecutor(XxlJobProperties xxlJobProperties) {
		log.info(">>>>>>>>>>> xxl-job config init.");
		XxlJobSpringExecutor xxlJobSpringExecutor = new XxlJobSpringExecutor();
		xxlJobSpringExecutor.setAdminAddresses(xxlJobProperties.getAdminAddresses());
		xxlJobSpringExecutor.setAccessToken(xxlJobProperties.getAccessToken());
		xxlJobSpringExecutor.setAppname(xxlJobProperties.getExecutor().getAppname());
		xxlJobSpringExecutor.setIp(xxlJobProperties.getExecutor().getIp());
		xxlJobSpringExecutor.setPort(xxlJobProperties.getExecutor().getPort());
		xxlJobSpringExecutor.setLogPath(xxlJobProperties.getExecutor().getLogPath());
		xxlJobSpringExecutor.setLogRetentionDays(xxlJobProperties.getExecutor().getLogRetentionDays());
		return xxlJobSpringExecutor;
	}
}

可以尝试写一个demo

package com.shareprog.exam.service.handler.job;

import org.springframework.stereotype.Component;

import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;

import lombok.extern.slf4j.Slf4j;

@Component
@Slf4j
public class JobHandler {

	/**
	 * 1、简单任务示例(Bean模式)
	 */
	@XxlJob("demoJobHandler")
	public void demoJobHandler() throws Exception {
		log.info("测试定时任务。");
		XxlJobHelper.log("测试定时任务。");
	}
}

Last Updated:
Contributors: clcheng
Prev
任务调度服务