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();
}
}
}
我尝试了Java调度器和石英调度器。使用java调度器,代码每15分钟运行一次。但它仍然不起作用。我需要每天从下午6点到凌晨12点运行这个过程。每15分钟运行一次。如果找到邮件。停止当天的调度程序,第二天下午6点重新启动进程。Syste.exet(0)停止代码,但我需要第二天重新启动它。
你说:
我需要每天从下午6点到凌晨12点运行该流程。每15分钟运行一次。如果找到邮件。停止当天的调度程序,并在第二天下午6点重新启动进程。
您可以通过将任务安排为每15分钟一次来简化问题。让该任务检查(a)是否执行了日常家务,以及(b)一天中的时间和日期。
这里的想法是让你的任务不必要地全天候运行,但时间非常短暂。检查当前时刻,并与上次执行家务的时间进行比较,只需占用少量的CPU时间和内存。因此,每天暂停一部分时间的检查真的没有意义。
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.
}
}
}
实例化。
EveningEmailCheckingTask task = new EveningEmailCheckingTask( ZoneId.of( "Asia/Kolkata" , LocalTime.of( 18 , 0 ) ) ;
创建一个ScheduledExecutorService
,并记住它。在应用程序退出之前,你需要关闭该服务,否则后台线程池可能会像僵尸一样无限期地继续♂️.
ScheduledExecutorService ses = Executors. … ;
安排你的任务。
ses.scheduleAtFixedRate( … ) ;