• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录

cynchanpin

  • 博客园
  • 联系
  • 订阅
  • 管理

View Post

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


quartz---任务调度小试

 

 

        背景

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

 

        配置

        我的项目用的是springMVC,spring+Ejb,EclipseLink,server用的是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"/>
       ………..

         结果

        接下来看看我打印的信息

 

        总之,这样下来在我的项目中是可以正常使用了,可是我想当运行任务调度的时候是不是可以单独的给它新起一个线程,这样会不会更好呢?这块因为这几天项目比較忙一直没弄,总之,看兴许更新的博客吧。

 

 

 

 

posted on 2017-07-28 10:24  cynchanpin  阅读(583)  评论(0)    收藏  举报

刷新页面返回顶部
 
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3