Spring定时任务执行
备注:这个是基于搭建好spring的环境下的
注解方式:
1.定时任务类
package com.test;
import java.util.Date;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@Component("mytask")
public class MyTask {
int i =0;
@Scheduled(cron = "0 0/1 * * * ?")//!!!!!表示每分钟执行一次!!!!!cron是表示触发的时间表达式
public void doTask(){
System.out.println("结果是:"+(i++)+":"+new Date());
}
}
2.配置文件:schedule.xml;备注:名称随便起。还有,需要在总的配置文件映入改文件
<?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:context="http://www.springframework.org/schema/context"
xmlns:task="http://www.springframework.org/schema/task"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"
xmlns:aop="http://www.springframework.org/schema/aop"> <context:annotation-config />
<!-- 扫描定时任务类所在的包 -->
<context:component-scan base-package="com.test" /> </beans>
XML方式:
1.定时任务类:相比注解方式,方法上面没有注解,通过XML方式注入
@Component("mytask")
public class MyTask {
int i =0;
public void doTask(){
System.out.println("结果是:"+(i++)+":"+new Date());
}
}
2.配置文件:schedule.x
<?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:context="http://www.springframework.org/schema/context"
xmlns:task="http://www.springframework.org/schema/task"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"
xmlns:aop="http://www.springframework.org/schema/aop"> <!-- <context:annotation-config /> -->
<!-- 扫描定时任务类所在的包 -->
<task:scheduled-tasks>
<task:scheduled ref="cancelSysSeatByTask" method="doTask" cron="0 0/1 * * * ?"/>
</task:scheduled-tasks> </beans>
3.部署到tomcat并且启动,看看后台是否输出以下信息:
结果是:0:Mon Mar 30 16:23:00 CST 2015
结果是:1:Mon Mar 30 16:24:00 CST 2015
结果是:2:Mon Mar 30 16:25:00 CST 2015
Spring定时任务执行的更多相关文章
- Spring整合Quartz定时任务执行2次,Spring定时任务执行2次
Spring整合Quartz定时任务执行2次,Spring定时任务执行2次 >>>>>>>>>>>>>>>&g ...
- 解决spring定时任务执行2次和tomcat部署缓慢的问题
spring定时任务执行2次 问题重现和解析 最近使用quartz定时任务框架,结果发现开发环境执行无任何问题,部署到服务器上后,发现同一时间任务执行了多次.经过搜索发现是服务器上tomcat的配置文 ...
- spring定时任务执行两次的原因与解决方法
spring定时任务,本地执行一次,放到服务器上后,每次执行时会执行两次,原因及解决办法. http://blog.csdn.net/yaobengen/article/details/7031266 ...
- spring 定时任务执行2次
eclipse 上定时任务执行没有问题,生产环境可以看到定时任务同时执行了2次,排除代码原因,网上找了些资料,最后发现是tomcat的原因, Host 节点中有一个appBase 属性指向了webap ...
- spring 定时任务执行两次解决办法
在web.xml中同时配置了ContextLoaderListener和DispatcherServlet?假如真是这样的话,需要删掉一个配置,因为你相当于配置了两个spring容器,两个容器分别都执 ...
- spring定时任务执行两次
最近用Spring的quartz定时器的时候,发现到时间后,任务总是重复执行两次,在tomcat或jboss下都如此. 打印出他们的hashcode,发现是不一样的,也就是说,在web容器启动的时候, ...
- spring定时任务执行两次 项目重复初始化 项目启动两次
tomcat/config/server.xml中Host标签Context节点的问题 项目里quartz定时器总是被执行2次,通过打印发现原来项目被加载了两次,所以项目下的Listener被重复加载 ...
- 动态改变Spring定时任务执行频率
@Component@EnableSchedulingpublic class updateCronTask implements SchedulingConfigurer { public stat ...
- Tomcat启动后加载两次web.xml的问题(因为spring定时任务执行了俩次,引出此问题)
http://www.linuxidc.com/Linux/2011-07/38779.htmhttp://jingyan.baidu.com/article/48206aeaf9422e216ad6 ...
随机推荐
- GetHashCode()
[GetHashCode] GetHashCode 方法的默认实现不保证针对不同的对象返回唯一值.而且,.NET Framework 不保证 GetHashCode 方法的默认实现以及它所返回的值在不 ...
- Windows Server 2012 R2 Standard x64 deploy Visual Studio 2015 Application
When I run the Server application on Windows Server 2012 R2 operation system. I meet the error:MSVCP ...
- [SoapUI] Loop a test with different sets of data
- Linux虚拟机安装配置手册(版本:7.3)
一.准备 ①.安装VMWare 安装步骤省略.我这里使用的版本如下: ②.Linux系统镜像文件: 建议使用最新版本的Linux系统.有些组件对系统内核版本要求比较高,有可能会产生错误. 我这里使用的 ...
- VMware联网问题
VMware 服务启动优化当虚拟机连不上网,或不显示ip时,先查看vm服务是否启动.控制台运行:services.msc 以下为改为手工启动:1.VMware 服务自动启动影响计算机启动速度,再此对V ...
- html传参数 js工具类
var QueryUtils = { GetQueryString: function (name) { var reg = new RegExp("(^|&)" + na ...
- Visual Studio 2015 开发 ASP.NET 5 有何变化?(转)
出处:http://www.cnblogs.com/xishuai/p/visual-studio-2015-preview-asp-net-5-change.html 本篇博文目录: ASP.NET ...
- [GO]可见性
GO的可见性:如果想使用别的包的函数.结构体类型.结构体成员 函数名.结构体类型.结构体成员变量名的首字母必须是大写,则为可见,反之,则只能在一个包里使用 比如本来就有一个项目叫awesomeproj ...
- qt下的跨目录多工程编译(转)
这里要编译的工程包含一个库和一个可执行文件.可执行文件依赖于库,所以要先编译库,编译后库放在lib目录里面,可执行文件放在bin目录里面. 目录结构如下: 全局的工程文件complex.pro在工程根 ...
- java Random类和Math.Rondom
Java中存在着两种Random函数: 一.java.lang.Math.Random; 调用这个Math.Random()函数能够返回带正号的double值,该值大于等于0.0且小于1.0,即取 ...