在applicationContext.xml中添加:

xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-4.0.xsd"> <task:annotation-driven executor="myExecutor" scheduler="myScheduler" />
<task:executor id="myExecutor" pool-size="5" />
<task:scheduler id="myScheduler" pool-size="10" />

java代码:

@Component
public class CleanExpireTokenTask { private Logger logger = LoggerFactory.getLogger(LogTag.BUSINESS); @Scheduled(cron = "0 * * * * ?")
public void startUpdateSaleThread(){
try{
System.out.println("check token expire");
}catch(Exception e){
logger.error("Make salesReport faild",e);
}
}
}

注意:

实现类上要加注解@Component

定时器的任务方法不能有返回值

配置及启动报错问题参考自
 
2016-11-22日 安全关闭spring定时任务线程池
java代码

@Resource(name = "myScheduler")
private ThreadPoolTaskScheduler threadPoolTaskScheduler;

/**
* 等待正在执行的定时任务执行完毕,不再执行新的定时任务,
*/
public void shutdown(){
  threadPoolTaskScheduler.shutdown();
  // 等待任务执行完毕
  while(threadPoolTaskScheduler.getActiveCount() > 0){
    try {
      Thread.sleep(2000);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
  }
}

注:1.根据测试,通过此方式创建的定时任务,比如每分钟0秒执行任务,如果上一分钟0秒执行的任务还没执行完,则这次任务就不会启动。
  2.在ThreadPoolTaskScheduler调用shutdown方法后,不会杀掉正在执行的任务,已启动的定时任务可以执行完,这时如果定时任务执行Thread.sleep方法,会抛 InterruptedException,需要注意。
参考自

使用spring的@Scheduled注解执行定时任务,启动项目不输出警告的更多相关文章

  1. Spring 的@Scheduled注解实现定时任务运行和调度

    Spring 的@Scheduled注解实现定时任务运行和调度 首先要配置我们的spring.xml   ---  即spring的主配置文件(有的项目中叫做applicationContext.xm ...

  2. spring @Scheduled注解执行定时任务

    以前框架使用quartz框架执行定时调度问题. 这配置太麻烦.每个调度都需要多加在spring的配置中. 能不能减少配置的量从而提高开发效率. 最近看了看spring的 scheduled的使用注解的 ...

  3. quartz 框架定时任务,使用spring @Scheduled注解执行定时任务

    配置quartz 在spring中需要三个jar包: quartz-1.6.5.jar.commons-collections-3.2.jar.commons-logging-1.1.jar 首先要配 ...

  4. 使用spring @Scheduled注解执行定时任务

    以前框架使用quartz框架执行定时调度问题. 老大说这配置太麻烦.每个调度都需要多加在spring的配置中. 能不能减少配置的量从而提高开发效率. 最近看了看spring的 scheduled的使用 ...

  5. 【转】使用spring @Scheduled注解执行定时任务

    http://blog.csdn.net/sd4000784/article/details/7745947 以前框架使用quartz框架执行定时调度问题. 老大说这配置太麻烦.每个调度都需要多加在s ...

  6. 使用轻量级Spring @Scheduled注解执行定时任务

    WEB项目中需要加入一个定时执行任务,可以使用Quartz来实现,由于项目就一个定时任务,所以想简单点,不用去配置那些Quartz的配置文件,所以就采用了Spring @Scheduled注解来实现了 ...

  7. 使用spring @Scheduled注解执行定时任务、

    http://blog.csdn.net/sd4000784/article/details/7745947,留下来备用.

  8. Spring Boot 使用 @Scheduled 注解创建定时任务

    在项目开发中我们经常需要一些定时任务来处理一些特殊的任务,比如定时检查订单的状态.定时同步数据等等. 在 Spring Boot 中使用 @Scheduled 注解创建定时任务非常简单,只需要两步操作 ...

  9. 使用spring @Scheduled注解运行定时任务、

    曾经框架使用quartz框架运行定时调度问题. 老大说这配置太麻烦.每一个调度都须要多加在spring的配置中. 能不能降低配置的量从而提高开发效率. 近期看了看spring的 scheduled的使 ...

随机推荐

  1. 读书笔记-JavaScript面向对象编程(一)

    PDF下载链接: http://pan.baidu.com/s/1eSDSTVW 密码: 75jr 第1章 引言 1.1 回顾历史 1.2 变革之风 1.3 分析现状 1.4 展望未来 1.5 面向对 ...

  2. git 实用技巧

    一.git 常用操作 1.1 // 该方法会显示某次提交的所有更改 git log --pretty=oneline 文件名 git show 356f6def9d3fb7f3b9032ff5aa4b ...

  3. Android下LayoutInflater的使用

    在我们想XML布局文件转换为View对象的时候.我们都会使用LayoutInflate对象.顾名思义咋一眼就能看出来他是布局填充器.那么接下来看看LayoutInfalte的使用 总体分为 Layou ...

  4. python界面

    import easygui as g import sys while 1: g.msgbox("我一定要学会编程!","加油!") #choices = [ ...

  5. protocol http not supported or disabled in libcurl apt-get

    ubuntu 14.04 碰到了这个莫名其妙的问题.谷歌了一把,解决方案如下:http://askubuntu.com/questions/683857/curl-1-protocol-https-n ...

  6. 《PHP字符串函数》笔记

    ① str_repeat() 重复一个字符串; ② str_shuffle() 随机打乱一个字符串; ③ strftime() 根据区域设置格式化本地时间/日期; ④ strip_tags() 从字符 ...

  7. WebService -- Java 实现之 CXF ( 使用Spring添加拦截器)

    最重要的就是在ApplicationContext.xml下面添加配置 <!-- service provider --> <jaxws:endpoint implementor=& ...

  8. inq to datatable group by 多列 实现

    using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.T ...

  9. WPF去边框与webbrowser的冲突

    首先建一个类,比如NativeMethods.cs class NativeMethods{     public const int WS_CAPTION=0x00C0000;     public ...

  10. tomcat端口被占用问题完美解决方案!

    启动Tomcat服务器报错:Several ports (8005, 8080, 8009) required by Tomcat v7.0 Server at localhost are alrea ...