quartz---任务调度小试(多任务)

来源:互联网 发布:工商 安排网络监管人员 编辑:程序博客网 时间:2024/06/12 18:13


quartz---任务调度小试

 

 

        背景

        笔者目前做的项目”jrkj“首页上的信息都是从redis中读取的,每小时更新一次存入redis中,那么问题来了怎么才能让系统每个小时执行一次存入数据的方法呢?这个我用到的事quartz任务调度框架。

 

        配置

        我的项目用的是springMVC,spring+Ejb,EclipseLink,服务器用的是Jboss。由于项目中用到的Ejb所以在写配置文件applicationContext-common.xml的时候还是需要注意一些东西的,详细见配置文件。

<?xml version="1.0"encoding="UTF-8"?><beansxmlns="http://www.springframework.org/schema/beans"   xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"   xmlns:util="http://www.springframework.org/schema/util"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   xmlns:context="http://www.springframework.org/schema/context"   xmlns:jee="http://www.springframework.org/schema/jee"   xsi:schemaLocation="http://www.springframework.org/schema/beans   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd   http://www.springframework.org/schema/aop   http://www.springframework.org/schema/aop/spring-aop-3.0.xsd   http://www.springframework.org/schema/tx   http://www.springframework.org/schema/tx/spring-tx-3.0.xsd    http://www.springframework.org/schema/util   http://www.springframework.org/schema/util/spring-util-3.0.xsd   http://www.springframework.org/schema/context   http://www.springframework.org/schema/context/spring-context-3.0.xsd   http://www.springframework.org/schema/jee   http://www.springframework.org/schema/jee/spring-jee-2.5.xsd">   <context:component-scanbase-package="com.tgb.itoo.jrkj.controller" />   <util:properties id="evn"       location="classpath:config/jboss-ejb-client.properties"></util:properties>   <!-- 启动触发器的配置开始 -->   <bean name="startQuertz" lazy-init="false"autowire="no"       class="org.springframework.scheduling.quartz.SchedulerFactoryBean">       <property name="triggers">           <list>                <ref bean="myJobTrigger" />                <refbean="autoJobTrigger" />           </list>       </property>   </bean>   <!-- 启动触发器的配置结束 -->   <!-- 调度的配置开始 -->   <!-- quartz-2.x的配置 -->   <bean id="myJobTrigger"       class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">       <property name="jobDetail">           <ref bean="myJobDetail" />       </property>       <property name="cronExpression">           <value>0 0 0/1 * * ?</value><!--        <value>0 0/1 * * *?</value> -->       </property>   </bean>   <!-- 调度的配置结束 --><!-- 调度的配置开始 -->   <!-- quartz-2.x的配置 -->   <bean id="autoJobTrigger"       class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">       <property name="jobDetail">           <ref bean="autoJobDetail" />       </property>       <property name="cronExpression">           <value>0 0 3 * * ?</value>       </property>   </bean>   <!-- 调度的配置结束 -->   <!-- job的配置开始 -->   <bean id="autoJobDetail"       class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">       <property name="targetObject">           <ref bean="homePageShowController" />       </property>       <property name="targetMethod">           <value>autoClassEndOrderLog</value>       </property>   </bean>   <!-- job的配置开始 -->   <bean id="myJobDetail"       class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">       <property name="targetObject">           <ref bean="homePageShowController" />       </property>       <property name="targetMethod">           <value>loadHomeData</value><!--             <value>run</value>-->       </property>   </bean>      <jee:local-slsb id="homePageShowBean"       jndi-name="java:global/itoo-jrkj-homepageset-ear/itoo-jrkj-homepageset-core-0.0.1-SNAPSHOT/homePageShowBeanImpl!com.tgb.itoo.jrkj.service.HomePageShowBean"       business-interface="com.tgb.itoo.jrkj.service.HomePageShowBean"/>   <bean name="homePageShowController"class="com.tgb.itoo.jrkj.controller.HomePageShowController">       <property name="homePageShowBean"ref="homePageShowBean"></property>   </bean>   </beans>


        配置完成一个之后发现其实还有一些其他的的地方需要配置,所以您会发现上面的配置文件中配置了两个计时器,以及两个任务,如果业务有需要的话,还可以配置更多。

 

        Controller代码如下

@RequestMapping("/loadHomeData")public voidloadHomeData() {    System.out.println("test");    List<FieldVo>listFieldVos = new ArrayList<FieldVo>();    …… }


       别以为上面配置了,代码写了任务就完成了,jboss还需要配置依赖的jar包。

首先在jboss的modules\org\springframework\spring\snowdrop路径下添加quartz2.2.1.jar(目前我用的版本),然后在该路径下的module.xml中resources节点下添加<resource-rootpath="quartz2.2.1jar"/>

<pre name="code" class="plain"><modulexmlns="urn:jboss:module:1.0"name="org.springframework.spring"slot="snowdrop">  <resources>      <resource-root path="spring-aop-4.0.9.RELEASE.jar"/>       ……………       <resource-rootpath="spring-messaging-4.0.9.RELEASE.jar"/>        <resource-rootpath="spring-security-config-3.0.2.RELEASE.jar"/>        <resource-rootpath="commons-fileupload-1.3.1.jar"/>        <resource-rootpath="quartz2.2.1jar"/>       ………..

         结果

        接下来看看我打印的信息

 

        总之,这样下来在我的项目中是可以正常使用了,但是我想当执行任务调度的时候是不是能够单独的给它新起一个线程,这样会不会更好呢?这块由于这几天项目比较忙一直没弄,总之,看后续更新的博客吧。

 

 

 

 

1 1
原创粉丝点击