springMVC项目中配置定时任务的两种方式

第一种:spring自带的定时任务功能

 

一)在xml里加入task的命名空间

 


 
xmlns:task="http://www.springframework.org/schema/task" http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.1.xsd

 

(二)启用注解驱动的定时任务

<task:annotation-driven scheduler="myScheduler"/> 

(三)配置定时任务的线程池(多任务时需要进行配置)

配置线程池,若不配置多任务下会有问题。后面会详细说明单线程的问题。

<task:scheduler id="myScheduler" pool-size="5"/>

 

(四)任务类

 

@Component("regularTasks")
public class RegularTasks {
    @Resource
    private ContractManager   contractManager;
    @Resource
    private PayDetailsManager payDetailsManger;

    /*@Scheduled(fixedRate = 1000 * 5) //心跳更新。启动时执行一次,之后每五秒执行一次
    public void updateContractPayStatus() {
        
    }*/
}

上面是spring的注解方式搞的定时任务。
 

 

 

第二种:spring结合quartz配置定时任务

(一)依赖:

 

<dependency>
	<groupId>org.quartz-scheduler</groupId>
	<artifactId>quartz</artifactId>
</dependency>	<groupId>org.quartz-scheduler</groupId>
	<artifactId>quartz</artifactId>
</dependency>

(二)配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" 
	   xmlns:aop="http://www.springframework.org/schema/aop" 
	   xmlns:context="http://www.springframework.org/schema/context"
	   xmlns:mvc="http://www.springframework.org/schema/mvc" 
	   xmlns:tx="http://www.springframework.org/schema/tx" 
	   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	   xsi:schemaLocation="http://www.springframework.org/schema/aop 
		http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 
		http://www.springframework.org/schema/beans 
		http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
		http://www.springframework.org/schema/context 
		http://www.springframework.org/schema/context/spring-context-3.0.xsd 
		http://www.springframework.org/schema/mvc 
		http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd 
		http://www.springframework.org/schema/tx 
		http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

	<!-- Timer schedule -->
	<!--  总管理类如果将lazy-init='false'那么容器启动就会执行调度程序   -->
	<bean id="startQuertz" class="org.springframework.scheduling.quartz.SchedulerFactoryBean" lazy-init="false" >
 		<property name="triggers">
 			<!-- 作业调度器,list下可加入其他的调度器 -->
 			<list>
				<ref bean="sendMessageTrigger" />
 			</list>
		</property>
		<property name="autoStartup" value="true"/>
	</bean>
	
	<!-- start 轮巡发送信息-->
	<bean id="sendMessageJobBean" class="com.biz.timeTask.TimeTask"/> <!-- 任务类 -->
	<bean id="sendMessageJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
		<!-- 调用的类 -->
		<property name="targetObject" ref="sendMessageJobBean" />
		<!-- 调用类中的方法 -->
		<property name="targetMethod" value="execute" />
		<!-- 将并发设置为false -->
		<property name="concurrent" value="false" />
	</bean>
	<bean id="sendMessageTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
		<property name="jobDetail" ref="sendMessageJobDetail" />
        <property name="cronExpression" value="0 0/10 * * * ?" /><!-- 0 0/60 * * * ?     0 0 12 * * ?-->
	</bean>                           <!-- 这个value是写入执行时间表达式的 -->
	<!-- end  轮巡发送信息-->
</beans>

 

 

(三)任务类

 

public class TimeTask {
    
    public void execute() {
        
    }
}

 

 

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章