Spring整合quart初识

Posted 斷弦悠揚

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring整合quart初识相关的知识,希望对你有一定的参考价值。

Spring集成quart有两种方式,一种是实现Job接口,一种是继承QuartzJobBean

刚开始报错:持久化时未序列化异常

    <bean id="simpleJobDetail"  
        class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">  
        <property name="targetObject">  
            <ref bean="aaa" />  
        </property>  
        <property name="targetMethod">  
            <value>doSth</value>  
        </property>  
    </bean> 

java.io.NotSerializableException: Unable to serialize JobDataMap for insertion into database because the value of property ‘methodInvoker‘ is not serializable: org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean

原因请看org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean

NOTE: JobDetails created via this FactoryBean are not serializable and thus not suitable for persistent job stores. You need to implement your own Quartz Job as a thin wrapper for each case where you want a persistent job to delegate to a specific service method.

Compatible with Quartz 1.5+ as well as Quartz 2.0-2.2, as of Spring 3.2.

废话不多说直接上主要代码

application.xml

  <bean id="simpleJobDetail" class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
        <property name="jobClass" value="com.cntaiping.tpp.tppeservice.job.PrintCurrentTimeJobs" />
        <property name="name" value="executeInternal" />
        <property name="durability" value="true"></property>
    </bean>
    <bean id="jobTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
        <property name="jobDetail" >
            <ref bean="simpleJobDetail"></ref>
        </property>
        <property name="cronExpression" >
            <value>0 */1 * * * ?</value>
        </property>
    </bean>
    <!-- 启动触发器的配置开始 -->
     <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
         <property name="configLocation" value="classpath:quartz.properties"/>
         <property name="dataSource" ref="dataSource"/>
         <property name="transactionManager" ref="transactionManager"/>
         <property name="schedulerName" value="baseScheduler"/>
         <!-- 每台集群机器部署应用的时候会更新触发器-->
         <property name="overwriteExistingJobs" value="false"/>
         <property name="applicationContextSchedulerContextKey" value="appli"/>
         <property name="jobFactory">
             <bean class="com.cntaiping.tpp.tppeservice.auth.AutowiringSpringBeanJobFactory"/>
         </property>
         <property name="triggers">
             <list>
                 <ref bean="jobTrigger"/>
             </list>
         </property>
         <property name="autoStartup" value="true"/>

      </bean>
PrintCurrentTimeJobs.java(继承QuartzJobBean)
public class PrintCurrentTimeJobs extends QuartzJobBean{
    Logger log = Logger.getLogger(JobService.class);
    @Autowired
    private UserJob userJob;

     @Override
    protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException
    {
        log.info("测试定时任务开始"+new Date());
        System.err.println(new Date());
        userJob.createTmsUser();
        log.info("测试定时任务结束"+new Date());
    }
}

QuartJobFactory.java(实现Job接口)

public class QuartJobFactory implements Job {
    private Logger log = Logger.getLogger(QuartJobFactory.class);
    ScheduleJob scheduleJob ;
    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
          scheduleJob = (ScheduleJob) context.getMergedJobDataMap().get("scheduleJob");
          StringBuffer sbf = new StringBuffer();
          System.err.print(new Date());
    }

}

quartz.properties

#============================================================================
# Configure Main Scheduler Properties  
#============================================================================
org.quartz.scheduler.instanceId: AUTO
org.quartz.scheduler.skipUpdateCheck: true

#============================================================================
# Configure ThreadPool  
#============================================================================

org.quartz.threadPool.class: org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount: 10
org.quartz.threadPool.threadPriority: 5
org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread=true

#============================================================================
# Configure JobStore  
#============================================================================
org.quartz.jobStore.misfireThreshold: 1000
org.quartz.jobStore.class: org.quartz.impl.jdbcjobstore.JobStoreCMT
org.quartz.jobStore.driverDelegateClass: org.quartz.impl.jdbcjobstore.oracle.weblogic.WebLogicOracleDelegate
org.quartz.jobStore.useProperties: false
org.quartz.jobStore.tablePrefix: QRTZ_

org.quartz.jobStore.isClustered=true
org.quartz.jobStore.clusterCheckinInterval=60000
#============================================================================
# Configure Plugins 
#============================================================================
org.quartz.plugin.shutdownHook.class: org.quartz.plugins.management.ShutdownHookPlugin
org.quartz.plugin.shutdownHook.cleanShutdown: true

org.quartz.plugin.triggHistory.class: org.quartz.plugins.history.LoggingJobHistoryPlugin

 

 

  

以上是关于Spring整合quart初识的主要内容,如果未能解决你的问题,请参考以下文章

初识Spring源码 -- doResolveDependency | findAutowireCandidates | @Order@Priority调用排序 | @Autowired注入(代码片段

spring注解配置quartz应用

spring+quarts常见问题

[原创] 在spring 中使用quarts

Spring+SpringMVC+MyBatis+Maven框架整合

Spring Boot&&Spring Cloud系列Spring Boot初识