This article mainly introduces the detailed explanation of using annotations (@Scheduled) to create scheduled tasks in Spring 3. It has certain reference value. Interested friends can refer to it.
The use of annotations has been strengthened in Spring 3, and scheduled tasks have also been enhanced. Now creating a scheduled task only requires two steps:
Create a Java class, add a method with no parameters and no return value, and modify it with the @Scheduled annotation;
Add three
Finally, the Java class created in the first step must become a spring-manageable bean. It can be written directly in XML or @Component The
example is as follows
Scheduled task class:
/** * com.zywang.spring.task.SpringTaskDemo.java * @author ZYWANG 2011-3-9 */ package com.zywang.spring.task; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; /** * Spring3 @Scheduled 演示 * @author ZYWANG 2011-3-9 */ @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!"); } }
Spring configuration file:
<?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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd"> <!-- Enables the Spring Task @Scheduled programming model --> <task:executor id="executor" pool-size="5" /> <task:scheduler id="scheduler" pool-size="10" /> <task:annotation-driven executor="executor" scheduler="scheduler" /> </beans>
The above is the detailed content of Detailed explanation of examples of using annotations (@Scheduled) to create scheduled tasks in Spring 3. For more information, please follow other related articles on the PHP Chinese website!