Quartz Integration with Spring

只愿长相守 提交于 2020-01-25 00:21:08

问题


I have a web application and I am trying to start Quartz scheduler programmatically in spring. I have a service class where I create an instance of SchedulerFactory and then get a scheduler.

The code is as follows.

@Service("auctionWinnerService")
public class NormalAuctionWinnerServiceImpl implements AuctionWinnerService {

    public static final String NORMAL_AUCTION = "NORMAL AUCTION";
    public static int NORMAL_AUCTION_COUNTER = 0;
    private SchedulerFactory schedulerFactory;
    private Scheduler scheduler;

    public void declareWinner(int auctionId, Map<String, Object> parameterMap) {
        System.out.println("INSIDE declareWinner of NormalAuctionWinner");
        schedulerFactory = new StdSchedulerFactory();
        try {
            scheduler = schedulerFactory.getScheduler();
            System.out.println("GOT SCHEDULER : "+scheduler);
        } catch (SchedulerException e1) {
            e1.printStackTrace();
        }

        JobDetail jd = new JobDetail();
        jd.setName(NORMAL_AUCTION+" JOB "+NORMAL_AUCTION_COUNTER);
        jd.setJobClass(NormalAuctionWinnerJob.class);

        /** CREATE CRON TRIGGER INSTANCE **/
        CronTrigger t = new CronTrigger();
        t.setName(NORMAL_AUCTION + ++NORMAL_AUCTION_COUNTER);
        t.setGroup("Normal Auction");
        Date d = new Date();
        Date d1 = new Date();
        d1.setMinutes(d.getMinutes()+5);

        t.setStartTime(d);
        t.setEndTime(d1);
        try {
            t.setCronExpression("10 * * * * ? *");
            scheduler.scheduleJob(jd, t);
        } catch (SchedulerException e) {
            e.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
}

The schedulerFactory and scheduler are instantiated but my jobs do not run. Could someone point out what am I missing here?

Also I need only one instance of Factory and one scheduler instance. I tried making the static but it didn't work. Any pointers in this direction will be helpful.

Thanks


回答1:


Unless you have a specific requirement on Quartz's proprietary functionality, I recommend getting rid of it and using Spring's internal scheduling capability. As of Spring 3, this includes support for cron-type expressions, very similar to Quartz's cron trigger.

As well as bringing simplicity to your application and its config, it's inherently more reliable than Quartz, and provides an easier API for programmatic usage, via the TaskScheduler interface.




回答2:


First of all, how well do you know Quartz or cron trigger expressions? I may be mistaken, but 10 * * * * ? * will make the trigger fire every 10th second of every minute, but I've never seen such expression, it may not fire at all.

Are you trying to create a trigger to fire every 10 seconds? In this case, use a simple trigger like this:

new SimpleTrigger((NORMAL_AUCTION + ++NORMAL_AUCTION_COUNTER),
                   "Normal Auction",
                   d,
                   d1,
                   SimpleTrigger.REPEAT_INDEFINITELY,
                   10000L);

Edit:

Ok, so if that's you requirement, you need a trigger that fill fire just once, at the end time of the auction. For that, use a SimpleTrigger like this:

new SimpleTrigger((NORMAL_AUCTION + ++NORMAL_AUCTION_COUNTER),
               "Normal Auction",
               d1,
               null,
               0,
               0L);

The start date in this case does not matter, as long as you set it to fire on the appropriate time (the end time) and just once.

And as an additional note, do not calculate dates like that. I suggest you to try Joda Time library. Really simple and well known replacement for the clumsy standard Date/Calendar API.




回答3:


You have forgotten to start the scheduler! scheduler.start();

...
try {
   t.setCronExpression("10 * * * * ? *");
scheduler.scheduleJob(jd, t);
scheduler.start();
} catch (SchedulerException e) {
      e.printStackTrace();
} catch (ParseException e) {
      e.printStackTrace();
}

I have proved this, after adding the missing statement, (and replacing the job with an dummy) it worked for me/



来源:https://stackoverflow.com/questions/4794560/quartz-integration-with-spring

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!