定义一个任务是很简单的实现TimerTask的run方法就可以了. 如下: SayHelloTask.java 1package test.timerTask;
2 3import java.util.TimerTask; 4 5public class Task extends TimerTask { 6 7 @Override 8 public void run() { 9 // TODO Auto-generated method stub 10 System.out.println("测试TimerTask : Hello !!"); 11 } 12 13} 1package test.springTimer;
2 3import java.util.TimerTask; 4 5public class Task2 extends TimerTask{ 6 public void run(){ 7 8 System.out.println("task2 is running"); 9 } 10 11 12} 13 然后是配置文件: <?xml version="1.0" encoding="UTF-8"?> <beans> <bean id="scheduledTask" class="org.springframework.scheduling.timer.ScheduledTimerTask">
测试类如下: TestApp.java package test.timerTask;
import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class TestApp { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub ApplicationContext context = new ClassPathXmlApplicationContext("test/timerTask/javaTimer.xml"); // ApplicationContext context2 = new ClassPathXmlApplicationContext("test/timerTask/quartzTimer.xml"); } // 只要加载配置文件就可以了, } 运行结果: task2 is running task2 is running haha,task is running haha,task is running task2 is running 使用Java中的定时器比较简单,其提供的任务也比较简单, 下面来看看使用quartz来执行一个复杂的任务. 1package test.timerTask;
2 3import org.quartz.JobExecutionContext; 4import org.quartz.JobExecutionException; 5import org.springframework.scheduling.quartz.QuartzJobBean; 6 7public class SayHelloTaskUsingQuartz extends QuartzJobBean { 8 9 @Override 10 protected void executeInternal(JobExecutionContext context) 11 throws JobExecutionException { 12 // TODO Auto-generated method stub 13 System.out.println("使用Quartz 认为调度: Hello!!"); 14 } 15 16} 17 <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <bean id="sayHelloJob" class="org.springframework.scheduling.quartz.JobDetailBean"> <property name="jobClass"> <value>test.timerTask.SayHelloTaskUsingQuartz</value> </property> </bean> <!-- 关键在如下两个触发器的配置 --> <!-- 类似于Java的简单触发器 --> <bean id="helloTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean"> <property name="jobDetail"> <ref bean="sayHelloJob"/> </property> <property name="startDelay"> <value>1000</value> </property> <property name="repeatInterval"> <value>3000</value> </property> </bean> <!-- 复杂触发器 --> <bean id="helloCronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean"> <property name="jobDetail"> <ref bean="sayHelloJob"/> </property> <property name="cronExpression"> <!-- 关键在配置此表达式 --> <value>0 49 15 * * </value> </property> </bean> <bean id="scheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <ref bean="helloCronTrigger"/> </property> </bean> </beans> |
- 浏览: 169590 次
- 性别:
- 来自: 广州
最新评论
-
why240829544:
谢谢
Spring Security 2.0.x中文参考文档.pdf -
lzc879141573:
...
Spring Security 2.0.x中文参考文档.pdf -
zpl:
C:\Documents and Settings\Admin ...
导入word到Fckeditor(java实现) -
zpl:
[img][/img] asdfasdfasdfa
导入word到Fckeditor(java实现) -
zhoutao.temp:
Java文件下载的几种方式
相关推荐
spring定时任务 spring多线程的一个简单示例。
3、SpringCloudSchedule定时任务(使用mybatis读写mysql数据库, spring实现定时任务, 按照间隔时间或固定时间方式执行定时任务) 4、定时自动运行任务(在数据库中设置好定时时间,及运行的程序,及参数) 5、C#...
1.使用Quartz,这是一个功能比较强大的的调度器,可以让你的程序在指定时间执行,也可以按照某一个频度执行. 2.Spring3.0以后自带的task,可以将它看成一个轻量级的Quartz,而且使用起来比Quartz简单许多.
通过本文,读者将能够了解如何在Spring Boot应用中轻松创建和管理定时任务。 # @Scheduled注解简介 在Spring框架中,@Scheduled注解用于标记一个方法,使其能够在固定的时间间隔内自动执行。这个注解主要基于Java的...
主要介绍了spring定时任务(scheduler)的串行、并行执行实现解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
主要给大家介绍了关于spring定时任务执行两次及tomcat部署缓慢问题的解决方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面小编来一起学习学习吧。
主要介绍了spring 定时任务@Scheduled的相关资料,文中通过示例代码介绍的很详细,相信对大家的理解和学习具有一定的参考借鉴价值,有需要的朋友们下面来一起看看吧。
1、从数据库中获取定时任务执行; 2、在数据库中修改定时任务后,执行任务也可以更新(刷新间隔时间); 3、适应于spring 于quartz不能解决的场景 基本等价整合功能,满足实际应用场景(任务暂停、恢复等复杂功能不能...
主要给大家介绍了关于Spring Boot中配置定时任务、线程池与多线程池执行的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用Spring Boot具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
1,实现最新版本的Struts2.3.8 Spring 3.2 Hibernate4.1.9 集成 2,网站启动初始化,定时器执行代码 3,实现CRUD示例 此代码是对 http://download.csdn.net/detail/net_lover/4954763 的修正,不要再下载 ...
1,实现最新版本的Struts2.3.8 Spring 3.2 Hibernate4.1.9 集成 2,网站启动初始化,定时器执行代码 3,实现CRUD示例
主要给大家介绍了关于Spring定时任务中@PostConstruct被多次执行异常的分析与解决方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧。
主要介绍了浅谈spring 线程异步执行,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
9.5.5. <tx:advice/> 有关的设置 9.5.6. 使用 @Transactional 9.5.6.1. @Transactional 有关的设置 9.5.7. 插入事务操作 9.5.8. 结合AspectJ使用 @Transactional 9.6. 编程式事务管理 9.6.1. 使用 ...
9.5.5. <tx:advice/> 有关的设置 9.5.6. 使用 @Transactional 9.5.7. 事务传播 9.5.8. 通知事务操作 9.5.9. 结合AspectJ使用 @Transactional 9.6. 编程式事务管理 9.6.1. 使用TransactionTemplate 9.6.2. ...
9.5.5. <tx:advice/> 有关的设置 9.5.6. 使用 @Transactional 9.5.7. 插入事务操作 9.5.8. 结合AspectJ使用 @Transactional 9.6. 编程式事务管理 9.6.1. 使用 TransactionTemplate 9.6.2. 使用 ...
9.5.5. <tx:advice/> 有关的设置 9.5.6. 使用 @Transactional 9.5.7. 事务传播 9.5.8. 通知事务操作 9.5.9. 结合AspectJ使用 @Transactional 9.6. 编程式事务管理 9.6.1. 使用TransactionTemplate 9.6.2. ...
主要介绍了Spring boot如何通过@Scheduled实现定时任务及多线程配置,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
9.5.5. <tx:advice/> 有关的设置 9.5.6. 使用 @Transactional 9.5.7. 插入事务操作 9.5.8. 结合AspectJ使用 @Transactional 9.6. 编程式事务管理 9.6.1. 使用 TransactionTemplate 9.6.2. 使用 ...
9. 定时任务、异步任务线程池管理 ## 项目备注 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 2、本项目适合计算机相关专业(如计科、人工智能、通信工程、自动化、电子信息等)...