JavaWeb三种常用的定时任务简单示例
来源:互联网 发布:在线视频聊天源码 编辑:程序博客网 时间:2024/06/07 21:45
一、JavaWeb项目中java自带的定时器Timer
(1)、代码如下:package com.sundy.task;import java.util.Timer;import java.util.TimerTask;import javax.servlet.ServletContextEvent;import javax.servlet.ServletContextListener;public class MyTimerTask implements ServletContextListener{private Timer timer;@Overridepublic void contextDestroyed(ServletContextEvent arg0) {if(timer!=null) timer.cancel();}@Overridepublic void contextInitialized(ServletContextEvent arg0) {timer = new Timer();timer.schedule(new TimerTask() {@Overridepublic void run() {work();}}, 1000, 15*1000);// 一秒后执行,间隔十五秒执行一次。}private void work() {System.out.println("定时任务....");}}(2)、需要在web.xml中增加<listener><listener-class>com.sundy.task.MyTimerTask</listener-class></listener>
二、JavaWeb项目中Spring自带的定时任务的使用简介
1)、非注解的开发1.1、普通的JavaBeanpackage com.sundy.task;public class MyTask {public void task(){System.out.println("定时任务...");}}1.2、在applicationContext.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:task="http://www.springframework.org/schema/task"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/taskhttp://www.springframework.org/schema/task/spring-task-4.2.xsd"><bean id="myTask" class="com.sundy.task.MyTask"></bean> <task:scheduled-tasks> <!-- cron的表达式可参考《http://blog.csdn.net/evilcry2012/article/details/51991791》 --><task:scheduled ref="myTask" method="task" cron="*/5 * * * * ?"/> <!-- 每隔五秒执行一次com.sundy.task.MyTask类中的task()方法 --></task:scheduled-tasks> </beans>1.3、在web中记得增加:<!-- 加载spring容器 --><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:applicationContext.xml</param-value></context-param><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener>(2)、注解的开发2.1、普通的JavaBeanpackage com.sundy.task;import org.springframework.scheduling.annotation.Scheduled;import org.springframework.stereotype.Component;@Componentpublic class MyTask {@Scheduled(cron="*/5 * * * * ?")public void task1(){System.out.println("定时任务...");}}2.2、在applicationContext.xml中的配置<context:component-scan base-package="com.sundy.task"/><!-- 配置任务线性池 --> <task:executor id="executor" pool-size="10" /> <task:scheduler id="scheduler" pool-size="10" /> <task:annotation-driven scheduler="scheduler" executor="executor" proxy-target-class="true"/>
三、定时任务quartz的简单使用
(1)、官网下载地址:http://www.quartz-scheduler.org/downloads/任务调度开源框架Quartz动态添加、修改和删除定时任务 地址:http://blog.csdn.net/luo201227/article/details/37511137(2)、非注解的简单示例(参考:http://veiking.iteye.com/blog/2371511)1.1、普通的JavaBeanpackage com.sundy.quartz;public class SimpleJob {public void runTask() {System.out.println("runTask()....");}}1.2、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:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/taskhttp://www.springframework.org/schema/task/spring-task-4.2.xsd"><bean id="simpleJob" class="com.sundy.quartz.SimpleJob"></bean> <bean id="simpleJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"><property name="targetObject" ref="simpleJob"/> <property name="targetMethod" value="runTask"/> <property name="concurrent" value="false"/> <!-- concurrent : false表示等上一个任务执行完后再开启新的任务 --> </bean> <!-- 配置任务调度的时间/周期、延时 --> <bean id="simpleJobDetailTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean"> <property name="jobDetail" ref="simpleJobDetail"/> <property name="cronExpression" value="*/10 * * * * ?"/> <property name="startDelay" value="3000"/> </bean> <!-- 配置任务调度中心 --> <bean id="schedulerFactoryBean" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <list> <ref bean="simpleJobDetailTrigger"/> </list> </property> </bean> </beans>
阅读全文
0 0
- JavaWeb三种常用的定时任务简单示例
- java实现定时任务的三种常用方法
- quartz简单定时任务示例
- JAVAweb开发技术-------(六)技术点-定时任务实现的三种方式
- 简单的定时任务
- 简单的定时任务
- quartz定时任务时间设置--常用示例
- 定时执行任务的三种方法
- 定时执行任务的三种方法
- 定时执行任务的三种方法
- 定时执行任务的三种方法
- 定时执行任务的三种方法
- 定时执行任务的三种方法:
- 定时执行任务的三种方法
- 定时执行任务的三种方法
- 定时执行任务的三种方法
- 定时执行任务的三种方法
- 定时执行任务的三种方法
- C++设计模式感想
- 幽默笑话-傻冒-猿粪啊
- 【python学习笔记】多维数组初始化
- spring-data-redis使用jdk序列化时increment的异常
- JAVA进程占用高内存原因分析与优化方法
- JavaWeb三种常用的定时任务简单示例
- Spring集成Quartz--ScheduleThreadPool详解
- 关于oracle数据库还原备份
- LeetCode--Combinations
- 文件处理命令head
- My97DatePicker双日期时间选择控制
- i386 、x86_64 、ppc是指
- Mysql分页查询丢失数据
- 云计算技术线下学没时间,在线学习4个月挑战年薪30万