定时任务(Spring与Quartz使用)

这是一个spring4和Quartz-2.2.1的整合使用,定时执行某一个方法。Quartz-2.2.1.jar下载

类方法:

package com.test.quartz;

public class FirstQuartz {

	public void doTestJob() {
		System.out.println("\n测试定时任务:" +
		(new java.text.SimpleDateFormat("yyyy-MM-dd hh:mm:ss:SSS")).format(new java.util.Date()) + "\n"
				+"当前线程:"+Thread.currentThread());
	}
	
}
在spring中配置:

可以单独写一个定时的配置文件:applicationContext-quartz.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:context="http://www.springframework.org/schema/context"
	xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
    
    
	<!-- 定时任务Bean -->
	<bean id="firstQuartz" class="com.test.quartz.FirstQuartz" />
	

	<!-- 定义调用对象和调用对象的方法 -->
	<bean id="testDetail"
		class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
		<!-- 调用的类 -->
		<property name="targetObject" ref="firstQuartz" />
		<!-- 调用类中的方法 -->
		<property name="targetMethod" value="doTestJob" />
		<!-- 是否允许任务并发执行。当值为false时,表示必须等到前一个线程处理完毕后才再启一个新的线程 -->
		<property name="concurrent" value="false" />
	</bean>
	
	<!-- quartz-2.x后的配置 -->
	<bean id="myJobTrigger"
		class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
		<property name="jobDetail">
			<ref bean="testDetail" />
		</property>
		<property name="cronExpression">
<!-- 时间大小由小到大排列,从秒开始,顺序为 秒,分,时,天,月,年    *为任意 ?为无限制 ,0/n  每过n单位时间执行一次具体的格式请参照:http://z3sm2012.iteye.com/blog/1736534 -->
			<value>0/10 * * * * ?</value>
		</property>
	</bean>
	

	<!-- 总管理类:如果lazy-init='false',那么容器启动后就会执行调度程序 -->
	<!-- 如果lazy-init='true',则需要实例化该bean才能执行调度程序 -->
	<bean name="testQuertz" lazy-init="false" autowire="no"
		class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
  		<property name="triggers">
			<list>
				<ref bean="myJobTrigger" />
			</list>
		</property>
		<!-- <property name="autoStartup" value="true"/> -->
	</bean>
</beans>
在web.xml文件中不要忘记添加applicationContext-quartz.xml的配置:

可以使用如下代码:

  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext*.xml</param-value>
  </context-param>


在JUint4中进行测试:

package com.test.quartz;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.transaction.TransactionConfiguration;
import org.springframework.transaction.annotation.Transactional;

/**
 * 测试定时任务
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:applicationContext*.xml" })
@TransactionConfiguration
@Transactional
public class TestQuartz {
	
	@Autowired
	private ApplicationContext applicationContext;

	@Test
	public void test_job() {
		//观察一分钟:请先调整定时任务的间隔时间,建议十秒
		try {
			//还可以在这里获取Spring容器上下文
			System.out.println("\n获取Spring容器上下文:\n" + applicationContext + "\n");
			
			Thread.sleep(30000);//单位:毫秒,所以一分钟:60*1000毫秒
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
}


更多的参考:http://gong1208.iteye.com/blog/1773177


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值