Search code examples
javajakarta-eeschedulerrunnablecronexpression

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

`
  • in my IDE(SpringToolSuite) given File-->Export-->selecting Runnable JAR File-->Lunch Configuration (main class ) & Export destination(my local path)-->Finsh

  • i got jar file in local path.

  • from cmd c:\mypath>java -jar ScheduleClassTest.jar
  • only one time executed.
  • need to run every 5seconds.

Solution

  • I know this should be a comment, but can't format there so I post it as an answer.

    Don't know how SpringToolSuite works or create jars. I did by myself and works as expected:

    • Compilation: I use same code than you (adding a Date with format yyyy-MM-dd HH:mm:ss.S in HelloJob class). Compiled and run using Oracle JDK 1.6 with quartz 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 
      
    • Create jar:

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

      Here you can add an especific MANIFEST file indicating its Main-Class entry (I didn't do, but it should work as well)

      It contains:

      unzip -l sched.jar 
      

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

    • Running

      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
      
    • Output

      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!

    It continues until CTRL+C is pressed

    You see your code is correct. Maybe something is wrong in the way your jar is created. Take a look at it, or try to compile by yourself (if possible) as I did.

    Sorry for not being more helpful, but I see nothing wrong.