上回说到每个线程组会创建自己的com.taobao.pamirs.schedule.taskmanager.TBScheduleManager
实例来管理线程组,一个JVM中该实例的个数与结合调度机数目分配給JVM的数目一致。TBScheduleManager实例中会计算调度任务的启动时机(与控制台界面设置保持一致)。实际开发一个调度任务按框架要求需要实现com.taobao.pamirs.schedule.IScheduleTaskDealMulti
或com.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;
}