scheduler 的使用

1:scheduler自动调配任务的使用:

开发组件,SSH。我这里用的是SSH,当然也可用其他的方法

2:首先应该建立一个schedule来管理我们使用的任务时间等

publicclassBidBoardFinalizeScheduler{

//StaticLoggervariable

privatestaticfinalLoggerlogger=Logger

.getLogger(BidBoardFinalizeScheduler.class.getName());

publicBidBoardFinalizeScheduler()throwsBBSystemException,

BBSystemException{

scheduleFinalizeJob();

}

privatevoidscheduleFinalizeJob()throwsBBSystemException{

//fieldsdeclaration

SchedulerFactoryschedulerFactory=null;

Schedulerscheduler=null;

JobDetailjobDetail=null;

SimpleTriggersimpleTrigger=null;

try{

schedulerFactory=newStdSchedulerFactory();

scheduler=schedulerFactory.getScheduler();

jobDetail=newJobDetail(BBCommonConstants.Finalize,

Scheduler.DEFAULT_GROUP,BidboradFinalizeJob.class);

simpleTrigger=newSimpleTrigger("myFinalizetrigger",

Scheduler.DEFAULT_GROUP,5000,90000);//15minutes

try{

scheduler.scheduleJob(jobDetail,simpleTrigger);

}catch(SchedulerExceptione){

logger.debug("ExceptionoccuredininvokeJob");

throwBBUtil.getBBSystemException(e,

BBMessageConstants.ERROR_WHILE_SCHEDULING_JOB);

}

scheduler.start();

}catch(BBSystemExceptione){

logger.error("schedulercouldnotbeintialized");

throwBBUtil.getBBSystemException(BBMessageConstants.OBJECTS_NULL);

}catch(Exceptionexception){

logger.debug("ErrorwhileInitializingScheduler");

}

}

}

3:我们应该接着写一个job类文件,在这个类里我们调用DAO以及SERVICE层,就是我们要执行的功能,而且这个类我们必须要实现job类的一个接口,而且实现它的execute方法。

publicclassBidboradFinalizeJobimplementsStatefulJob{

publicvoidexecute(JobExecutionContextarg0){

//variabledeclaration

CommonServicecommonService=null;

try{

commonService=getBidBoardServiceFactory().getCommonService();

commonService.bidBoardFinalize();

}catch(BBSystemExceptione){

e.printStackTrace();

}

}

publicstaticsynchronizedBidBoardServiceFactorygetBidBoardServiceFactory()

throwsBBSystemException{

BidBoardServiceFactorybbServiceFactory=null;

if(null==bbServiceFactory){

bbServiceFactory=(BidBoardServiceFactory)SpringContextSupport

.getBean(BBCommonConstants.BIDBOARDSERVICE_FACTORY);

}

returnbbServiceFactory;

}

}

4:等配置完这些之后,我们只需要在bean.xml的配置文件中,加入我们要实例化的这个即可

如何:<beanid="bidBoardFializeScheduler"

class="com.fritolay.bidboard.service.bidsheet.BidBoardFinalizeScheduler"singleton="true">

</bean>

相关推荐