Announcement Announcement Module
Collapse
No announcement yet.
How do I handle Quartz exceptions? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • How do I handle Quartz exceptions?

    I am scheduling a job using the MethodInvokingJobDetailFactoryBean. In order to run the job, I just instantiate the ApplicationContext object and the container runs the scheduled job.

    Code:
    public static void main(String[] args) {
    	try {
    		System.out.println("Before Scheduler starts....");
    		ApplicationContext ctx = new ClassPathXmlApplicationContext("bean.xml");
    		System.out.println(ctx.toString());
    		System.out.println("After Scheduler starts....");
    	} catch (Exception e) {
    		System.err.println(e.getMessage());
    		System.exit(-1);
    	}
    }
    So far, so good. My concern is how to trap exceptions thrown by the job that I am scheduling. It is a method call on a simple POJO service and when there's an exception, it never makes it to the exception block inside the main method. How do I handle exceptions thrown by the Quartz scheduler?

    Here's my service:

    Code:
    public class MessageService {
    	
    	public void getMessage() {
    		Object errorObject;
    		//code to generate NPE
    		System.out.println(errorObject.toString());
    		System.out.println("Hello, world!");
    	}
    }
    Here's my bean.xml file:
    Code:
    <beans>
    	<bean id="messageService" class="test.MessageService" />
    	<bean id="messageJob"
    		class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
    		<property name="targetObject" ref="messageService" />
    		<property name="targetMethod" value="getMessage" />
    	</bean>
    	<bean id="messageTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
    		<property name="jobDetail" ref="messageJob" />
    		<property name="cronExpression" value="0 0-45 15 * * ?"></property>
    	</bean>
    
    	<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
    		<property name="triggers">
    			<list>
    				<ref bean="messageTrigger" />
    			</list>
    		</property>
    	</bean>
    </beans>
    Clearly, my objective here is to exit the process if an exception is thrown, but the code is never reached and the job just keeps running.

    Thank you for any insight you can provide.

    Doug

  • #2
    Most common behavior of schedule jobs wouldn't be to stop the job on exception, but yes I can see cases why you would like to do that.

    I have not tried this, but what about injecting messageTrigger into messageService? Just a thought.

    On exception you could possibly find a suitable method on the messageService that will stop the job, such as nullify chronExpression or something...

    Cheers

    G

    Comment


    • #3
      The constructor for ClassPathXmlApplicationContext will only throw an exception on initialization if the config file is missing or invalid. Anything after that is handled by the application, so you won't see anything in main().

      Also, from what I can tell, MethodInvokingJobDetailFactoryBean is intended as a quick one-liner method to fire off simple jobs that don't return anything and won't throw exceptions. You can extend it and implement postProcessJobDetail() but that kind of defeats the whole one-liner thing.

      Comment


      • #4
        Thanks to both of you for nudging me in the right direction!

        My solution is to go back to what I was doing before: wrapping the call to the service in a QuartzJob class. Now, I can handle the exception using the JobExecutionException:

        Code:
         ...
        protected void executeInternal(JobExecutionContext ctx) throws JobExecutionException {
        	try {
        		messageService.getMessage();
        	} catch (Exception e) {
        		JobExecutionException jee = new JobExecutionException(e);
        		jee.setUnscheduleFiringTrigger(true);
        		throw jee;
        	}
        }
        ...
        When the service generates the exception, the job is shut down until I can identify and resolve the cause of the exception.

        Doug

        Comment

        Working...
        X