TBSchedule源码学习笔记-任务处理

本文详细探讨了TBSchedule框架中任务处理的实现,包括线程的启动、任务的分片策略以及数据同步机制。重点分析了TBScheduleProcessorSleep类在SLEEP模式下的工作原理,解释了如何通过线程安全的并发容器CopyOnWriteArrayList实现数据共享,并确保在多线程环境下的数据一致性。同时,文章提到了任务项(TaskItem)的概念,它是任务分片的基础,确保任务在集群中均匀分布。
摘要由CSDN通过智能技术生成

上回说到每个线程组会创建自己的com.taobao.pamirs.schedule.taskmanager.TBScheduleManager实例来管理线程组,一个JVM中该实例的个数与结合调度机数目分配給JVM的数目一致。TBScheduleManager实例中会计算调度任务的启动时机(与控制台界面设置保持一致)。实际开发一个调度任务按框架要求需要实现com.taobao.pamirs.schedule.IScheduleTaskDealMulticom.taobao.pamirs.schedule.IScheduleTaskDealSingle接口。接着上边的启动过程来。
之前有说道:

这里使用的“开启服务”和“暂停服务”,分别使用了com.taobao.pamirs.schedule.taskmanager.TBScheduleManager类的public void resume(String message) throws Exception方法以及public void pause(String message) throws Exception方法

那么就接着打开看看public void resume(String message) throws Exception 方法,毕竟他是控制了一个线程组的启动。每当到达调度时间(开始执行时间)都会调用该方法。


/**
     * 处在了可执行的时间区间,恢复运行
     * @throws Exception 
     */
    public void resume(String message) throws Exception{
        if (this.isPauseSchedule == true) {
            if(log.isDebugEnabled()){
                log.debug("恢复调度:" + this.currenScheduleServer.getUuid());
            }
            this.isPauseSchedule = false;
            this.pauseMessage = message;
            if (this.taskDealBean != null) {
                if (this.taskTypeInfo.getProcessorType() != null &&
                    this.taskTypeInfo.getProcessorType().equalsIgnoreCase("NOTSLEEP")==true){
                    this.taskTypeInfo.setProcessorType("NOTSLEEP");
                     = new TBScheduleProcessorNotSleep(this,
                            taskDealBean,this.statisticsInfo);
                }else{
                    this.processor = new TBScheduleProcessorSleep(this,
                            taskDealBean,this.statisticsInfo);
                    this.taskTypeInfo.setProcessorType("SLEEP");
                }
            }
            rewriteScheduleInfo();
        }
    }   

大佬们说常用SLEEP模式,所以这里主要看对SLEEP 模式的处理,可见这里判断任务设置如果为sleep模式,则创建一个com.taobao.pamirs.schedule.taskmanager.TBScheduleProcessorSleep<T>实例。这里创建这个实例赋值给了TBScheduleManager的一个全局变量,这个processor被拿去做了些什么,难道是利用他启动了一些线程?毕竟我这设置了好多的线程项,应该是在这里selectTask并对数据分片的吧?所以先去看processor被拿去干什么用了。找了一圈发现以下几个操作

/**
     * 当服务器停止的时候,调用此方法清除所有未处理任务,清除服务器的注册信息。
     * 也可能是控制中心发起的终止指令。
     * 需要注意的是,这个方法必须在当前任务处理完毕后才能执行
     * @throws Exception 
     */
    public void stop(String strategyName) throws Exception{
        //.........//
            this.processor.stopSchedule();
        //.........//
    }
/**
     * 清除内存中所有的已经取得的数据和任务队列,在心态更新失败,或者发现注册中心的调度信息被删除
     */
    public void clearMemoInfo(){
        //.........//
                this.processor.clearAllHasFetchData();
        //.........//
    }
/**
     * 超过运行的运行时间,暂时停止调度
     * @throws Exception 
     */
    public void pause(String message) throws Exception{
        //.........//
                this.processor.stopSchedule();
        //.........//
    }

好吧好像都是一些要求释放资源的操作,并没有开启所以到这里有一种不详的预感“是不是又在构造函数里开线程了?!”,带着疑问打开TBScheduleProcessorSleep类的代码。嗯,看到了关键字 startThread 看来在这个类里启动线程无疑了。

/**
     * 创建一个调度处理器 
     * @param aManager
     * @param aTaskDealBean
     * @param aStatisticsInfo
     * @throws Exception
     */
    public TBScheduleProcessorSleep(TBScheduleManager aManager,
            IScheduleTaskDeal<T> aTaskDealBean, StatisticsInfo aStatisticsInfo) throws Exception {
        //线程组管理器
        this.scheduleManager = aManager;
        this.statisticsInfo = aStatisticsInfo;
        //任务配置信息
        this.taskTypeInfo = this.scheduleManager.getTaskTypeInfo();
        //通过控制台设置的bean
        this.taskDealBean = aTaskDealBean;
         //加载调度任务(实现IScheduleTaskDealMulti或IScheduleTaskDealSingle的区别)
        if (this.taskDealBean instanceof IScheduleTaskDealSingle<?>) {
            if (taskTypeInfo.getExecuteNumber() > 1) {
                taskTypeInfo.setExecuteNumber(1);
            }
            isMutilTask = false;
        } 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值