Search code examples
javaquartz-schedulercronexpression

Quartz scheduler time setting - comma not accepted


I'm trying to set a Quartz job to run every minute in every hour, except in 0 and 30 minutes.

When I try to configure like this:

ScheduledSenderJob=0 2-28 * * * ?

It works perfectly for me. But when I try this:

ScheduledSenderJob=0 2-28,35-58 * * * ?

I got an Exception, when I launch Glassfish server: CronException, invalid character, . Somehow the comma character doesn't seem to work, and I couldn't work it out.

Could someone help me out, what the correct scheduling would be? Thank you.

Stacktrace:

Caused by: java.lang.RuntimeException: java.lang.RuntimeException: CronExpression '0 2-28' is invalid,.
at eu.pont.zf.szkif.sched.quartz.QuartzServicesImpl.startScheduling(QuartzServicesImpl.java:200)
at eu.pont.zf.szkif.sched.quartz.QuartzServicesImpl.init(QuartzServicesImpl.java:78)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.glassfish.ejb.security.application.EJBSecurityManager.runMethod(EJBSecurityManager.java:1052)
at org.glassfish.ejb.security.application.EJBSecurityManager.invoke(EJBSecurityManager.java:1124)
at com.sun.ejb.containers.BaseContainer.invokeBeanMethod(BaseContainer.java:5388)
at com.sun.ejb.EjbInvocation.invokeBeanMethod(EjbInvocation.java:619)
at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:800)
at com.sun.ejb.EjbInvocation.proceed(EjbInvocation.java:571)
at org.jboss.weld.ejb.SessionBeanInterceptor.aroundInvoke(SessionBeanInterceptor.java:49)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.sun.ejb.containers.interceptors.AroundInvokeInterceptor.intercept(InterceptorManager.java:861)
at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:800)
at com.sun.ejb.EjbInvocation.proceed(EjbInvocation.java:571)
at com.sun.ejb.containers.interceptors.SystemInterceptorProxy.doAround(SystemInterceptorProxy.java:162)
at com.sun.ejb.containers.interceptors.SystemInterceptorProxy.aroundInvoke(SystemInterceptorProxy.java:144)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.sun.ejb.containers.interceptors.AroundInvokeInterceptor.intercept(InterceptorManager.java:861)
at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:800)
at com.sun.ejb.containers.interceptors.InterceptorManager.intercept(InterceptorManager.java:370)
at com.sun.ejb.containers.BaseContainer.__intercept(BaseContainer.java:5360)
at com.sun.ejb.containers.BaseContainer.intercept(BaseContainer.java:5348)
at com.sun.ejb.containers.EJBObjectInvocationHandler.invoke(EJBObjectInvocationHandler.java:206)
... 48 more

Solution

  • UPDATED:

    Whatever software is providing you access to the ScheduledSenderJob property is interpreting the string and tokenizing on the commas. Below is an example app that loads your cron expression from a java properties file and runs without a problem:

    import static org.quartz.CronScheduleBuilder.cronSchedule;
    import static org.quartz.TriggerBuilder.newTrigger;
    
    import java.io.IOException;
    import java.io.InputStream;
    import java.util.Properties;
    
    import org.quartz.JobBuilder;
    import org.quartz.JobDetail;
    import org.quartz.Scheduler;
    import org.quartz.SchedulerException;
    
        import org.quartz.SchedulerFactory;
        import org.quartz.Trigger;
    
    
        /**
         * Hello world!
         *
         */
        public class App 
        {
    
            public static void main( String[] args )
            {
                 SchedulerFactory schedFact = new org.quartz.impl.StdSchedulerFactory();
    
                  Scheduler sched;
                try {
                    sched = schedFact.getScheduler();
    
                    Properties props = new Properties();
                    InputStream inputStream = sched.getClass().getClassLoader()
                            .getResourceAsStream("test.properties");
    
                    props.load(inputStream);
    
                    String cronString = props.getProperty("cron");
    
                      // define the job and tie it to our HelloJob class
                      JobDetail job = JobBuilder.newJob(HelloJob.class)
                          .withIdentity("myJob", "group1")
                          .build();
    
                    System.out.println( "Hello World!" );
                    Trigger trigger = newTrigger()
                            .withIdentity("trigger3", "group1")
                            .withSchedule(cronSchedule(cronString))
                            .forJob("myJob", "group1")
                            .build();
    
                    sched.scheduleJob(job,trigger);
                    sched.start();
                } catch (SchedulerException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
    
    
            }
    
        }
    

    HelloJob.java:

    import org.quartz.Job;
    import org.quartz.JobExecutionContext;
    import org.quartz.JobExecutionException;
    
    public class HelloJob implements Job{
    
        public void execute(JobExecutionContext arg0) throws JobExecutionException {
            System.out.println("Hello ! ");
    
        }
    
    }
    

    test.properties - I moved the timing bits to seconds to see the results faster.

    cron=2-28,35-58 * * * * ?