2017-06-02 163 views
0

這是我的ScheduleFactory類,它爲我創建和安排作業。在Java中運行cron作業

我想了解如何調用此Java類來觸發cron作業。

public class XScedulerFacory 
{ 
    public void jobSchedule() throws SchedulerException 
    { 
     SchedulerFactory schedulerFactory = new StdSchedulerFactory(); 
     Scheduler scheduler = schedulerFactory.getScheduler(); 
     JobDetail job = newJob(MyJob.class).withIdentity("job", "group1") 
              .build(); 
     Trigger trigger = newTrigger().withIdentity("trigger1", "group1") 
             .startNow() 
             .withSchedule(simpleSchedule() 
             .withIntervalInSeconds(40) 
             .repeatForever()) 
             .build(); 
     scheduler.scheduleJob(job, trigger); 
    } 

這是我的工作類在那裏我將有個execute方法

public class MyJob implements org.quartz.Job { 
    @Override 
    public void execute(JobExecutionContext jec) throws JobExecutionException { 
     System.out.println("MyJob.execute()"); 
    } 

我想知道我怎麼能叫XScedulerFacory.jobSchedule()方法。我想獨立運行並每隔40秒觸發一次工作。

回答

0

事情是這樣的:

package poop.test; 

public class MainTest 
{ 
    public static void main(String[] args) 
    { 
     XScedulerFacory factory = new XScedulerFacory(); 
     try 
     { 
      factory.jobSchedule();  
     } 
     catch(SchedulerException e) 
     { 
      System.out.println("Error " + e); 
     } 
    } 
} 
+0

這是一個主類。我想一直運行這個類,以便它輪詢db – user8068412