一、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;
@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>