'How to schedule a task which checks for an Email every 15 min. If mail is found, then task should end and only execute the next day using Java
public static void main(String[] args) throws Exception {
String saveDirectorySib = PropertiesHolder.getInstance().getProperty(PropertiesHolder.SIB_Txt_FOLDER_KEY);
JobDetail Job = JobBuilder.newJob(SibCronScheduler.class).build();
Trigger t1 = TriggerBuilder.newTrigger().withIdentity("CronTrigger")
.withSchedule(CronScheduleBuilder.cronSchedule(PropertiesHolder.getInstance().getProperty(PropertiesHolder.schedulerExec))).build();
Scheduler sched = StdSchedulerFactory.getDefaultScheduler();
sched.start();
sched.scheduleJob(Job, t1);
EmailFoundBoolean mailFetch = new EmailFoundBoolean();
boolean mailFound = mailFetch.emailFoundValueReturn();
System.out.println("Value is"+mailFound);
if(mailFound== true)
{ System.out.println(mailFound);
// sched.deleteJob(Job.getKey());
// sched.pauseJob(Job.getKey());
sched.standby();
System.out.println("Process completed for the day.");
}
else
{
System.out.println("Process continues");
}
public class SibCronScheduler implements Job{
@Override
public void execute(JobExecutionContext arg0) throws JobExecutionException {
// Timer time = new Timer();
// ScheduledTask st = new ScheduledTask();
// time.schedule(st, 9000,1000 * 60 * 15);
System.out.println("Checking for the mail from South Indian Bank....");
String saveDirectorySib = PropertiesHolder.getInstance().getProperty(PropertiesHolder.SIB_Txt_FOLDER_KEY);
mailRead.setSaveDirectory1(saveDirectorySib);
try {
mailRead.sibEmailReader();
} catch (Exception e) {
e.printStackTrace();
}
}
}
I tried Java scheduler and quartz scheduler. Using java scheduler the code runs every 15 min. But still it does not work. I need to run the process every day from 6 pm to 12 am. every 15 min. And if the mail is found. stop the scheduler for the day and restart the process the next day at 6 pm. Syste.exit(0) stops the code but I need to restart it the next day.
Solution 1:[1]
You said:
I need to run the process every day from 6 pm to 12 am. every 15 min. And if the mail is found. stop the scheduler for the day and restart the process the next day at 6 pm.
You could simplify your problem by scheduling a task to run every 15 minutes around the clock. Let that task check (a) whether the daily chore was performed, and (b) the time-of-day and date.
The idea here is to let you task run needlessly, but ever so briefly, around the clock. Checking the current moment, and comparing to when the chore was last performed, take a minuscule amount of CPU time and memory. So there really is no point to bothering with suspending the checks for part of each day.
public class EveningEmailCheckingTask implements Runnable
{
final private ZoneId zoneId;
final private LocalTime startTime ;
private ZonedDateTime whenEmailRetrieved;
public EveningEmailCheckingTask( final ZoneId z , final LocalTime lt )
{
this.zoneId = Objects.requireNonNull( z ) ;
this.startTime = Objects.requireNonNull( lt ) ;
this.whenEmailRetrieved = ZonedDateTime.now( this.zoneId ).minusDays( 1 ) ; // Initialize a value so our main code can skip null check.
}
@Override
public void run()
{
ZonedDateTime now = ZonedDateTime.now( this.zoneId ) ;
// See if chore has been completed successfully today.
LocalDate then = this.whenEmailRetrieved.toLocalDate() ;
LocalDate today = now.toLocalDate() ;
if( then.isBefore( today ) )
{
if( now.toLocalTime().isBefore( this.startTime ) ) { return ; }
// … Perform chore.
// … If email received, update `this.whenEmailRetrieved`.
}
else if( then.isEqual( today ) )
{
return ; // Daily chore already performed.
}
else if( then.isAfter( today ) )
{
// Houston, we have a problem. The task was performed in the future.
}
else
{
// Should never reach this point. Defensive check in case our if-tests above are flawed.
}
}
}
Instantiate.
EveningEmailCheckingTask task = new EveningEmailCheckingTask( ZoneId.of( "Asia/Kolkata" , LocalTime.of( 18 , 0 ) ) ;
Create a ScheduledExecutorService, and remember it. You’ll need to shutdown that service before your app exits, otherwise the backing thread pool can continue indefinitely like a zombie ????.
ScheduledExecutorService ses = Executors. … ;
Schedule your task.
ses.scheduleAtFixedRate( … ) ;
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
