spring 定时任务


本文前提,确保自己的web项目可以正常运行,即spring基本依赖包本地齐全。


spring的定时任务有3种,一一验证,亲测可用。


一、Spring-Task


xml配置  (注意不要漏掉   xmlns:task="http://www.springframework.org/schema/task")
<?xml version="1.0" encoding="UTF-8"?>
<beans  xmlns="http://www.springframework.org/schema/beans" 	
		xmlns:task="http://www.springframework.org/schema/task"
		xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
		xsi:schemaLocation="http://www.springframework.org/schema/beans
						   http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
						   http://www.springframework.org/schema/task 
					       http://www.springframework.org/schema/task/spring-task.xsd"
						   >
	
	<!-- Spring定时器注解开关-->  
    <task:annotation-driven />  
    
    <bean id="userTask" class="com.camelot.openplatform.usercenter.web.UserTask"/>
    
     <task:scheduled-tasks >  
        <task:scheduled ref="userTask" method="pringTask" cron="0/2 * * * * *"/>  
    </task:scheduled-tasks>  
</beans>

pojo
package com.camelot.openplatform.usercenter.web;

public class UserTask {

	public void pringTask(){
		System.out.println("task:scheduled-tasks-----------------tests");
	}
	
}

二、Quartz  (需要添加quartz-2.2.0.jar)

<?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"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
						   http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">
	
	<!-- 要调用的工作类 -->
   <bean id="userTask" class="com.camelot.openplatform.usercenter.web.UserTask"/>
    <!-- 定义调用对象和调用对象的方法 -->
    <bean id="userTaskDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
        <!-- 调用的类 -->
        <property name="targetObject">
            <ref bean="userTask"/>
        </property>
        <!-- 调用类中的方法 -->
        <property name="targetMethod">
            <value>quartzTask</value>
        </property>
    </bean>
    <!-- 定义触发时间 -->
    <bean id="userTaskTime" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
        <property name="jobDetail">
            <ref bean="userTaskDetail"/>
        </property>
        <!-- cron表达式 -->
        <property name="cronExpression">
            <value>0/5 * * * * ?</value>
        </property>
    </bean>
       
    <!-- 总管理类 如果将lazy-init='false'那么容器启动就会执行调度程序  -->

    <bean id="startQuertz" lazy-init="false" autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <property name="triggers">
            <list>
                <ref bean="userTaskTime"/>
            </list>
        </property>
    </bean>
	
</beans>

pojo
package com.camelot.openplatform.usercenter.web;

public class UserTask {

	public void quartzTask(){
		System.out.println("quartz-tasks-----------------tests");
	}
}

三、注解方式

xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:task="http://www.springframework.org/schema/task" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
						   http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
						   http://www.springframework.org/schema/task
						   http://www.springframework.org/schema/task/spring-task.xsd">
	<!-- pool-size="1-4",表示线程池活跃的线程数为1,最大线程数为4 -->
	<task:executor id="executor" pool-size="5" /> 
	<task:scheduler id="scheduler" pool-size="10" /> 
	<task:annotation-driven executor="executor" scheduler="scheduler" /> 
</beans>

pojo
package com.camelot.openplatform.usercenter.web;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component    
public class TaskJob {    
	@Scheduled(cron="0/5 * *  * * ? ")   //每5秒执行一次
    public void job1() {    
        System.out.println("TaskJob---任务进行中。。。\n");    
        System.out.println(Thread.currentThread().getName());
    }    
}   

注意,此种方式需要在主配置文件中加入,组件扫描,否则@comment扫描不到,定时任务不会执行
<context:component-scan base-package="com.camelot"/>



四、引用
方式一以及方式二,作者都是以 spring-task.xml文件单独存放便于管理 通常在 spring-context.xml 中引入
<!-- 定时任务 -->
	<import resource="spring/spring-task.xml" />

附加 如果需要通过profile 控制定时任务是否执行(添加  <beans profile="userTask">)
<beans profile="userTask">
    <bean id="startQuertz" lazy-init="false" autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <property name="triggers">
            <list>
                <ref bean="userTaskTime"/>
            </list>
        </property>
    </bean>
</beans>
同时 web.xml中需要添加
<context-param>
	  <param-name>spring.profiles.active</param-name>
	  <param-value>userTask</param-value>
  </context-param>


profile 的使用见:   http://stevex.blog.51cto.com/4300375/1348307/


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值