创建了 schedule cron job main class 作为可运行的 jar

created schedule cron job main class as runnable jar

- i have written ScheduleClass which call HelloJob class every 5 seconds.

`

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import org.quartz.CronScheduleBuilder;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;



public class ScheduleClass {

    public static void main( String[] args ) throws Exception
    {   
        JobDetail job = JobBuilder.newJob(HelloJob.class)
                .withIdentity("dummyJobName", "group1").build();

        Trigger trigger = TriggerBuilder
                .newTrigger()
                .withIdentity("dummyTriggerName", "group1")
                .withSchedule(
                        CronScheduleBuilder.cronSchedule("0/10 * * * * ?"))
                .build();

        //schedule it
        Scheduler scheduler = new StdSchedulerFactory().getScheduler();
        scheduler.start();
        scheduler.scheduleJob(job, trigger);

    }
}


public class HelloJob implements Job
 {
  public void execute(JobExecutionContext context)
  throws JobExecutionException {
    System.out.println("Hello Quartz!");    
  }
}

`

我知道这应该是一个评论,但不能在那里格式化所以我 post 它作为一个答案。

不知道 SpringToolSuite 如何工作或创建 jar。我自己完成并按预期工作:

  • 编译: 我使用与你相同的代码(在 HelloJob 中添加格式为 yyyy-MM-dd HH:mm:ss.SDate class).使用 Oracle JDK 1.6quartz 2.2

    编译并 运行
    javac -cp c3p0-0.9.1.1.jar;log4j-1.2.16.jar;quartz-2.2.1.jar;quartz-jobs-2.2.1.jar;slf4j-api-1.6.6.jar;slf4j-log4j12-1.6.6.jar;. ScheduleClass.java 
    javac -cp c3p0-0.9.1.1.jar;log4j-1.2.16.jar;quartz-2.2.1.jar;quartz-jobs-2.2.1.jar;slf4j-api-1.6.6.jar;slf4j-log4j12-1.6.6.jar;. HelloJob.java 
    
  • 创建 jar:

    jar -cf sched.jar ScheduleClass.class HelloJob.class 
    

    您可以在此处添加一个特定的 MANIFEST 文件,指示其 Main-Class 条目(我没有这样做,但它应该也能正常工作)

    它包含:

    unzip -l sched.jar 
    

    Archive: sched.jar
    META-INF/
    META-INF/MANIFEST.MF
    ScheduleClass.class
    HelloJob.class

  • 运行

    java -cp c3p0-0.9.1.1.jar;log4j-1.2.16.jar;quartz-2.2.1.jar;quartz-jobs-2.2.1.jar;slf4j-api-1.6.6.jar;slf4j-log4j12-1.6.6.jar;sched.jar ScheduleClass
    
  • 输出

    log4j:WARN No appenders could be found for logger (org.quartz.impl.StdSchedulerFactory).
    log4j:WARN Please initialize the log4j system properly.
    log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
    2015-04-16 13:26:20.17 - Hello Quartz!
    2015-04-16 13:26:30.2 - Hello Quartz!
    2015-04-16 13:26:40.3 - Hello Quartz!
    2015-04-16 13:26:50.3 - Hello Quartz!
    2015-04-16 13:27:00.4 - Hello Quartz!
    2015-04-16 13:27:10.5 - Hello Quartz!
    2015-04-16 13:27:20.21 - Hello Quartz!
    2015-04-16 13:27:30.6 - Hello Quartz!

它一直持续到按下 CTRL+C

你看你的代码是正确的。也许您的 jar 创建方式有问题。看看它,或者像我一样尝试自己编译(如果可能的话)。

抱歉没有提供更多帮助,但我没有发现任何问题。