【xxl-job】实现分布式定时任务demo

【官网地址】:http://www.xuxueli.com/xxl-job

【示例搭建】

      1.任务调度服务搭建示例请参考官网地址:http://www.xuxueli.com/xxl-job/#/?id=_21-%e5%88%9d%e5%a7%8b%e5%8c%96%e8%b0%83%e5%ba%a6%e6%95%b0%e6%8d%ae%e5%ba%93

      2.demo调用

          2.1新建了一个spring boot工程,项目加入maven依赖


		
			com.xuxueli
			xxl-job-core
			1.9.1
		

         2.2xxlJobConfig配置类

@Slf4j
@Configuration
//扫描定时任务设置包路径
@ComponentScan(basePackages = "com.uqiauto.springbootxxljob.job")
public class XxlJobConfig {

    @Value("${xxl.job.admin.address}")
    private String adminAddresses;

    @Value("${xxl.job.executor.appname}")
    private String appName;

    @Value("${xxl.job.alarmEmail}")
    private String alarmEmail;

    @Value("${xxl.job.executor.ip}")
    private String ip;

    @Value("${xxl.job.executor.port}")
    private int port;

    @Value("${xxl.job.accessToken}")
    private String accessToken;

    @Value("${xxl.job.executor.logpath}")
    private String logPath;

    @Value("${xxl.job.executor.logretentiondays}")
    private int logRetentionDays;

    @Bean(initMethod = "start", destroyMethod = "destroy")
    public XxlJobExecutor xxlJobExecutor() {
        log.info(">>>>>>>>>>> xxl-job config init.");
        XxlJobExecutor xxlJobExecutor = new XxlJobExecutor();
        xxlJobExecutor.setAdminAddresses(adminAddresses);
        xxlJobExecutor.setAppName(appName);
        xxlJobExecutor.setIp(ip);
        xxlJobExecutor.setPort(port);
        xxlJobExecutor.setAccessToken(accessToken);
        xxlJobExecutor.setLogPath(logPath);
        xxlJobExecutor.setLogRetentionDays(logRetentionDays);

        return xxlJobExecutor;
    }

    public String getAdminAddresses() {
        return adminAddresses;
    }

    public String getAppName() {
        return appName;
    }

    public String getIp() {
        return ip;
    }

    public int getPort() {
        return port;
    }

    public String getAccessToken() {
        return accessToken;
    }

    public String getLogPath() {
        return logPath;
    }

    public int getLogRetentionDays() {
        return logRetentionDays;
    }

    public String getAlarmEmail() {
        return alarmEmail;
    }
}

           2.3定时任务设置demo

/**
 * 任务Handler示例(Bean模式)
 *
 * 开发步骤:
 * 1、继承"IJobHandler":“com.xxl.job.core.handler.IJobHandler”;
 * 2、注册到Spring容器:添加“@Component”注解,被Spring容器扫描为Bean实例;
 * 3、注册到执行器工厂:添加“@JobHandler(value="自定义jobhandler名称")”注解,注解value值对应的是调度中心新建任务的JobHandler属性的值。
 * 4、执行日志:需要通过 "XxlJobLogger.log" 打印执行日志;
 *
 * @author xuxueli 2015-12-19 19:43:36
 */
@JobHandler(value="demoJobHandler")
@Component
public class DemoJobHandler extends IJobHandler {

	@Override
	public ReturnT execute(String param) throws Exception {
		XxlJobLogger.log("XXL-JOB, Hello World.");

		for (int i = 0; i < 5; i++) {
			XxlJobLogger.log("beat at:" + i);
			TimeUnit.SECONDS.sleep(2);
		}

		return SUCCESS;
	}

}

          2.4 配置文件

xxl.job:
  admin.address: http://localhost:8088/xxl-job-admin/
  accessToken:
  alarmEmail: [email protected]
  executor:
    appname: xxl-job-executor-sample
    ip:
    port: 9999
    logpath: /data/applogs/xxl-job/jobhandler
#执行器Log文件定期清理功能,指定日志保存天数,日志文件过期自动删除。限制至少保持3天,否则功能不生效;
    logretentiondays: -1

 

你可能感兴趣的:(web)