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() {
        
    }
}

 

 

 

 

 

 

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值