Use spring to integrate Quartz to implement the timer (Maven project for demonstration)
Methods that are not based on specific base classes
1. Development environment and dependency jar packages
Spring 4.2.6.RELEASE
Maven 3.3.9
Jdk 1.7
Idea 15.04
2. An indispensable jar dependency (added in the pom.xml file in the maven project)
<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>
3. Files used when implementing the timer:
planWorkExcute.java -- Class executed by timer
spring-plan.xml --xml for configuring timer information
Fourth, implement the timer steps:
1. Create the planWorkExcute.java file, under the cc.royao.plantask package.
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;//You can delete import org.springframework.beans.factory.annotation.Autowired;public class PlanWorkExecute { Logger logger = Logger.getLogger(this.getClass());//logger prints the log, and you can remove /** * Method of execution of timer*/ public synchronized void withdrawNoAuditTask() { SimpleDateFormat outFormat = new SimpleDateFormat("yyyyy MM month ddd date HH:mm:ss"); System.out.println("Start withdrawal exemption task------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- logger.info("Start withdrawal and exemption from review task-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------2. Create spring-plan.xml configuration file Note: Just create a timer configuration file. If multiple timers are needed, just add beans in spring-plan.xml and define the timer class method. There is no need to create multiple xml.
・ For the Cron expression of how long the timer will be executed, please refer to: http://www.VeVB.COM/article/138900.htm
・The URL for generating expressions online: 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" /><!-- Modify the path to your timing class --><!-- You can create multiple timing beans --><bean id="jobDetail_1"> <property name="targetObject"> <ref bean="job1" /> </property> <property name="targetMethod"> <value>withdrawNoAuditTask</value><!-- Method name of the timer class --> </property></bean><bean id="cronTrigger_1"> <property name="jobDetail"> <ref bean="jobDetail_1" /> <!-- This corresponds to the above bean--> </property> <property name="cronExpression"> <value>0/2 * * * * ?</value><!-- 0 10 0 * * ? Execute at 0:10 every day --> </property></bean><bean> <property name="triggers"> <list> <ref local="cronTrigger_1" /> <!-- Every timer added, you must also add --> </list> </property></bean></beans>
3. You need to introduce spring-plan.xml in applicationContext.xml. The following code focuses on the bottom line
<?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 http://www.springframework.org/schema/cache/spring-cache-3.2.xsd" default-lazy-init="true"> <!-- Loading system properties file configuration--> <bean id="propertyConfigurer"> <property name="locations"> <list> <value>WEB-INF/jdbc.properties</value> <!-- <value>WEB-INF/sms.properties</value> --> </list> </property> </bean> <bean id="dataSource"> <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>60000000</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="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"> <property name="dataSource" ref="dataSource"/> </bean> <bean id="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"> <property name="dataSource" ref="dataSource"></property> </bean> <!--<!--<!--<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> <!-- Configure transaction notification--> <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="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> <property name="basePackage"> <value>cc.royao.mana.auth.mapper.*</value> </property> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> </bean> <import resource="application-servlet.xml"/> <!-- The point is here, I copied the entire xml file content, afraid you don't know where to insert it --> <import resource="spring-plan.xml"/></beans>Summarize
The above is what the editor introduced to you using spring integrated Quartz to implement the timer function. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time. Thank you very much for your support to Wulin.com website!