Create a scheduled task source using annotations (@Scheduled) in Spring3

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!");
}

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.