`
handonghandong
  • 浏览: 87746 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Quartz遇到的一个问题总结

阅读更多
由于客户需要尽快的看到项目原型,自己在两天内使用Quartz+Struts2,使用XML文件做持久化,搭建了一个Demo的项目工程。
功能的主要是希望能够通过界面化对Job进行CRUD操作之外,以及控制Job的启动、停止以及Job的删除操作,至于CRUD操作,主要就是通过对XML文件使用Dom4j进行解析,在有就是拼接等操作。对于Job的控制操作,需要了解下Quartz,把自己看过研究些的资料说下,在把遇到的问题说下。
JobDetail是用来包装Job实例的具体细节的。Job以class的方式传入JobDetail,quartz会自动实例化Job类的。同时需要规定job的name和group,在jobdetail的validate()下,一个job是必须有name和group属性,并且二者组合标示唯一的job。JobDetail中的一些重要数据存放在JobDataMap中,这个map可以用来在控制程序和job内传递需要传递的参数。
    JobDetail还有三个重要的boolean值的属性,分别是durability、volatility、shouldRecover。这三个属性默认是false,主要在数据库操作的时候起作用。
    Trigger时间调度
    相对于job,trigger简单一些。所谓的时间调度,就是设置job执行的时间表。Trigger主要分为SimpleTrigger和CronTrigger两大类。前者主要是以1/1000
为单位的简单时间,可是设置startTime、endTime、repeatCount、repeatInterval等主要参数。CronTrigger的详细用法参看quartz文档的的CronTriggers Tutorial。
    scheduler.schedulerJob(jobDetail, trigger);将JobDetail和对应的Trigger部署到scheduler中,然后根据trigger的定义,自动执行指定job。
    Calendar这个类用来标识出特殊的时间,scheduler根据trigger执行job的时候,如果遇到这些标识出的特殊时间则不执行job。Calendar具体的接口有BaseCalendar、AnnualCalendar、HolidayCalendar、MonthlyCalendar、WeeklyCalendar等等。calendar的具体用法参看quartz的文档。
    Misfire Instruction....设定当trigger错过了触发时刻的时候需要采取的处理策略
    TriggerUtils相当于一个trigger factory,方便我们取得特殊trigger的实例,而不用自己去构造。
关于Quartz的多线程方面可以参考文章http://liuwei-git-com-cn.iteye.com/blog/332787
注意一点的是Quartz中的实例例如HelloWorld实例:
1. HelloJob 接口
public class HelloJob implements Job{

private static Log logger = LogFactory.getLog(HelloJob.class);
@Override
public void execute(JobExecutionContext arg0) throws JobExecutionException {
// TODO Auto-generated method stub
logger.info("Hello World ");
System.out.println("Hello World");
}
2 HelloScheduler类
public class HelloScheduler {

public void run() throws Exception{
Log logger=LogFactory.getLog(HelloScheduler.class);

logger.info("Hello Schduler init***");
System.out.println("Hello Schduler init***");
        SchedulerFactory sf = new StdSchedulerFactory();
        Scheduler sched = sf.getScheduler();
        if(sched.isShutdown()){
        System.out.println("调度已经停止**");
        sched.start();
        }
        logger.info("Hello Schduler init end ***");
        System.out.println("Hello Schduler init end ***");
     // computer a time that is on the next round minute
        Date runTime = TriggerUtils.getEvenMinuteDate(new Date());
     // define the job and tie it to our HelloJob class
        JobDetail helloJob = new JobDetail("helloJob", "helloGroup", HelloJob.class);
        // Trigger the job to run on the next round minute
        SimpleTrigger trigger =
            new SimpleTrigger("helloTrigger", "helloGroup", runTime);
       
        sched.scheduleJob(helloJob, trigger); //对应触发器触发的Job
        System.out.println(helloJob.getFullName() + " will run at: " + runTime);
        logger.info(helloJob.getFullName() + " will run at: " + runTime); 
        // Start up the scheduler (nothing can actually run until the
        // scheduler has been started)
        sched.start();
        logger.info("------- Started Scheduler -----------------");
       
     // wait long enough so that the scheduler as an opportunity to
        // run the job!
        logger.info("------- Waiting 90 seconds... -------------");
        try {
            // wait 90 seconds to show jobs
            Thread.sleep(90L * 1000L);
            // executing...
        } catch (Exception e) {
        }

        // shut down the scheduler
        logger.info("------- Shutting Down ---------------------");
        //sched.shutdown(true);
        logger.info("------- Shutdown Complete -----------------");
}
    public static void main(String[] args) throws Exception {

        HelloScheduler helloExample = new HelloScheduler();
        helloExample.run();

    }
}
这个运行没问题,但是如果通过在Web项目中,通过使用getScheduler()方法获取Schedule对象,会抛出异常,所以需要通过工厂在调用之前创建几个Schedule即可。
DirectSchedulerFactory factory=DirectSchedulerFactory.getInstance();  
factory.createVolatileScheduler(10);
Scheduler sche=factory.getScheduler();
我在做Web项目时,按照给的实例直接获得造成了异常,这样解决就可以了
0
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics