JavaWeb三种常用的定时任务简单示例

一、JavaWeb项目中java自带的定时器Timer

//(1)、代码如下:
	package com.sundy.task;
 
	import java.util.Timer;
	import java.util.TimerTask;
 
	import javax.servlet.ServletContextEvent;
	import javax.servlet.ServletContextListener;
 /**
 *
 * java自带的定时器Timer
 * 实现ServletContextListener监听器
 *
 * ServletContextListener接口作用:监听 ServletContext 对象的生命周期,实际上就是监听 Web 应用的生命周期。
 * ServletContextListener 接口主要有两个方法
 * 1)个在当Servlet 容器启动web应用时调用,
 * 2)另一个是在Servlet 容器终止web应用时调用
 */
	public class MyTimerTask implements ServletContextListener{
		private Timer timer;
		@Override
		public void contextDestroyed(ServletContextEvent arg0) {
			if(timer!=null) timer.cancel();
		}
 
		@Override
		public void contextInitialized(ServletContextEvent arg0) {
			timer = new Timer();
			timer.schedule(new TimerTask() {
				
				@Override
				public 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、普通的JavaBean
	package 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/beans
		http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context
		http://www.springframework.org/schema/context/spring-context.xsd
		http://www.springframework.org/schema/task
		http://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、普通的JavaBean
	package com.sundy.task;
 
	import org.springframework.scheduling.annotation.Scheduled;
	import org.springframework.stereotype.Component;
 
	@Component
	public 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、普通的JavaBean
	package 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/beans
		http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context
		http://www.springframework.org/schema/context/spring-context.xsd
		http://www.springframework.org/schema/task
		http://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>

 

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值