简体   繁体   English

在Java中运行cron作业

[英]Running cron job in Java

This is my ScheduleFactory Class which creates and schedule the Job for me. 这是我的ScheduleFactory类,它为我创建和计划作业。

I want to understand how this Java class will be called to trigger the cron job. 我想了解如何调用此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);
    }

This is my job class where i will have th execute method 这是我的工作类别,我将拥有执行方法

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

I want to understand how can i call XScedulerFacory.jobSchedule() method. 我想了解如何调用XScedulerFacory.jobSchedule()方法。 I wanted to run this independently and trigger my job every 40 secs. 我想独立运行此程序,并每40秒触发一次工作。

Something like this : 像这样的东西:

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);
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM