欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

使用spring整合Quartz实现—定时器功能

程序员文章站 2023-11-29 10:03:10
使用spring整合quartz实现—定时器(maven项目做演示) 不基于特定的基类的方法 一,开发环境以及依赖的jar包     spring 4.2.6.rel...

使用spring整合quartz实现—定时器(maven项目做演示)

不基于特定的基类的方法

一,开发环境以及依赖的jar包

    spring 4.2.6.release

    maven 3.3.9

    jdk 1.7

    idea 15.04

二,不可少的jar依赖(添加在maven项目里面的pom.xml文件里面)

 <dependency>
 <groupid>org.springframework</groupid>
 <artifactid>spring-context-support</artifactid>
 <version>4.2.6.release</version>
 </dependency>
 <dependency>
 <groupid>org.quartz-scheduler</groupid>
 <artifactid>quartz</artifactid>
 <version>2.2.1</version>
 </dependency>

三,实现定时器时使用到的文件:

     planworkexcute.java    --定时器执行的类

     spring-plan.xml    --配置定时器信息的xml

四,实现定时器步骤:

   1,创建 planworkexcute.java文件  ,在   cc.royao.plantask   包下。      

package cc.royao.plantask;
import java.text.simpledateformat;
import java.util.date;
import java.util.hashmap;
import java.util.list;
import java.util.map;
import java.util.concurrent.executorservice;
import java.util.concurrent.executors;
import org.apache.log4j.logger;//可以删除
import org.springframework.beans.factory.annotation.autowired;
public class planworkexecute {
 logger logger = logger.getlogger(this.getclass());//logger打印日志,可以去掉
 /**
 * 定时器执行的方法
 */
 public synchronized void withdrawnoaudittask() {
 simpledateformat outformat = new simpledateformat("yyyy年mm月dd日 hh:mm:ss");
 system.out.println("开始提现免审核任务-------------------------------" + outformat.format(new date()));
 logger.info("开始提现免审核任务-------------------------------");
 system.out.println("结束提现免审核任务-------------------------------" + outformat.format(new date()));
 logger.info("结束提现免审核任务-------------------------------");
 }
}

  2,创建spring-plan.xml  配置文件  注:创建一个定时器的配置文件就行,如果需要多个定时器,直接在spring-plan.xml添加 bean和定义定时器类的方法就行,不需要创建多个xml,

      · 关于那个定时器多久执行的   cron表达式 可以参考:

      ·有在线生成表达式的网址:

<?xml version="1.0" encoding="utf-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"
xsi:schemalocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"
default-lazy-init="false">
<bean id="job1" class="cc.royao.plantask.planworkexecute" /><!-- 修改为你的定时类的路径 -->
<!-- 可以创建多个定时bean -->
<bean id="jobdetail_1"
 class="org.springframework.scheduling.quartz.methodinvokingjobdetailfactorybean">
 <property name="targetobject">
 <ref bean="job1" /> 
 </property>
 <property name="targetmethod">
 <value>withdrawnoaudittask</value><!-- 定时器类的方法名-->
 </property>
</bean>
<bean id="crontrigger_1"
 class="org.springframework.scheduling.quartz.crontriggerfactorybean">
 <property name="jobdetail">
 <ref bean="jobdetail_1" /> <!-- 这里对应上面bean-->
 </property>
 <property name="cronexpression">
 <value>0/2 * * * * ?</value><!-- 0 10 0 * * ? 每天0:10执行 -->
 </property>
</bean>
<bean
 class="org.springframework.scheduling.quartz.schedulerfactorybean">
 <property name="triggers">
 <list>
 <ref local="crontrigger_1" /> <!-- 每加一个定时器这里也要加-->
 </list>
 </property>
</bean>
</beans>

  3,需要在  applicationcontext.xml 中引入  spring-plan.xml    以下代码重点在最下

