0

I am trying to set date and time from database in Quartz Schedular but is unable to do so. Please help me out.

Here is my code:

public class CroneScheduler { public CroneScheduler() throws Exception { SchedulerFactory sf = new StdSchedulerFactory(); Scheduler sche = sf.getScheduler(); sche.start(); JobDetail jDetail; jDetail = new JobDetail("Newsletter", "NJob", MyJob.class); //"0 0 12 * * ?" Fire at 12pm (noon) every day //"0/2 * * * * ?" Fire at every 2 seconds every day CronTrigger crTrigger = new CronTrigger("cronTrigger", "NJob", "0/2 * * * * ?"); sche.scheduleJob(jDetail, crTrigger); } } 
4
  • 1
    Please explain what you mean by "unable to do so". Also, how does the title relate to the code? Where are you trying to "set the date and time"? Did you mean you want to schedule something to happen at a specific date and time? Commented Jun 27, 2018 at 5:14
  • Yes, I want that whenever I insert date and time in database it should hit to the quartz schedular and at that particular date and time data can be send. Commented Jun 27, 2018 at 8:41
  • @TanishaAgarwal have you checked my answer? Commented Jun 27, 2018 at 9:47
  • Yes I have implement your answer but it's not working... Commented Jun 28, 2018 at 11:12

1 Answer 1

1

If the version of quartz is 1.7.2,then you can use below code:

public void resetJob(String expression){ ApplicationContext applicationContext = WebApplicationContextUtils.getRequiredWebApplicationContext(context); Scheduler scheduler = (Scheduler) applicationContext.getBean("testScheduler"); try { CronTriggerBean trigger = new CronTriggerBean(); trigger.setCronExpression(expression); trigger.setName("testJobTrigger"); trigger.setGroup(Scheduler.DEFAULT_GROUP); trigger.setJobName("testJobDetail"); scheduler.rescheduleJob("testJobTrigger", Scheduler.DEFAULT_GROUP, trigger); } catch (SchedulerException | ParseException e) { e.printStackTrace(); } } 

If the version of quartz is newer than 1.7.2,you can use below code:

public void resetJob(String expression){ ApplicationContext applicationContext = WebApplicationContextUtils.getRequiredWebApplicationContext(context); Scheduler scheduler = (Scheduler) applicationContext.getBean("testScheduler"); CronTriggerImpl trigger = null; try { TriggerKey triggerKeys = TriggerKey.triggerKey("testJobTrigger",Scheduler.DEFAULT_GROUP); trigger = new CronTriggerImpl(); trigger.setCronExpression(expression); trigger.setKey(triggerKeys);//keep key the same scheduler.rescheduleJob(triggerKeys,trigger); } catch (ParseException | SchedulerException e) { e.printStackTrace(); } } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.