Spring+Schedule(定时任务)小示例

本篇文章简单介绍一个Spring的schedule小例子。此定时任务放在一个mvc工程里面,配置以及代码都可以参考 http://fengyilin.iteye.com/admin/blogs/2338830这篇文章,下面主要把关于schedule的内容记录下来。

变更点:
1.在servlet-context.xml文件中引入schedule的配置文件

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

	<!-- DispatcherServlet Context: defines this servlet's request-processing 
		infrastructure -->

	<!-- Enables the Spring MVC @Controller programming model -->
	<annotation-driven>

	</annotation-driven>

	<!-- Handles HTTP GET requests for /resources/** by efficiently serving 
		up static resources in the ${webappRoot}/resources/ directory -->
	<resources mapping="/resources/**" location="/resources/" />
	<!-- Resolves views selected for rendering by @Controllers to .jsp resources 
		in the /WEB-INF/views directory -->
<!-- 	<beans:bean -->
<!-- 		class="org.springframework.web.servlet.view.InternalResourceViewResolver"> -->
<!-- 		<beans:property name="prefix" value="/WEB-INF/views/" /> -->
<!-- 		<beans:property name="suffix" value=".jsp" /> -->
<!-- 	</beans:bean> -->
	<beans:bean id="templateResolver"
		class="org.thymeleaf.spring4.templateresolver.SpringResourceTemplateResolver">
		<beans:property name="prefix" value="/WEB-INF/views/" />
		<beans:property name="suffix" value=".html" />
		<!-- Template cache is true by default. Set to false if you want -->
		<!-- templates to be automatically updated when modified. -->
		<beans:property name="cacheable" value="false" />
	</beans:bean>
	<!-- SpringTemplateEngine automatically applies SpringStandardDialect and -->
	<!-- enables Spring's own MessageSource message resolution mechanisms. -->
	<beans:bean id="templateEngine" class="org.thymeleaf.spring4.SpringTemplateEngine">
		<beans:property name="templateResolver" ref="templateResolver" />
	</beans:bean>
	
	<beans:bean class="org.thymeleaf.spring4.view.ThymeleafViewResolver">
        <beans:property name="templateEngine" ref="templateEngine" />
    </beans:bean>
	
	<!-- Imports user-defined @Controller beans that process client requests -->
	<beans:import resource="controllers.xml" />
	
    <!-- Imports user-defined @Scheduled beans that process schedule tasks -->
    <beans:import resource="schedules.xml" />

</beans:beans>


                                             
在此文件的最后一行引入了schedules.xml
2.追加schedules.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:task="http://www.springframework.org/schema/task"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    <!--扫描schedule类package-->
    <context:component-scan base-package="test.spring.schedule"/>
     <!--使用spring scheduled的核心配置,启动scheduled注解功能-->
    <task:annotation-driven />
</beans>                                         

                                             
3.追加具体执行任务的类
  /**
* 
*/
package test.spring.schedule;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * @author dwxx-116
 *
 */
@Component
public class MySchedule {
    private static final Log logger = LogFactory.getLog(MySchedule.class);
    @Scheduled(fixedRate=2000)
    public void processQueues() {
        logger.info("start to processQueues,time:" + System.currentTimeMillis());
    }
}



本示例只是简单的做了测试,定时任务每2秒钟重复执行一次,当然spring Scheduled通过cron表达式支持更加灵活的调度配置。

4.追加依赖jar包aopalliance-1.0.jar

最终的工程目录



完整的工程代码请参考附件

猜你喜欢

转载自fengyilin.iteye.com/blog/2338990