zoukankan      html  css  js  c++  java
  • 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.csdn.net/lmj623565791/article/details/27109467

  • 相关阅读:
    沟通是项目管理知识体系中的九大知识领域之一
    项目管理的三要素时间、成本、质量
    项目管理提升效率的几大关键点
    收到FRDMKL02Z
    【转】arm 开发工具比较(ADS vs RealviewMDK vs RVDS)
    你不能自己把自己放弃写在毕业季
    【转】为什么你应该(从现在开始就)写博客
    Vivado 2014.4 FFT IP 使用及仿真
    项目需求的一些事
    娇荣电子工作室成立了
  • 原文地址:https://www.cnblogs.com/dongqiSilent/p/5170324.html
Copyright © 2011-2022 走看看