JavaWeb三種常用的定時任務簡單示例
阿新 • • 發佈:2018-12-31
(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>