spring3.0定时器
来源:互联网 发布:js base64解码图片 编辑:程序博客网 时间:2024/04/29 09:57
- public class TestService {
- private Logger logger = LoggerFactory.getLogger(TestService.class);
- public void sayHello() {
- System.out.println("hello world");
- }
- public void sayBye() {
- System.out.println("bye world");
- }
- }
- public class TestService {
- private Logger logger = LoggerFactory.getLogger(TestService.class);
- public void sayHello() {
- System.out.println("hello world");
- }
- public void sayBye() {
- System.out.println("bye world");
- }
- }
然后配置文件也更简单
- <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"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans.xsd
- http://www.springframework.org/schema/task
- http://www.springframework.org/schema/task/spring-task-3.0.xsd">
- <bean id="testService" class="com.xxx.spring.business.TestService" />
- <task:scheduled-tasks>
- <task:scheduled ref="testService" method="sayHello" cron="3/11 * * * * ?" />
- <task:scheduled ref="testService" method="sayBye" cron="7/13 * * * * ?" />
- </task:scheduled-tasks>
- </beans>
- <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"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans.xsd
- http://www.springframework.org/schema/task
- http://www.springframework.org/schema/task/spring-task-3.0.xsd">
- <bean id="testService" class="com.xxx.spring.business.TestService" />
- <task:scheduled-tasks>
- <task:scheduled ref="testService" method="sayHello" cron="3/11 * * * * ?" />
- <task:scheduled ref="testService" method="sayBye" cron="7/13 * * * * ?" />
- </task:scheduled-tasks>
- </beans>
只要用一个新增的<task:scheduled-tasks>就可以了
就是有一点要注意一下,新的时间配置,是类似于cron的语法,比以前强大很多。
不过我只用到了第一个参数:3/11,表示延迟3秒启动,间隔11秒;7/13表示延迟7秒启动,间隔13秒
于注解的sping3定时器配置
1.首先要在application-context.xml里面配置好namespace 和schema,如下:
xmlns:task="http://www.springframework.org/schema/task"
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.0.xsd
2.在application-context.xml里面配置<task:annotation-driven/>,加下面一行就行:
<task:annotation-driven/>
<!-- The <task:annotation-driven/> element sets Spring up to automatically support
scheduled and asynchronous methods. These methods are identified with the
@Scheduled and @Async methods, respectively -->
3.在Bean里面的调度方法加注解@Scheduled,其中@Scheduled的attribute有三种:
(1)fixedRate:每隔多少毫秒执行一次该方法。如:
- @Scheduled(fixedRate=2000)
- public void scheduleMethod(){
- System.out.println("Hello world...");
- }
(2)fixedDelay:当一次方法执行完毕之后,延迟多少毫秒再执行该方法。
(3)cron:详细配置了该方法在什么时候执行。cron值是一个cron表达式。如:
- @Scheduled(cron="0 0 0 * * SAT")
- public voidarchiveOldSpittles(){
- // ...
- }
The value given to the cron attribute is a Cron expression. For those who aren’t so
well-versed in Cron expressions, let’s break down the cron attribute. The Cron expres-
sion is made up of six (or possibly seven) time elements, separated by spaces. In order
from left to right, the elements are defined as follows:
1 Seconds (0-59)
2 Minutes (0-59)
3 Hours (0-23)
4 Day of month (1-31)
5 Month (1-12 or JAN-DEC)
6 Day of week (1-7 or SUN-SAT)
7 Year (1970-2099)
一些cron表达式的例子:
Cron expression What it means
0 0 10,14,16 * * ? Every day at 10 a.m., 2 p.m., and 4 p.m.
0 0,15,30,45 * 1-30 * ? Every 15 minutes on the first 30 days of the month
30 0 0 1 1 ? 2012 30 seconds after midnight on January 1, 2012
0 0 8-17 ? * MON-FRI Every working hour of every business day
- spring3.0定时器
- spring 定时器(spring3.0)
- Spring3.0 定时器scheduled案例
- Spring3定时器
- spring3 定时器 时间配置
- spring3.x 定时器
- 三步搞定Spring3.0 quartz 定时器执行定时任务
- spring3.0定时器 xml配置和注解方式
- spring3.0基于注解的定时器以及执行两次的解决办法
- Spring3.1.1 quartz1.8.6 定时器配置
- Spring3.1.1 quartz1.8.6 定时器配置
- spring3.2 定时器 SchedulerFactoryBean 注入错误
- spring3.0
- Spring3.0
- 关于spring3定时器cron参数设置每月最后一天不支持L
- Spring3中定时器的配置使用之Quartz
- Spring3中定时器的配置使用之Quartz
- Spring3中定时器的配置使用之Quartz
- xampp修改密码
- 交付程序给别人看,展示劳动成果
- 利用Java编写简单的WebService实例
- UI控件 UIPageController ,控制页数
- 黑马程序员——Java基础---判断语句
- spring3.0定时器
- 按照百分比切割圆形图片(CoreGraphics重绘)
- sbt pom reader - sbt读取pom
- Android Studio Adb启动异常
- java Collections Framework
- 2015编程之美资格赛 A 2月29日
- AndroidManifest.xml中的<uses-feature>以及和<uses-permission>之间的联系
- 自适应网页设计(Responsive Web Design)
- 小工具 -- MyEclipse快捷键整理