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
Spring 定时执行任务的更多相关文章
- Spring 定时执行任务重复执行多次
使用spring的定时任务组件的时候,代码如下. @Scheduled(cron="0 5/5 * * * ?") public void sendWeatherSMS() { S ...
- Spring+quartz集群配置,Spring定时任务集群,quartz定时任务集群
Spring+quartz集群配置,Spring定时任务集群,quartz定时任务集群 >>>>>>>>>>>>>> ...
- Spring定时服务QuartZ
在JavaEE系统中,我们会经常用到定时任务,比如每天凌晨生成前天报表,每一小时生成汇总数据等等. 我们可以使用java.util.Timer结合java.util.TimerTask来完成这项工作, ...
- elastic-job 分布式定时任务框架 在 SpringBoot 中如何使用(一)初始化任务并定时执行
第一篇需要实现一个最简单的需求:某个任务定时执行,多台机子只让其中一台机子执行任务 一.安装 分布式应用程序协调服务 zookeeper,安装步骤在链接里面 Linux(Centos7)下安装 zoo ...
- Spring 定时操作业务需求
1.定时分析 在业务需求中有的需要检测用户的状态,通过对用户状态的检测做出对此状态相应的操作,如果这种检测由运营人工检测,不仅工作量大,而且准确性不高,人工无法很好的完成工作: 问题根源:在检测用户状 ...
- Spring定时(任务)刷新-quartz
Quartz是一个完全由java编写的开源作业调度框架.他可以与J2EE.J2SE集成,用与处理定时任务.定时刷新的需求.此处使用为与Spring项目集成. 在SpringMVC项目中使用quartz ...
- SpringBoot中使用@scheduled定时执行任务需要注意的坑
spring boot: 计划任务@ EnableScheduling和@Scheduled @Scheduled中的参数说明 @Scheduled(fixedRate=2000):上一次开始执行时间 ...
- 使用oracle 的 PL/Sql 定时执行一个存储过程
CSDN日报20170322--<关于软件研发的一些体会总结> 同步博客至 CSDN ,让更多开发者看到你的文章 看微博技术大咖解析互联网应用架构实战 使用oracle 的 PL/Sql ...
- C#定时执行
代码: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; ...
随机推荐
- python语言switch-case
初学python语言,竟然很久才发现python没有switch-case语句,查看官方文档说是可以用if-elseif-elseif....代替. 讲真,这都不是问题.不就是一个条件判断吗.用if- ...
- python 元组操作
关于元组的常用操作,请参考:http://www.runoob.com/python/python-tuples.html 元组的元素不可修改 ,元组的元素的元素可修改 count(self,valu ...
- systemtap
http://www.ibm.com/developerworks/library/l-systemtap/index.html http://wiki.eclipse.org/Linux_Tools ...
- php递归函数--遍历
多应用在 栏目多级.无限极调用 数据库数据如: id catname pid 1 关于我们 0 2 新闻 0 3 公司新闻 2 4 ...
- Python numpy函数hstack() vstack() stack() dstack() vsplit() concatenate()
感觉numpy.hstack()和numpy.column_stack()函数略有相似,numpy.vstack()与numpy.row_stack()函数也是挺像的. stackoverflow上也 ...
- Firebug在Firefox DevTools 中复活
英文:Firefox,编译:开源中国 链接:www.oschina.net/news/80230/firebug-lives-on-in-firefox-devtools 技术最前线转注:2016年1 ...
- 【mysql元数据库】使用information_schema.tables查询数据库和数据表信息
概述 对于mysql和Infobright等数据库,information_schema数据库中的表都是只读的,不能进行更新.删除和插入等操作,也不能加触发器,因为它们实际只是一个视图,不是基本表,没 ...
- 使用新浪云(SAE)实现基于mySql和微信公众平台的关键字请求响应服务
本例是作者初次尝试微信公众平台开发之作,实现传统的关键字请求响应功能.即:用户发关键字,公众号通过关键字进行检索, 在mysql数据库中读取与关键字相关的信息,并返回给用户.本例在微信订阅号(开发者模 ...
- OAF_文件系列4_实现OAF上传显示数据库动态图片Image(案例)
20150805 Created By BaoXinjian
- 电源开关IC
RT9701:IO控制的电源开关.宽输入电压(2.2~6v),1.1A的连续输出电流.用在USB开关电压,热插拔和电池电池充电器的场合应用