Quarts(定时)在Spring中应用实例

最近项目要实现一个定时批量提交数据的功能,采用Spring的Quarts的技术来实现。

以下是使用实例:

首先:

编写自己的Service类和方法,这些省略咯。。主要讲怎么使用Quarts:

package com.wonders.hs.registration.healthevent.front.quartz;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Logger;
import javax.xml.datatype.DatatypeConfigurationException;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;
import com.wonders.hs.registration.healthevent.front.service.HealthEventFrontService;

public class QuartzJobFront extends QuartzJobBean {
	private HealthEventFrontService healthEventFrontService;
	private Logger log = Logger.getAnonymousLogger();
	
	@Override
	protected void executeInternal(JobExecutionContext arg0)
			throws JobExecutionException {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		log.info("我是用quartz定时器打印出来的"+format.format(new Date()));
		try {
			long start = System.nanoTime();
			//批量注册前置表数据
			healthEventFrontService.registHealthEventFront();
			
			long end = System.nanoTime();
			log.info("总耗时:" + (end-start)/1000000000.0000);
		} catch (DatatypeConfigurationException e) {
			e.printStackTrace();
		}
	}
	
	public void setHealthEventFrontService(
			HealthEventFrontService healthEventFrontService) {
		this.healthEventFrontService = healthEventFrontService;
	}
	
}

 

在spring的配置文件中配置相关的Quarts信息:

<?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:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:jaxws="http://cxf.apache.org/jaxws"
	xsi:schemaLocation="
	http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
	http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
	http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
	">
    <!-- =================================================================== -->
	<!-- Context Define                                                      -->
	<!-- =================================================================== -->
    <bean
		id="propertyConfigurer"
		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpath:hibernate.properties</value>
            </list>
        </property>
    </bean>
    <!-- =================================================================== -->
	<!-- Data Source Define (jdbc & jndi)                                    -->
	<!-- =================================================================== -->
    <bean id="dataSource"
		class="org.apache.commons.dbcp.BasicDataSource">
		<property name="driverClassName" value="${hibernate.connection.driver_class}" />
		<property name="url" value="${hibernate.connection.url}" />
		<property name="username" value="${hibernate.connection.username}" />
		<property name="password" value="${hibernate.connection.password}" />
		<property name="maxActive" value="100"/>
        <property name="maxWait" value="60000"/>
        <property name="maxIdle" value="10"/>
	</bean>
	<!-- JNDI Configuration -->
	<!--<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
		<property name="jndiName" value="java:jdbc/healthevent_front"/>
	</bean>-->
    
    <!-- =================================================================== -->
	<!-- SessionFactory(For Hibernate)/ManagerFactory(For JPA) Define        -->
	<!--  ONLY ONE OF THEM SHOULD BE USED                                    -->
	<!-- =================================================================== -->
    <bean
		id="sessionFactory"
		class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        
        <property name="mappingDirectoryLocations">
            <list>
                <value>classpath:/com/wonders/hs/registration/healthevent/front/</value>
            </list>
        </property>
       
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">${hibernate.dialect}</prop>
                <prop key="hibernate.show_sql">false</prop>
                <!-- <prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
                <prop key="hibernate.cache.use_query_cache">true</prop> -->
            </props>
        </property>
    </bean>
    
    <!-- =================================================================== -->
	<!-- Transaction Define                                                  -->
	<!-- =================================================================== -->
	<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory" />
	</bean>
	
	<tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="save*" propagation="REQUIRED"/>
            <tx:method name="add*" propagation="REQUIRED"/>
            <tx:method name="remove*" propagation="REQUIRED"/>
            <tx:method name="del*" propagation="REQUIRED"/>
            <tx:method name="update*" propagation="REQUIRED"/>
            <tx:method name="edit*" propagation="REQUIRED"/>
            <tx:method name="get*" propagation="SUPPORTS" read-only="true"/>
            <tx:method name="find*" propagation="SUPPORTS" read-only="true"/>
            <tx:method name="*" propagation="REQUIRED"/>
        </tx:attributes>
    </tx:advice>
    <aop:config>
        <aop:pointcut id="serviceMethods" expression="execution(* com.wonders.hs.registration.healthevent.front.*.service.*.*(..))"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethods"/>
    </aop:config>

	<jaxws:client id="eventClient"
        serviceClass="com.wonders.hs.registration.healthevent.ws.register.HealthEventRegistrationPortType"
        address="http://10.1.25.168:8081/healthevent/EventRegister" />
	
    <!--DAO-->
    <bean id="healthEventFrontDao" class="com.wonders.hs.registration.healthevent.front.dao.HealthEventFrontDao">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
    
    <!--Service-->
    <bean id="healthEventFrontService" class="com.wonders.hs.registration.healthevent.front.service.impl.HealthEventFrontServiceImpl">
        <property name="healthEventFrontDao" ref="healthEventFrontDao"></property>
        <property name="port" ref="eventClient"></property>
    </bean>
    
    <!-- Quartz Job 定时操作 开始 -->
    <bean id="quartzJobFront" 
	    class="org.springframework.scheduling.quartz.JobDetailBean">
	  <property name="jobClass" value="com.wonders.hs.registration.healthevent.front.quartz.QuartzJobFront"/>
	  <property name="jobDataAsMap">
	  	<map>
	  		<entry key="healthEventFrontService">
	  			<ref bean="healthEventFrontService"/>
	  		</entry>
	  	</map>
	  </property>
	</bean>
	<bean id="cronEmailTrigger" 
	    class="org.springframework.scheduling.quartz.CronTriggerBean"> 
	  <property name="jobDetail" ref="quartzJobFront"/> 
	  <!--<property name="cronExpression" value="0 4 17 ? * *" />-->
	  <property name="cronExpression" value="2 * * * * ?" />
	</bean>
	<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> 
	  <property name="triggers"> 
	    <list> 
	      <ref bean="cronEmailTrigger"/> 
	    </list> 
	  </property> 
	</bean>
	<!-- Quartz Job 定时操作 结束 -->
	
