Spring的Quartz定时机制

来源:互联网 发布:玖富微理财网络借贷 编辑:程序博客网 时间:2024/04/27 05:49

Spring的Quartz定时机制

 

1、在导入完spring标准配置的包外,下载一个叫做quartz-all-1.6.1.jar的包,并导入。

2、创建一个xml的文件,名字任意,并写入一下代码

<?xmlversion="1.0"encoding="UTF-8"?>

<!DOCTYPEbeansPUBLIC "-//SPRING//DTDBEAN//EN"

"http://www.springframework.org/dtd/spring-beans.dtd">

 

<beans>

    <!—指定你要执行的任务在哪个类中 -->

    <beanid="mainTask"class="com.pb.service.Impl.OrderServiceImpl"/>  

   <!—指定jar类,该类其实是在spring-context-support.jar这个jar包中 -->

    <beanid="mainJob"class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">

        <propertyname="targetObject">

             <!--将你的类添加到定时器当中 -->

             <refbean="mainTask"/>

         </property>

        <propertyname="targetMethod">

        <!—指定你要执行上面指定的类里面的哪个方法 -->

            <value>findAllCustomerWillLost</value>

        </property>

</bean>

 

<beanid="timeTrigger"class="org.springframework.scheduling.quartz.CronTriggerBean">

        <propertyname="jobDetail">

           <refbean="mainJob"/>

        </property>

        <propertyname="cronExpression">

        <!—指定触发定时器的时间  附件中会有定时器定时的语法 -->

            <value>0 0 12 ? * SAT</value> 

        </property>

</bean>

 

<!—创建定时器-->

<beanid="sfb"class="org.springframework.scheduling.quartz.SchedulerFactoryBean">

        <propertyname="triggers">

            <list>

                <reflocal="timeTrigger"/>

            </list>

        </property>

    </bean>

</beans>

 

3、创建定时器任务类和方法

   /**

    * 这是一个查询在某一段时间内没有新增订单的客户,如果该客户最后订单时间和现在时间比相差180天,则进行客户流失预警。

    */

   @Override

   publicvoidfindAllCustomerWillLost() {

      List<CstCustomer> list =customerDao.findAllCustomer();

      for(CstCustomer cust : list){

         CstOrder lastOrder = orderDao.findLastOrder(cust.getCustNo());

          //进行时间差运算  

         SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

         Timestamp currentDate = Timestamp.valueOf(df.format(new Date()));//格式化当前时间

         Calendar cl = Calendar.getInstance();

         cl.setTime(lastOrder.getOdrDate());

         long begin = cl.getTimeInMillis();//将订单时间set给对象后并转化成毫秒

         cl.setTime(currentDate);

         long end = cl.getTimeInMillis();//将当前时间set给对象后并转换成毫秒

         long val = (end - begin) / (1000 * 3600 * 24);

         intnoOrderTimer = Integer.parseInt(String.valueOf(val));

         if(val >= 180){

            CstLost custLost = new CstLost();

            custLost.setCstCustomer(cust);

         custLost.setLstCustManagerName(cust.getMangerUser());

         custLost.setLstLastOrderDate(lastOrder.getOdrDate());

            custLost.setLstStatus("流失预警");

            custLostDao.creatCustLost(custLost);

           

         }

      }

   }

  

4、配置web.xml

将上面配置的timerConfig.xml配置文件配置到web.xml中,在系统启动时启动监听

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
       xmlns="http://java.sun.com/xml/ns/javaee"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
       http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
       <context-param>
    <param-name>
        contextConfigLocation
    </param-name>
    <param-value>
        timerConfig.xml
    </param-value>
       </context-param>
       <listener>
          <listener-class>
              org.springframework.web.context.ContextLoaderListener
          </listener-class>
       </listener>
  <welcome-file-list>
   <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

常见问题:

 

问题一

在配置完监听器并写好任务类后,有时会报找不到org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean等这种类,这是因为在你导入完quartz-all-1.6.1.jar包后,还需要导入一个spring-context-support.jar包,因为上面这种org.springframework.scheduling.quartz。。。的类其实都在support包里面。

 

问题二

在启动时还会遇到这种Caused by:java.lang.NoSuchMethodError:org.apache.commons.collections.SetUtils.orderedSet(Ljava/util/Set;)Ljava/util/Set;问题,如果遇见这种关于SetUtils的问题,大部分都是因为现有的commons-collections.jar包或开发工具自带的commons-collections.jar包版本太低或缺少该包,一般要求该包为3.0以上版本,换完包后还不行的话,而且没有找到其他的包的话很可能是MyEclipse自带的Liberary中包括了版本低的包而你导入了这个lib。
比如MyEclipse的Hibernate的Lib中包括commons-collections.jar但是版本是2.*。
Window->Preferences->Myeclipse->Project Capabilities->Hibernate
从 lib中删除此包即可。(注:此时有三个选项卡hibernate2,hibernate3,hibernate3.1。),用的哪个版本选哪个。下面有 commons-collections.jar包。把它删了。如果还不行。看看你的tomcat发布工程的classpath下是否有commons- collections.jar包。

 

 

 

附:定时器指定触发时间的语法

<!--
0 0 10,14,16 * * ? 每天上午10点,下午2点,4点
0 0/30 9-17 * * ?   朝九晚五工作时间内每半小时
0 0 12 ? * WED 表示每个星期三中午12点
"0 0 12 * * ?" 每天中午12点触发
"0 15 10 ? * *" 每天上午10:15触发
"0 15 10 * * ?" 每天上午10:15触发
"0 15 10 * * ? *" 每天上午10:15触发
"0 15 10 * * ? 2005" 2005年的每天上午10:15触发
"0 * 14 * * ?" 在每天下午2点到下午2:59期间的每1分钟触发
"0 0/5 14 * * ?" 在每天下午2点到下午2:55期间的每5分钟触发
"0 0/5 14,18 * * ?" 在每天下午2点到2:55期间和下午6点到6:55期间的每5分钟触发
"0 0-5 14 * * ?" 在每天下午2点到下午2:05期间的每1分钟触发
"0 10,44 14 ? 3 WED" 每年三月的星期三的下午2:10和2:44触发
"0 15 10 ? * MON-FRI" 周一至周五的上午10:15触发
"0 15 10 15 * ?" 每月15日上午10:15触发
"0 15 10 L *?" 每月最后一日的上午10:15触发
"0 15 10 ? * 6L"每月的最后一个星期五上午10:15触发
"0 15 10 ? * 6L2002-2005" 2002年至2005年的每月的最后一个星期五上午10:15触发
"0 15 10 ? * 6#3" 每月的第三个星期五上午10:15触发

 

 部分讲述来自网络资源,第一次写博,见谅

0 0