您好,登录后才能下订单哦!
使用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表达式 可以参考:https://www.jb51.net/article/138900.htm
·有在线生成表达式的网址:http://cron.qqe2.com/
<?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实现—定时器功能,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对亿速云网站的支持!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。