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

<?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

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. Java笔记4-do while循环,break,修饰符,方法的调用

    do while循环语法:do{ //循环体}while(条件表达式); 注:它是先执行循环体,后再判断的循环结构. 如:int i = 0;do{ System.out.println(" ...

  2. Oracle学习系列7

    Oracle学习系列7 ************************************************************************************ 关联表 ...

  3. Spring源码学习之:ClassLoader学习(1)

    转载:http://longdick.iteye.com/blog/442213 java应用环境中不同的class分别由不同的ClassLoader负责加载. 一个jvm中默认的classloade ...

  4. JSBinding+SharpKit / 脚本加密(JSC或Bytecode,参考cocos2d-js)

    现在已经可以编译JSC,目前只能在 Windows 下编译 JSC.这个功能是从 cocos2d-js 抄过来的,他应该也支持在Mac编译,但是我没有试过. 菜单:JSB | Compile all ...

  5. Flume1.5.0的安装、部署、简单应用(含伪分布式、与hadoop2.2.0、hbase0.96的案例)

    目录: 一.什么是Flume? 1)flume的特点 2)flume的可靠性 3)flume的可恢复性 4)flume 的 一些核心概念 二.flume的官方网站在哪里? 三.在哪里下载? 四.如何安 ...

  6. c#检测端口是否被占用

    当我们要创建一个Tcp/Ip Server connection ,我们需要一个范围在1000到65535之间的端口 . 但是本机一个端口只能一个程序监听,所以我们进行本地监听的时候需要检测端口是否被 ...

  7. docker跨容器之使用link大法通信

    容器1 docker run --name elixir -it edib/elixir-phoenix-dev /bin/bash ip address看看自己的ip 容器2 docker run ...

  8. Asp.net MVC 之过滤器

    整理一下MVC中的几种过滤器,以及每种过滤器是干什么用的 四种过滤器 1.AuthorizationFilter(授权过滤器) 2.ActionFilter(方法过滤器) 3.ResultFilter ...

  9. ISO 14229 简介 转载

    作者:autogeek 原文链接:http://www.cnblogs.com/autogeek/p/4458591.html 前言 由于工作中经常用到ISO-14229,因此决定对该协议做个总体介绍 ...

  10. 【javascript基础】1、基本概念

    前言 最近迷茫了一段时间,不知道应该从何处开始学习前端知识,好像这种状态已经持续了一年了,天天也在看前端的东西,但是记住的多少或者说在脑中一团糟,没有什么清晰的概念.最近加入了jQuery源码交流群( ...