Spring 定时执行任务

时间:2023-03-09 06:49:48
Spring 定时执行任务

好不容易写了半天的文章竟然由于断网而丢失了,并未自动保存到草稿箱。只能简单的贴贴代码了。

<?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:util="http://www.springframework.org/schema/util"
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/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd"> <context:annotation-config />
<!-- spring扫描注解的配置 -->
<context:component-scan base-package="com.dong.schedule" /> <!--此处如果不配置的话,导致只有一个线程执行任务。 -->
<task:annotation-driven executor="myExecutor" scheduler="myScheduler"/>
<task:executor id="myExecutor" pool-size="5"/>
<task:scheduler id="myScheduler" pool-size="10"/>
</beans>

  

package com.dong.schedule;

import java.util.Date;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component; @Component
public class ScheduleTask { @Scheduled(cron="*/5 * * * * ?")
public void demoServiceMethod()
{
System.out.println("ScheduleTask "+Thread.currentThread()+" Method executed at every 5 seconds. Current time is :: "+ new Date());
} @Scheduled(cron="*/3 * * * * ?")
public void sayHi()
{
System.out.println("ScheduleTask "+Thread.currentThread()+" Method executed at every 3 seconds. Current time is :: "+ new Date());
}
}

  

package com.dong.schedule;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.stereotype.Component; /***
* 代码中动态设置cron表达式的方式。
*/
@Component
public class MyTask {
@Autowired
private ThreadPoolTaskScheduler myScheduler; public void run(){
//此处可以动态设置
myScheduler.schedule(new MySchedulerTask(), new CronTrigger("*/2 * * * * ?"));
} private class MySchedulerTask implements Runnable{ @Override
public void run() {
// TODO Auto-generated method stub
System.out.println("MyTask"+Thread.currentThread()+" Method executed at every 2 seconds. Current time is :: "+ new Date());
} } }

  

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main {
public static void main(String[] args) {
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
MyTask myTask = (MyTask) context.getBean("myTask");
myTask.run();
}
}

  参考的资料 http://docs.spring.io/spring/docs/current/spring-framework-reference/html/scheduling.html

  http://howtodoinjava.com/spring/spring-core/4-ways-to-schedule-tasks-in-spring-3-scheduled-example/

  http://blog.****.net/lmj623565791/article/details/27109467