-3

So i want to schedule a task on a specific time like 7:15AM and a want to execute it once, i want to work with Quartz but it makes the task execute every period of time (5 minutes for example), but i don't want this i want to schedule the task based on real time. Thank you

4 Answers4

0

Please use CronTrigger expression.eg: 0 15 07 * * *

Mr.zhu
  • 1
  • 1
    Can you explain that further? How does this "CronTrigger expression" ensure that the task is run **exactly once**? – Nico Haase Aug 15 '19 at 08:00
0

and a want to execute it once,

Some old code of mine that fires off one-off scheduled jobs

Calendar cal = Calendar.getInstance();
SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd");
cal.setTime(df.parse(job.getRunatDate()));
cal.set(Calendar.HOUR_OF_DAY, Integer.parseInt(job.getRunatHour()));
cal.set(Calendar.MINUTE, Integer.parseInt(job.getRunatMin()));

log.debug ("One-off job at " + cal.getTime());

Trigger trigger = (SimpleTrigger) TriggerBuilder.newTrigger() 
                    .withIdentity("trigger" + job.getLineNumber(), "group1")
                    .startAt(cal.getTime()) 
                     .build();

sched = new StdSchedulerFactory().getScheduler();
sched.start();
sched.scheduleJob(job, trigger);

edit

As Basic mentions, this is using very old date-time classes that was replaced by JSR310. Either use that or the Three10 Backport

Scary Wombat
  • 44,617
  • 6
  • 35
  • 64
0

Get the current moment as seen in your desired time zone.

ZoneId z = ZoneId.of( "America/Montreal" ) ;
ZonedDateTime zdtNow = ZonedDateTime.now( z ) ;

Define your target time.

LocalTime targetTime = LocalTime.of( 7 , 15 ) ;

Compare to current time.

LocalTime timeNow = zdtNow.toLocalTime() ;
ZonedDateTime targetZdt = zdtNow.with( targetTime ) ;
if( ! timeNow.isbefore( targetTime ) )
{
    targetZdt = targetZdt.plusDays( 1 ) ;  // Auto-adjusts if this time of day is not valid on that date in that zone. 
}

Calculate elapsed time until then.

Duration d = Duration.between( zdtNow , targetZdt ) ;

Use the Executors framework to schedule your Runnable task with a single-threaded ScheduledExcutorService after waiting the amount of time in the Duration. Search Stack Overflow as this has been covered many times.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
-1

Thank you guys for the help i found an answer here:

CronTrigger Example 1 - an expression to create a trigger that simply fires every 5 minutes

“0 0/5 * * * ?”

CronTrigger Example 2 - an expression to create a trigger that fires every 5 minutes, at 10 seconds after the minute (i.e. 10:00:10 am, 10:05:10 am, etc.).

“10 0/5 * * * ?”

CronTrigger Example 3 - an expression to create a trigger that fires at 10:30, 11:30, 12:30, and 13:30, on every Wednesday and Friday.

“0 30 10-13 ? * WED,FRI”

CronTrigger Example 4 - an expression to create a trigger that fires every half hour between the hours of 8 am and 10 am on the 5th and 20th of every month. Note that the trigger will NOT fire at 10:00 am, just at 8:00, 8:30, 9:00 and 9:30

“0 0/30 8-9 5,20 * ?”

Note that some scheduling requirements are too complicated to express with a single trigger - such as “every 5 minutes between 9:00 am and 10:00 am, and every 20 minutes between 1:00 pm and 10:00 pm”. The solution in this scenario is to simply create two triggers, and register both of them to run the same job.