Spring 3 + Quartz 2 error

纵饮孤独 提交于 2019-11-29 10:59:04

问题


I received the error below when I use Spring 3 with Quartz 2. Does anyone knows the reason?

Error:

Exception in thread "main" org.springframework.beans.factory.CannotLoadBeanClassException: Error loading class [org.springframework.scheduling.quartz.JobDetailBean] for bean with name 'job' defined in class path resource [beans.xml]: problem with class file or dependent class; nested exception is java.lang.IncompatibleClassChangeError: class org.springframework.scheduling.quartz.JobDetailBean has interface org.quartz.JobDetail as super class
    at org.springframework.beans.factory.support.AbstractBeanFactory.resolveBeanClass(AbstractBeanFactory.java:1253)

Spring config file:

<bean name="job" class="org.springframework.scheduling.quartz.JobDetailBean">
  <property name="jobClass" value="Example.ExampleJob"/>
  <property name="jobDataAsMap">
    <map>
      <entry key="timeout" value="5"/>
    </map>
  </property>
</bean>

<bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
  <property name="jobDetail" ref="job"/>
  <property name="startDelay" value="1000"/>
  <property name="repeatInterval" value="5000"/>
</bean>

public class ExampleJob extends QuartzJobBean {

      private int timeout;

      /**
       * Setter called after the ExampleJob is instantiated
       * with the value from the JobDetailBean (5)
       */ 
      public void setTimeout(int timeout) {
        this.timeout = timeout;
      }

    @Override
    protected void executeInternal(JobExecutionContext ctx)
            throws JobExecutionException {
        *****
    }

}

回答1:


Last I checked, Spring doesn't have support for Quartz 2. Either have a look to see if the most recent Spring builds have added said support, or try downgrading to Quartz 1.8.x.




回答2:


If you use Spring 3.1,

Replace the SimpleTriggerBean with SimpleTriggerFactoryBean

In the 3.1 release, Spring has created Factory classes for crontrigger and simpletrigger

Update:

Using Spring 3.2.2, must be useful to change also JobDetailBean => JobDetailFactoryBean and CronTriggerBean => CronTriggerFactoryBean.

Credit to Osy (vote on the comment below)




回答3:


According to the 3.1.0.RC1 Change Log, Spring 3.1 has support for Quartz 2.x.

For every {Type}TriggerBean there is now a {Type}TriggerBeanFactory which can be used to setup triggers. In your case this would be SimpleTriggerFactoryBean

Excerpt

NOTE: This FactoryBean works against both Quartz 1.x and Quartz 2.0/2.1, in contrast to the older SimpleTriggerBean class.

Sidenote

You might also need to add the org.springframework.transaction dependency, depending on which type of trigger you are using:

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-tx</artifactId>
    <version>3.1.2.RELEASE</version>
</dependency>

We needed it for migration to Quartz 2 in a configuration using CronTriggerFactoryBean triggers.




回答4:


If You are using Spring 3.x & Quartz 2.1.x…

Then do only two changes IN YOUR configuration file 1st : for Simple Trigger

Use class=”org.springframework.scheduling.quartz.SimpleTriggerFactoryBean”> instead of class=”org.springframework.scheduling.quartz.SimpleTriggerBean”>

2nd : for Cron Trigger use class=”org.springframework.scheduling.quartz.CronTriggerFactoryBean” instead of class=”org.springframework.scheduling.quartz.CronTriggerBean”



来源:https://stackoverflow.com/questions/8245218/spring-3-quartz-2-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!