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"/>
………..

         结果

接下来看看我打印的信息

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

最新文章

  1. Oracle创建自增ID
  2. ThinkInside
  3. github添加ssh key报错Key is invalid. Ensure you&#39;ve copied the file correctly
  4. Mex文件在VS2010中调试方法
  5. [Spring] - Property注入
  6. vijos1144(小胖守皇宫)
  7. AngularJS模块加载
  8. 团队项目——NABC
  9. Android线性布局(Linear Layout)
  10. CodeForces 589B Layer Cake (暴力)
  11. (转载)内联函数inline和宏定义
  12. Java基础知识强化之IO流笔记59:打印流
  13. 在MAC下 Python+Django+mysql配置
  14. J2SE习题(2)
  15. mysql 基础之CURD
  16. C#网络程序设计(1)网络编程常识与C#常用特性
  17. 服务器cpu100%问题分析
  18. Android基础知识02—安卓日志工具LogCat的五种方法
  19. window.open 打开子窗口,关闭所有的子窗口
  20. md5加密utils

热门文章

  1. 【bzoj3029】守卫者的挑战 概率dp
  2. gerrit工具-workflow
  3. 文明距离(civil)
  4. eclipse快捷键及各种设置
  5. Codeforces Round #281 (Div. 2) B 模拟
  6. spring in action学习笔记十五:配置DispatcherServlet和ContextLoaderListener的几种方式。
  7. centos 7 安装配置mod_security
  8. do_exit——&gt;exit_notify()【转】
  9. python笔记-用python解决小学生数学题【转载】
  10. Ubuntu 14.04LTS+Git