2017/12/24

  1. 若要在 Spring 中使用 Quartz v1.x 則使用以下設定 (JobDetailBean & SimpleTriggerBean)

    <bean id="jobDetailBean" class="org.springframework.scheduling.quartz.JobDetailBean">
        <property name="jobClass" value="idv.shunyi.mvc.QuartzTest" />
    </bean>
    
    <bean id="simpleTriggerBean" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
        <property name="jobDetail" ref="jobDetailBean" />
        <property name="repeatInterval" value="5000" />
        <property name="startDelay" value="1000" />
    </bean>
    
  2. 若要在 Spring (v3.1+) 中使用 Quartz v2.x 則使用以下設定 (JobDetail**Factory**Bean & SimpleTrigger**Factory**Bean)

    <bean id="jobDetailBean" class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
        <property name="jobClass" value="idv.shunyi.mvc.QuartzTest" />
    </bean>
    
    <bean id="simpleTriggerBean" class="org.springframework.scheduling.quartz.SimpleTriggerFactoryBean">
        <property name="jobDetail" ref="jobDetailBean" />
        <property name="repeatInterval" value="5000" />
        <property name="startDelay" value="1000" />
    </bean>
    
  3. 若設定錯誤可能會看到以下 Exception (quartz-2.0.0.jar,卻使用 項目 1. 的設定)

    Caused by: java.lang.IncompatibleClassChangeError: class org.springframework.scheduling.quartz.JobDetailBean has interface org.quartz.JobDetail as super class
        at java.lang.ClassLoader.defineClass1(Native Method)
        at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
        at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
        at org.apache.catalina.loader.WebappClassLoaderBase.findClassInternal(WebappClassLoaderBase.java:2474)
        at org.apache.catalina.loader.WebappClassLoaderBase.findClass(WebappClassLoaderBase.java:855)
        at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1304)
        at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1163)
        at org.springframework.util.ClassUtils.forName(ClassUtils.java:258)
        at org.springframework.beans.factory.support.AbstractBeanDefinition.resolveBeanClass(AbstractBeanDefinition.java:417)
        at org.springframework.beans.factory.support.AbstractBeanFactory.doResolveBeanClass(AbstractBeanFactory.java:1283)
        at org.springframework.beans.factory.support.AbstractBeanFactory.resolveBeanClass(AbstractBeanFactory.java:1254)
        ... 18 more
    
  4. 參考資料
    Spring should support Quartz 2.0 CronTrigger interface
    IncompatibleClassChangeError : JobDetailBean has interface org.quartz.JobDetail as super class
    Spring 4 + Quartz Scheduler Integration Example
    JobDetailFactoryBean
    SimpleTriggerFactoryBean