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

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:task="http://www.springframework.org/schema/task"
  5. xmlns:util="http://www.springframework.org/schema/util"
  6. xmlns:context="http://www.springframework.org/schema/context"
  7. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  8. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
  9. http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
  10. http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd">
  11.  
  12. <context:annotation-config />
  13. <!-- spring扫描注解的配置 -->
  14. <context:component-scan base-package="com.dong.schedule" />
  15.  
  16. <!--此处如果不配置的话,导致只有一个线程执行任务。 -->
  17. <task:annotation-driven executor="myExecutor" scheduler="myScheduler"/>
  18. <task:executor id="myExecutor" pool-size="5"/>
  19. <task:scheduler id="myScheduler" pool-size="10"/>
  20. </beans>

  

  1. package com.dong.schedule;
  2.  
  3. import java.util.Date;
  4.  
  5. import org.springframework.scheduling.annotation.Scheduled;
  6. import org.springframework.stereotype.Component;
  7.  
  8. @Component
  9. public class ScheduleTask {
  10.  
  11. @Scheduled(cron="*/5 * * * * ?")
  12. public void demoServiceMethod()
  13. {
  14. System.out.println("ScheduleTask "+Thread.currentThread()+" Method executed at every 5 seconds. Current time is :: "+ new Date());
  15. }
  16.  
  17. @Scheduled(cron="*/3 * * * * ?")
  18. public void sayHi()
  19. {
  20. System.out.println("ScheduleTask "+Thread.currentThread()+" Method executed at every 3 seconds. Current time is :: "+ new Date());
  21. }
  22. }

  

  1. package com.dong.schedule;
  2.  
  3. import java.util.Date;
  4.  
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
  7. import org.springframework.scheduling.support.CronTrigger;
  8. import org.springframework.stereotype.Component;
  9.  
  10. /***
  11. * 代码中动态设置cron表达式的方式。
  12. */
  13. @Component
  14. public class MyTask {
  15. @Autowired
  16. private ThreadPoolTaskScheduler myScheduler;
  17.  
  18. public void run(){
  19. //此处可以动态设置
  20. myScheduler.schedule(new MySchedulerTask(), new CronTrigger("*/2 * * * * ?"));
  21. }
  22.  
  23. private class MySchedulerTask implements Runnable{
  24.  
  25. @Override
  26. public void run() {
  27. // TODO Auto-generated method stub
  28. System.out.println("MyTask"+Thread.currentThread()+" Method executed at every 2 seconds. Current time is :: "+ new Date());
  29. }
  30.  
  31. }
  32.  
  33. }

  

  1. import org.springframework.context.ApplicationContext;
  2. import org.springframework.context.support.ClassPathXmlApplicationContext;
  3.  
  4. public class Main {
  5. public static void main(String[] args) {
  6. ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
  7. MyTask myTask = (MyTask) context.getBean("myTask");
  8. myTask.run();
  9. }
  10. }

  参考的资料 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

Spring 定时执行任务的更多相关文章

  1. Spring 定时执行任务重复执行多次

    使用spring的定时任务组件的时候,代码如下. @Scheduled(cron="0 5/5 * * * ?") public void sendWeatherSMS() { S ...

  2. Spring+quartz集群配置,Spring定时任务集群,quartz定时任务集群

    Spring+quartz集群配置,Spring定时任务集群,quartz定时任务集群 >>>>>>>>>>>>>> ...

  3. Spring定时服务QuartZ

    在JavaEE系统中,我们会经常用到定时任务,比如每天凌晨生成前天报表,每一小时生成汇总数据等等. 我们可以使用java.util.Timer结合java.util.TimerTask来完成这项工作, ...

  4. elastic-job 分布式定时任务框架 在 SpringBoot 中如何使用(一)初始化任务并定时执行

    第一篇需要实现一个最简单的需求:某个任务定时执行,多台机子只让其中一台机子执行任务 一.安装 分布式应用程序协调服务 zookeeper,安装步骤在链接里面 Linux(Centos7)下安装 zoo ...

  5. Spring 定时操作业务需求

    1.定时分析 在业务需求中有的需要检测用户的状态,通过对用户状态的检测做出对此状态相应的操作,如果这种检测由运营人工检测,不仅工作量大,而且准确性不高,人工无法很好的完成工作: 问题根源:在检测用户状 ...

  6. Spring定时(任务)刷新-quartz

    Quartz是一个完全由java编写的开源作业调度框架.他可以与J2EE.J2SE集成,用与处理定时任务.定时刷新的需求.此处使用为与Spring项目集成. 在SpringMVC项目中使用quartz ...

  7. SpringBoot中使用@scheduled定时执行任务需要注意的坑

    spring boot: 计划任务@ EnableScheduling和@Scheduled @Scheduled中的参数说明 @Scheduled(fixedRate=2000):上一次开始执行时间 ...

  8. 使用oracle 的 PL/Sql 定时执行一个存储过程

    CSDN日报20170322--<关于软件研发的一些体会总结> 同步博客至 CSDN ,让更多开发者看到你的文章 看微博技术大咖解析互联网应用架构实战 使用oracle 的 PL/Sql ...

  9. C#定时执行

    代码: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; ...

随机推荐

  1. zoj3551 Bloodsucker ——概率DP

    Link: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4530 A[i]数组表示当吸血鬼有 I 个的时候,还需要的天数.可以 ...

  2. 有向图强连通分量 Tarjan算法

    [有向图强连通分量] 在有向图G中,如果两个顶点间至少存在一条路径,称两个顶点强连通(strongly connected).如果有向图G的每两个顶点都强连通,称G是一个强连通图.非强连通图有向图的极 ...

  3. Vmvare下Ubuntu安装Python3.4

    Ubuntu14.4下默认安装的Python版本是2.7.随着Python3.4的使用,现在大部分Python开发者都喜欢使用Py3.4.那么Ubuntu下应该怎么安装Python3.4呢? (1). ...

  4. PicPopupWindow的使用

    Github地址https://github.com/lujianfeiccie/android_picpopup_window 效果图1: 效果图2:

  5. php-fpm 老是warning 进程退出问题

    http://yangjunwei.com/a/723.html 分析Centos系统下LNMP频繁502 Bad Gateway问题 2012-01-28 杨俊伟 )     最近VPS总是出现 N ...

  6. [Eclipse] - Unicode properties editor

    在properpties文件中使用中文,需要将文件转成unicode. eclipse安装插件:PropertiesEditor 下载地址: http://propedit.sourceforge.j ...

  7. 大师教你<部落冲突>如何切换账号

    前提申请两个谷歌账号,账号一和账号二,想要切换账号,只需清除部落冲突在手机上的数据即可.详情请看下文! 1. 第一次登陆,进入游戏后 2. 没有谷歌商店的童鞋,下载谷歌安装器(一键修复)以及VPNFQ ...

  8. mac idea 设置

    鼠标悬停显示文档注释:preferences->Editor->General:勾选 show quick documentation on mouse move 智能提示模糊搜索:pre ...

  9. C#禁止程序重复启动

    采用线程互斥锁Mutex,在winform程序的主入口点中加入如下代码,将程序改为单实例运行. static class Program { /// <summary> /// 应用程序的 ...

  10. 大型博彩公司招聘 .net,DB,tester,android

    大型博彩公司招聘 .net,DB,tester,android,ios等. 等拿完年终奖的朋友,可以先发简历给我,先面试,年后上班. emai:sjchen1203@126.com 要求: 1. 全职 ...