Elastic-Job作业动态维护

#Elastic-Job是什么? 简单的说他是一个可集群,具有容错处理机制的作业调度平台,这里就不做过多阐述,自行搜索

参考资料1参考资料2参考资料3

#Elastic-Job作业动态配置 1、通过查看源码_JobScheduleController_,其实dangdang团队已经考虑到动态配置作业的业务场景,只不过在文档中并没有明确指出(也许是我没有看到相关文档),_JobScheduleController_类里面就封装了作业的控制方法,比如:_shutdown_关闭作业调度,_rescheduleJob_重新调度作业,_pauseJob_暂停作业等等。。。

2、还有一个关键类_JobRegistry_,他是一个单例类,在这个类里面有一个私有实例变量_schedulerMap_,它里面存储着作业名称跟作业控制器的对应关系,这样我们就能通过作业名称从变量中获取到作业控制器,通过作业调度控制器中的方法,配置我们的作业。

#废话不多说,上我们的代码 作者拿springboot做的案例,其他案例类型原理差不多,简单案例代码如下:

package com.dangdang.ddframe.job.example.controller;

import com.dangdang.ddframe.job.api.simple.SimpleJob;
import com.dangdang.ddframe.job.config.JobCoreConfiguration;
import com.dangdang.ddframe.job.config.simple.SimpleJobConfiguration;
import com.dangdang.ddframe.job.event.JobEventConfiguration;
import com.dangdang.ddframe.job.example.job.simple.JavaSimpleJob;
import com.dangdang.ddframe.job.lite.config.LiteJobConfiguration;
import com.dangdang.ddframe.job.lite.internal.schedule.JobRegistry;
import com.dangdang.ddframe.job.lite.spring.api.SpringJobScheduler;
import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperRegistryCenter;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * usedfor:作业动态配置
 * Created by javahao on 2017/7/24.
 * auth:JavaHao
 */
@RestController
@RequestMapping("/scheduler")
public class SchedulerController {

    @Resource
    private ZookeeperRegistryCenter regCenter;

    @Resource
    private JobEventConfiguration jobEventConfiguration;


    private void add(final SimpleJob simpleJob, final String cron,  final int shardingTotalCount,
                                           final String shardingItemParameters) {
        new SpringJobScheduler(simpleJob, regCenter,
                getLiteJobConfiguration(simpleJob.getClass(), cron, shardingTotalCount, shardingItemParameters),
                jobEventConfiguration).init();
    }

    private LiteJobConfiguration getLiteJobConfiguration(final Class jobClass, final String cron, final int shardingTotalCount, final String shardingItemParameters) {
        return LiteJobConfiguration.newBuilder(new SimpleJobConfiguration(JobCoreConfiguration.newBuilder(
                jobClass.getName(), cron, shardingTotalCount).shardingItemParameters(shardingItemParameters).build(), jobClass.getCanonicalName())).overwrite(true).build();
    }

    /**
     * 增加一个作业调度
     * @return 成功标识
     */
    @RequestMapping("/add")
    public boolean add(){
        add(new JavaSimpleJob(),"0/5 * * * * ?",3,"0=Beijing,1=Shanghai,2=Guangzhou");
        return true;
    }
    /**
     * 修改一个作业调度
     * @return 成功标识
     */
    @RequestMapping("/update")
    public boolean update(){
        JobRegistry.getInstance().getJobScheduleController(JavaSimpleJob.class.getName()).rescheduleJob("0/20 * * * * ?");
        return true;
    }
    /**
     * 删除一个作业调度
     * @return 成功标识
     */
    @RequestMapping("/delete")
    public boolean delete(){
        JobRegistry.getInstance().getJobScheduleController(JavaSimpleJob.class.getName()).shutdown();
        return true;
    }
}

这样我们就可以根据自己的业务场景,设计任务调度表结构,在服务启动前加载预制的调度作业,在服务启动过程中,用户可根据自己的需要增加、修改作业,维护作业调度时间周期等等。哈哈,是不是扩展还是比较灵活的?

转载于:https://my.oschina.net/u/2287170/blog/1486471

你可能感兴趣的:(大数据,java)