zoukankan      html  css  js  c++  java
  • 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的modulesorgspringframeworkspringsnowdrop路径下加入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"/>
           ………..
    
    

             结果

            接下来看看我打印的信息

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

  • 相关阅读:
    最详细的Vue Hello World应用开发步骤
    SAP Fiori + Vue = ?
    golang--连接redis数据库并进行增删查改
    golang--redis基本介绍
    golang--海量用户即时通讯系统
    (四十六)golang--网络编程(简易的聊天系统)
    动态规划--矿工挖矿
    (四十五)golang--反射
    动态规划--爬楼梯问题(入门)
    (四十四)golang--协程(goroutine)和管道(channel)相结合实例
  • 原文地址:https://www.cnblogs.com/cynchanpin/p/7248840.html
Copyright © 2011-2022 走看看