Create a scheduled task source using annotations (@Scheduled) in Spring3
Last Update:2018-07-19
Source: Internet
Author: User
Web.xml configuration:
<?xml version= "1.0" encoding= "UTF-8"?>
<web-app version= "3.0"
Xmlns= "Http://java.sun.com/xml/ns/javaee"
Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"
Xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee
Http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd ">
<display-name></display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/web-inf/classes/applicationcontext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
Applicationcontext.xml configuration:
<?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:p= "http://www.springframework.org/schema/p"
xmlns:context= "Http://www.springframework.org/schema/context"
xmlns:task= "Http://www.springframework.org/schema/task"
xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans-3.1.xsd
Http://www.springframework.org/schema/context
Http://www.springframework.org/schema/context/spring-context-2.5.xsd
Http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd "
>
<context:component-scan base-package= "Com.zywang.spring.task"/>
<task:executor id= "Executor" pool-size= "5"/>
<task:scheduler id= "Scheduler" pool-size= "ten"/>
<task:annotation-driven executor= "Executor" scheduler= "Scheduler"/>
</beans>
Java code:
Package com.zywang.spring.task;
Import org.springframework.scheduling.annotation.Scheduled;
Import org.springframework.stereotype.Component;
Import Org.springframework.stereotype.Service;
@Component
public class Springtaskdemo {
@Scheduled (Fixeddelay = 5000)
void Dosomethingwithdelay () {
System.out.println ("I ' m doing with delay now!");
}
@Scheduled (fixedrate = 5000)
void Dosomethingwithrate () {
System.out.println ("I ' m doing with rate now!");
}
@Scheduled (cron = "0/5 * * * *")
void Dosomethingwith () {
System.out.println ("I ' m doing with cron now!");
}
}