spring task 注解+xml的demo

来源:互联网 发布:淘宝评论违禁词有哪些 编辑:程序博客网 时间:2024/06/01 08:57
  1. @Service  
  2. @EnableScheduling  
  3. public class Annotation {  
  4.     @Scheduled(fixedDelay = 5000)  
  5.     public void fixedDelayTask() throws InterruptedException {  
  6.         System.out.println("anno 1,fixedDelay = 5000");  
  7.     }  
  8.   
  9.     @Scheduled(fixedRate = 6000)  
  10.     public void fixedRateTask() {  
  11.         System.out.println("anno 2,fixedRate = 6000");  
  12.     }  
  13.   
  14.     @Scheduled(fixedRate = 7000, initialDelay = 2000)  
  15.     public void fixedRateWithInitialDelayTask(){  
  16.         System.out.println("anno 3,fixedRate = 7000, initialDelay = 2000");  
  17.     }  
  18.   
  19.     @Scheduled(cron = "10 * * * * *")  
  20.     public void cronTask(){  
  21.         System.out.println("anno 4,cron = \"10 * * * * *\"");  
  22.     }  
  23.   
  24.     @Scheduled(cron = "${cron.expression}")  
  25.     public void useProperties()  
  26.     {  
  27.         System.out.println("Method executed at every 5 seconds. Current time is :: "new Date());  
  28.     }  
  29. }  



[java] view plain copy
  1. public class Xml {  
  2.     public void xmlFixedDelayTask() {  
  3.         System.out.println("xml1:  fixed delay");  
  4.     }  
  5.   
  6.     public void xmlFixedRateTask() {  
  7.         System.out.println("xml2: fixed rate");  
  8.     }  
  9.   
  10.     public void xmlCronTask() {  
  11.         System.out.println("xml3: cron schedule");  
  12.     }  
  13. }  

[java] view plain copy
  1. public class SpringScheduleDemo {  
  2.     public static void main(String[] args) {  
  3.         ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");  
  4.     }  
  5. }  



[html] view plain copy
  1. cron.expression=*/1 * * * * ?  

[html] view plain copy
  1. <beans xmlns="http://www.springframework.org/schema/beans"  
  2.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  3.        xmlns:context="http://www.springframework.org/schema/context"  
  4.        xmlns:task="http://www.springframework.org/schema/task"  
  5.        xsi:schemaLocation="http://www.springframework.org/schema/beans  
  6.     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd">  
  7.   
  8.     <context:component-scan base-package="com.mycuteblog.spring"/>  
  9.     <context:property-placeholder location="classpath:application.properties" />  
  10. <!--    <bean id="xmlScheduledTasks" class="com.mycuteblog.spring.schedule.Xml"/>  
  11.   
  12.     <task:scheduler id="taskScheduler" pool-size="10"/>  
  13.   
  14.     <task:scheduled-tasks>  
  15.         <task:scheduled ref="xmlScheduledTasks" method="xmlFixedDelayTask" fixed-delay="8000"/>  
  16.         <task:scheduled ref="xmlScheduledTasks" method="xmlFixedRateTask" fixed-rate="10000"/>  
  17.         <task:scheduled ref="xmlScheduledTasks" method="xmlCronTask" cron="15 * * * * *"/>  
  18.     </task:scheduled-tasks>-->  
  19.   
  20. </beans>  


[html] view plain copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"  
  3.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  
  5.     <modelVersion>4.0.0</modelVersion>  
  6.   
  7.     <groupId>com.mycuteblog</groupId>  
  8.     <artifactId>spring-schedule</artifactId>  
  9.     <version>1.0-SNAPSHOT</version>  
  10.   
  11.     <dependencies>  
  12.         <dependency>  
  13.             <groupId>org.springframework</groupId>  
  14.             <artifactId>spring-context</artifactId>  
  15.             <version>4.0.6.RELEASE</version>  
  16.         </dependency>  
  17.     </dependencies>  
  18.   
  19.     <!--configurations to run project standalone-->  
  20.     <build>  
  21.         <plugins>  
  22.             <plugin>  
  23.                 <groupId>org.apache.maven.plugins</groupId>  
  24.                 <artifactId>maven-jar-plugin</artifactId>  
  25.                 <version>2.4</version>  
  26.                 <configuration>  
  27.                     <archive>  
  28.                         <manifest>  
  29.                             <addClasspath>true</addClasspath>  
  30.                             <mainClass>com.mycuteblog.spring.schedule.SpringScheduleDemo</mainClass>  
  31.                             <classpathPrefix>dependency-jars/</classpathPrefix>  
  32.                         </manifest>  
  33.                     </archive>  
  34.                 </configuration>  
  35.             </plugin>  
  36.             <plugin>  
  37.                 <groupId>org.apache.maven.plugins</groupId>  
  38.                 <artifactId>maven-dependency-plugin</artifactId>  
  39.                 <version>2.5.1</version>  
  40.                 <executions>  
  41.                     <execution>  
  42.                         <id>copy-dependencies</id>  
  43.                         <phase>package</phase>  
  44.                         <goals>  
  45.                             <goal>copy-dependencies</goal>  
  46.                         </goals>  
  47.                         <configuration>  
  48.                             <outputDirectory>  
  49.                                 ${project.build.directory}/dependency-jars/  
  50.                             </outputDirectory>  
  51.                         </configuration>  
  52.                     </execution>  
  53.                 </executions>  
  54.             </plugin>  
  55.         </plugins>  
  56.     </build>  
  57.   
  58. </project>  
0 0
原创粉丝点击