</beans>

 

 其中<!-- Quartz Job 定时操作 开始 -->之后的代码是配置Quartz Job 定时操作的,嘿嘿。。

这样万事俱备,搞定啦。。。

另外,定时的时间设置貌似有点复杂,这里一并把他贴出来。。

附表 CronTrigger Expression( 来自http://quartz.sourceforge.net/javadoc/org/quartz/CronTrigger.html) 

Expression 
 Meaning 
 
"0 0 12 * * ?" 
 Fire at 12pm (noon) every day 
 
"0 15 10 ? * *" 
 Fire at 10:15am every day 
 
"0 15 10 * * ?" 
 Fire at 10:15am every day 
 
"0 15 10 * * ? *" 
 Fire at 10:15am every day 
 
"0 15 10 * * ? 2005" 
 Fire at 10:15am every day during the year 2005 
 
"0 * 14 * * ?" 
 Fire every minute starting at 2pm and ending at 2:59pm, every day 
 
"0 0/5 14 * * ?" 
 Fire every 5 minutes starting at 2pm and ending at 2:55pm, every day 
 
"0 0/5 14,18 * * ?" 
 Fire every 5 minutes starting at 2pm and ending at 2:55pm, AND fire every 5 minutes starting at 6pm and ending at 6:55pm, every day 
 
"0 0-5 14 * * ?" 
 Fire every minute starting at 2pm and ending at 2:05pm, every day 
 
"0 10,44 14 ? 3 WED" 
 Fire at 2:10pm and at 2:44pm every Wednesday in the month of March. 
 
"0 15 10 ? * MON-FRI" 
 Fire at 10:15am every Monday, Tuesday, Wednesday, Thursday and Friday 
 
"0 15 10 15 * ?" 
 Fire at 10:15am on the 15th day of every month 
 
"0 15 10 L * ?" 
 Fire at 10:15am on the last day of every month 
 
"0 15 10 ? * 6L" 
 Fire at 10:15am on the last Friday of every month 
 
"0 15 10 ? * 6L" 
 Fire at 10:15am on the last Friday of every month 
 
"0 15 10 ? * 6L 2002-2005" 
 Fire at 10:15am on every last friday of every month during the years 2002, 2003, 2004 and 2005 
 
"0 15 10 ? * 6#3" 
 Fire at 10:15am on the third Friday of every month 
 

 顺便上传quarts的包和一个相关的spring的包。。。。

 

另外,这篇别人写的讲Quartz在Spring中集群 的,http://www.iteye.com/topic/486055,大家可以看看!

 

=========================================================================

 

          看完文章,请到 “品润茶业”购买 茶叶茶具和零食,切记哦!  
           地址: http://prtea.taobao.com                                        

 

=========================================================================

你可能感兴趣的:(spring,AOP,Hibernate,bean,quartz)