Quartz schedulerContextAsMap 和jobDataMap

1.schedulerContextAsMap

配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:task="http://www.springframework.org/schema/task"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd
                           http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
                           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">


    <bean id="schedulerFactoryBean" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <property name="applicationContextSchedulerContextKey" value="applicationContext" />
        <property name="configLocation" value="classpath:quartz.properties" />
        <property name="dataSource" ref="dataSource" />
        <property name="transactionManager" ref="transactionManager" />
         <property name="schedulerContextAsMap">      
            <map>          
                <description>schedulerContextAsMap</description>      
                <entry key="monthlyreportqtzservice" value-ref="monthlyreportqtzservice"/>
                <entry key="reGenMonthlyReportForRatingingineService" value-ref="reGenMonthlyReportForRatingingineService" />
                <entry key="smfQtz" value-ref="smfQtz" />
            <entry key="reGenSMFReportForRatingengineService" value-ref="reGenSMFReportForRatingengineService" />
            </map>      
        </property> 
        <property name="jobDetails">
            <list>

                <ref bean="smfReportWithParameterTask"/>
            </list>        
        </property>

        <property name="triggers">
            <list>

                <ref bean="smfReportWithParameterTrigger"/>
            </list>        
        </property>


    </bean>

    <bean id="smfReportJobTask" class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
        <property name="group" value="AdminFee"/>
        <property name="durability" value="true" />
        <property name="jobClass" value="com.ncs.customerconnect.ratingengine.batch.task.SmfReportJobTask"></property>
    </bean>

     <bean id="smfReportCronTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
       <property name="group" value="AdminFee"/>
       <property name="jobDetail" ref="smfReportJobTask"/>
       <!--<property name="cronExpression" value="0 0/5 * * * ?"/>-->
       <property name="cronExpression" value="0 58 16 * * ? 2018" />
       <property name="misfireInstruction" value="2"/>
       <property name="description" value="AdminFee(Every 5 mins run)"/>
    </bean> 

</beans>

如何通过spring 向quartz动态注入service或者,传入动态参数?

http://blog.csdn.net/whaosy/article/details/6298686

spring容器中的bean只能放到SchedulerContext里面传入job中
: spring 管理的service需要放到这里,才能够注入成功
这样就可以在list中注入的bean中

    <list>
                <ref bean="smfReportWithParameterTask"/>
            </list>     
           这些类中使用那些service了,不过需要使用setter方法注入bean,不能使用注解的方式注入(师兄说的,没有亲测)
<!--applicationContextSchedulerContextKey: 是org.springframework.scheduling.quartz.SchedulerFactoryBean这个类中把spring上下 文以key/value的方式存放在了quartz的上下文中了,可以用applicationContextSchedulerContextKey所定义的key得到对应的spring上下文-->  

http://blog.sina.com.cn/s/blog_8f329b7b0101pqow.html

Quartz提供了调度运行环境的持久化机制,可以保存并恢复调度现场,即使系统因故障关闭,任务调度现场数据并不会丢失。

SchedulerFactoryBean属性介绍:

●triggers:triggers属性为Trigger[]类型,可以通过该属性注册多个Trigger
●calendars:类型为Map,通过该属性向Scheduler注册Calendar;
●jobDetails:类型为JobDetail[],通过该属性向Scheduler注册JobDetail;
●autoStartup:SchedulerFactoryBean在初始化后是否马上启动Scheduler,默认为true。如果设置为false,需要手工启动Scheduler;
●startupDelay:在SchedulerFactoryBean初始化完成后,延迟多少秒启动Scheduler,默认为0,表示马上启动。如果并非马上拥有需要执行的任务,可通过startupDelay属性让Scheduler延迟一小段时间后启动,以便让Spring能够更快初始化容器中剩余的Bean;

SchedulerFactoryBean的一个重要功能是允许你将Quartz配置文件中的信息转移到Spring配置文件中,带来的好处是,配置信息的集中化管理,同时我们不必熟悉多种框架的配置文件结构。回忆一个Spring集成JPA、Hibernate框架,就知道这是Spring在集成第三方框架经常采用的招数之一。SchedulerFactoryBean通过以下属性代替框架的自身配置文件:

●dataSource:当需要使用数据库来持久化任务调度数据时,你可以在Quartz中配置数据源,也可以直接在Spring中通过dataSource指定一个Spring管理的数据源。如果指定了该属性,即使quartz.properties中已经定义了数据源,也会被此dataSource覆盖;
●transactionManager:可以通过该属性设置一个Spring事务管理器。在设置dataSource时,Spring强烈推荐你使用一个事务管理器,否则数据表锁定可能不能正常工作;
●nonTransactionalDataSource:在全局事务的情况下,如果你不希望Scheduler执行化数据操作参与到全局事务中,则可以通过该属性指定数据源。在Spring本地事务的情况下,使用dataSource属性就足够了;
●quartzProperties:类型为Properties,允许你在Spring中定义Quartz的属性。其值将覆盖quartz.properties配置文件中的设置,这些属性必须是Quartz能够识别的合法属性,在配置时,你可以需要查看Quartz的相关文档。

配置好数据源dataSource后,需要在Quartz的QRTZ_LOCKS表中插入以下数据:

INSERT INTO QRTZ_LOCKS values(‘TRIGGER_ACCESS’);
INSERT INTO QRTZ_LOCKS values(‘JOB_ACCESS’);
否则会报
org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘scheduler’ defined in file […webappsWEB-INFclassesconfigapplicationContext-quartz.xml]: Invocation of init method failed; nested exception is org.quartz.SchedulerConfigException: Failure occured during job recovery. [See nested exception: org.quartz.impl.jdbcjobstore.LockException: Failure obtaining db row lock: No row exists in table QRTZ_LOCKS for lock named: TRIGGER_ACCESS [See nested exception: java.sql.SQLException: No row exists in table QRTZ_LOCKS for lock named: TRIGGER_ACCESS]]异常

2.jobDataMap

暂未理解明白

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值