Quartz Integration with Spring

I have a web application and am trying to run Quartz scheduler programmatically in spring. I have a service class where I create an instance of SchedulerFactory and then get the 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 scheduler and the scheduler are created, but my tasks are not completed. Can anyone point out what I'm missing here?

I also need only one instance of Factory and one instance of the scheduler. I tried to make static, but it did not work. Any pointers in this direction will be helpful.

thanks

+1
source share
3 answers

If you do not have a specific Quartz functionality requirement, I recommend getting rid of it and Spring with the possibility of internal planning . Starting with Spring 3, this includes support for expressions like cron, very similar to the Quartz cron trigger.

In addition to the simplicity of your application and its configuration, it is inherently more reliable than Quartz, and provides a simpler API for use in programs through the TaskScheduler interface.

+1
source

First of all, how well do you know the Quartz or cron trigger expressions? I could be wrong, but 10 * * * * ? * 10 * * * * ? * will trigger every 10 seconds every minute, but I have never seen such an expression, it may not work at all.

Are you trying to create a trigger to run every 10 seconds? In this case, use a simple trigger as follows:

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

Edit:

Well, therefore, if necessary, you need a trigger that fills the fire only once, at the end of the auction. To do this, use SimpleTrigger as follows:

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

The start date in this case does not matter if you started it at the appropriate time (end time) and only once.

And as an additional note, do not calculate such dates. I suggest you try the Joda Time library . A really simple and well-known replacement for the awkward standard Date / Calendar API.

0
source

You forgot to run 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 proved this by adding a missing instruction (and replacing the task with a dummy), this worked for me /

0
source

Source: https://habr.com/ru/post/1337319/


All Articles