Friday, January 14, 2011

classcastexception while working with quartz + spring

I was trying to integrate Quartz to Spring and while everything seemed OK I got the following exception:

java.lang.ClassCastException: org.quartz.impl.StdScheduler

I had everything in hand for the simple job I implemented to work.

A simple Job class that implements QuartzJobBean:

public class Job extends QuartzJobBean{

protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException {

The necessary spring context:

All I had to do is instantiate the bean with getBean() but I was getting "java.lang.ClassCastException: org.quartz.impl.StdScheduler" everytime. My code for instantiating the SchedulerFactoryBean was below:

SchedulerFactoryBean d = (SchedulerFactoryBean) appCon.getBean("scheduler");

I was trying to downcast to a wrong class. It seemed quite straightforward to use SchedulerFactoryBean but it was wrong. As the exception said I changed it to StdScheduler (of Quartz) and I fixed it.
Here's the correct form:

StdScheduler d = (StdScheduler) appCon.getBean("scheduler");

I hope that easy solution will be helpful for somebody out there.

No comments:

Post a Comment