<?xml version="1.0" encoding="utf-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"
 xmlns:jee="http://www.springframework.org/schema/jee"
 xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
 xmlns:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc"
 xmlns:cache="http://www.springframework.org/schema/cache"
 xsi:schemalocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
   http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
   http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
   http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.2.xsd"
 default-lazy-init="true">
 <!-- 加载系统properties文件配置 -->
 <bean id="propertyconfigurer" class="org.springframework.beans.factory.config.propertyplaceholderconfigurer">
 <property name="locations">
  <list>
  <value>web-inf/jdbc.properties</value>
  <!-- <value>web-inf/sms.properties</value> -->
  </list>
 </property>
 </bean>
 <bean id="datasource" class="org.apache.commons.dbcp.basicdatasource">
 <property name="driverclassname">
  <value>${jdbc.driverclass}</value>
 </property>
 <!--<property name="defaultautocommit" value="false"/>-->
 <property name="url">
  <value>jdbc:mysql://192.168.14.239:3306/test?useunicode=true&characterencoding=utf-8</value>
 </property>
 <property name="username">
  <value>${jdbc.username}</value>
 </property>
 <property name="password">
  <value>${jdbc.password}</value>
 </property>
 <property name="maxactive">
  <value>20</value>
 </property>
 <property name="maxidle">
  <value>60</value>
 </property>
 <property name="maxwait">
  <value>20000</value>
  <!-- 0 -->
 </property>
 <property name="removeabandoned">
  <value>true</value>
 </property>
 <property name="removeabandonedtimeout">
  <value>6000000</value>
  <!-- 180 -->
 </property>
 <!-- add -->
 <property name="validationquery" value="select 1"></property>
 <property name="testwhileidle" value="true"></property>
 <property name="testonborrow" value="true"></property>
 <property name="timebetweenevictionrunsmillis" value="3600000"></property>
 <property name="numtestsperevictionrun" value="50"></property>
 <property name="minevictableidletimemillis" value="120000"></property>
 <!-- add -->
 </bean>
 <!-- sqlsessionfactory -->
 <bean id="sqlsessionfactory" class="org.mybatis.spring.sqlsessionfactorybean">
 <property name="datasource" ref="datasource"/>
 </bean>
 <bean id="threadpooltaskexecutor" class="org.springframework.scheduling.concurrent.threadpooltaskexecutor">
 <property name="corepoolsize" value="1"/>
 <property name="maxpoolsize" value="10"/>
 <property name="keepaliveseconds" value="300"/>
 <property name="queuecapacity" value="50"/>
 <property name="waitfortaskstocompleteonshutdown" value="true"/>
 </bean>
 <bean id="transactionmanager" class="org.springframework.jdbc.datasource.datasourcetransactionmanager">
 <property name="datasource" ref="datasource"></property>
 </bean>
 <!--<!– 自动扫描service实现 –>-->
 <!--<context:component-scan base-package="com.royao">-->
 <!--<context:include-filter type="regex"-->
 <!--expression="com.royao.services.*" />-->
 <!--</context:component-scan>-->
 <aop:config proxy-target-class="true">
 <aop:pointcut id="serviceoperation" expression="execution(* cc.royao.mana.auth.service.*.impl.*serviceimpl.*(..))"/>
 <aop:advisor pointcut-ref="serviceoperation" advice-ref="txadvice"/>
 </aop:config>
 <!-- 配置事务通知 -->
 <tx:advice id="txadvice" transaction-manager="transactionmanager">
 <tx:attributes>
  <tx:method name="*" rollback-for="exception"/>
 </tx:attributes>
 </tx:advice>
 <tx:advice id="transactionmanageradivice" transaction-manager="transactionmanager">
 <tx:attributes>
  <tx:method name="*insert*" propagation="required"/>
  <tx:method name="*add*" propagation="required"/>
  <tx:method name="*update*" propagation="required"/>
  <tx:method name="*update*" propagation="required"/>
  <tx:method name="*del*" propagation="required"/>
  <tx:method name="*create*" propagation="required"/>
  <tx:method name="doapproved" propagation="required"/>
  <tx:method name="batchdelfm" propagation="required"/>
  <tx:method name="edittemplate" propagation="required"/>
  <tx:method name="dummydelete" propagation="required"/>
  <tx:method name="batchdeluser" propagation="required"/>
  <!--<tx:method name="*" propagation="required"/>-->
 </tx:attributes>
 </tx:advice>
 <bean class="org.mybatis.spring.mapper.mapperscannerconfigurer">
 <property name="basepackage">
  <value>cc.royao.mana.auth.mapper.*</value>
 </property>
 <property name="sqlsessionfactorybeanname" value="sqlsessionfactory"/>
 </bean>
 <import resource="application-servlet.xml"/>
  <!-- 重点在这里 ,我把整个xml文件内容复制出来,怕你们不知道插入在哪里-->
 <import resource="spring-plan.xml"/>
</beans>

总结

以上所述是小编给大家介绍的使用spring整合quartz实现—定时器功能,希望对大家有所帮助