java.util.Timer:实现周期Job
/**
*java.util.Timer:
* --工作原理:将处理模型放入到单线程队列中,在加入队列的时候对模型进行标记,之后通过线程实现查找最近执行目标进行执行。
* --周期执行任务
* --串行执行,若之前任务出现异常则周期任务停止
* --好像是使用junit测试环境是不好使
*/
public class TestTimer extends TimerTask{
public void run() {
//线程延迟
/* Thread thread = new Thread();
try {
thread.sleep(10000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}*/
System.out.println("Timer Begin。。。!!!");
//异常抛出
//throw new RuntimeException();
}
public static void main(String[] args) {
Timer timer = new Timer();
timer.schedule(new TestTimer(), 3000, 5000);
}
<p>}</p>
spring Quartz:实现周期Job和定时Job
实现:TestQuartz.class + spring-quartz.xml + web.xml
TestQuartz.class:
/**
*Spring Quartz:
* --SimpleTrigger:周期执行任务
* --配置方式:
* --:首先实现类必须继承QuartzJobBean,并且实现其方法,executeInternal()方法自动执行方法
* --:在spring.xml配置文件中:
* --首先配置JobDetailBean的Bean为Job明细Bean
* --并且填写其中参数<property name="jobClass" value="目标类"/>
* --配置SimpleTriggerBean实现周期触发器任务
* --实现参数:<property name="jobDetail" ref="配置的明细Bean"/>
* <properby name="startDelay" value="启动延迟时间。。(ms)"/>
* <properby name="repeatInterval" value="周期时间。。(ms)"/>
*
* --配置SchedulerFactoryBean,作为Job工厂Bean,批量调度任务
* --实现参数<property name="triggers"><list><ref bean="欲执行的Trigger"></ref></list>< /property>
* --CronTrigger:周期执行任务
* --配置方式:与SimpleTrigger1,3部相同,在配置触发器时参数为:
* <property name="jobDetail" ref="配置的明细Bean"/>
* <property name="cronExpression" value="执行时间"/>
* --执行时间参数(秒(0),分(0),时(0),天(*),月(*),年(*)),参数必须合理
*
*--Quartz除了依赖于spring的包之外,还需要一些日志信息包,如slf4j.api.jar,slf4j.log4j.ar,log4j.jar 反正不添加就报错。
*--还有就是版本支持问题,我使用了spring4.0.5版本的包,但是不能支持2.2.1的quartz.jar,但是可以支持1.8.4版本的quartz.jar
*--经过测试默认情况下SimpleTrigger的执行方式是并行的
*
*/
public class TestQuartz extends QuartzJobBean {
@Override
protected void executeInternal(JobExecutionContext arg0)
throws JobExecutionException {
Thread thread = new Thread();
try {
thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Quartz Begin...!!!");
}
}
spring-quartz.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:context="http://www.springframework.org/schema/context"
xmlns:task="http://www.springframework.org/schema/task"
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-3.0.xsd">
<!-- Job任务明细 -->
<bean id="jobOne" class="org.springframework.scheduling.quartz.JobDetailBean">
<property name="jobClass" value="com.test.schedule.quartz.TestQuartz" />
</bean>
<!-- 周期Job配置 触发器-->
<bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
<!-- Job任务明细 引入-->
<property name="jobDetail" ref="jobOne" />
<!-- Job延迟执行时间-->
<property name="startDelay" value="5000" />
<!-- Job任务周期-->
<property name="repeatInterval" value="3000"/>
</bean>
<!-- 定时Job配置 触发器-->
<bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<!-- 定时Job配置 引入-->
<property name="jobDetail" ref="jobOne"/>
<!-- 定时任务时间配置 参数:秒 分 时 天 月 年;其中时分秒必须用数字填写,*表示全局-->
<property name="cronExpression" value="0 15 14 * * ?"/>
</bean>
<!-- 任务执行工厂-->
<bean id="schedulerFactory" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<!-- 任务执行工厂 触发器引入-->
<property name="triggers">
<list>
<ref bean="cronTrigger"/>
<ref bean="simpleTrigger"/>
</list>
</property>
</bean>
</beans>
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>Spring_Copy</display-name>
<!-- Spring 通过监听获取配置的spring配置文件-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Spring 重新定义默认applicationContext.xml的名称以及位置,可以同时配置多个以及使用*进行通配
默认:/WEB-INF/applicationContext.xml
-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:spring-schedule.xml
</param-value>
</context-param>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:spring-quartz.xml
</param-value>
</context-param>
</web-app>
@scheduled:实现周期Job和定时Job
实现:TestSchedule.class + spring-schedule.xml + web.xml
TestSchedule.class:
/**
*测试spring @Scheduled 实现定时任务.
*配置3项:
* --1.配置spring-schedule.xml
* --首先添加spring-context.jar下的 task.xsd标签
* --添加包扫描
* --添加定时任务注解驱动器:<task:annotation-driven/> 这个是必须的奥,没有他不好使奥
* --2.在web.xml中添加将spring-schedule.xml添加到web服务的上下文中web.xml中添加
* --将web中默认的applicationContext.xml修改为指向指定位置的spring-schedule.xml文件,此工程中位置为 classpath:spring-schedule.xml
* --修改默认的spring配置文件名称 <param-name>的参数必须为contextConfigLocation才能被监听所捕获到!!!
* <context-param>
* <param-name>contextConfigLocation</param-name>
* <param-value>
* classpath:spring-schedule.xml
* </param-value>
* </context-param>
* --配置spring的文件加载监听:ContextLoaderListener
* <!-- Spring 通过监听获取配置的spring配置文件-->
* <listener>
* <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
* </listener>
* --3.实现类的配置:
* --实现类必须添加@Component注解
* --在具体的实现方法上添加@Scheduled(cron="秒 分 时 天 月 星期 年")参数,确定执行时间,注意因为天和星期互斥,所以可以使用 '?'屏蔽其中一个选项
* --默认情况下实现类不能有返回值,否则会报错,但是通过设置可以使用在带有返回值的实现类中。
*
*经过测试通过@Scheduled配置的定时任务为并行的
*/
@Component
public class TestSchedule extends UnitTestBase{
public TestSchedule(){
super("classpath:spring-schedule.xml");
}
//@Scheduled(cron = "0/5 * * * * ?")
public void testChedualOne(){
System.out.println("testChedualOne:Schedule Begin !!!");
threadSleep();
}
//@Scheduled(cron = "0/5 * * * * ?")
public void testChedualTwo(){
System.out.println("testChedualTwo:Schedule Begin !!!");
}
//@Scheduled(cron = "0/5 * * * * ?")
public void testChedualThree(){
System.out.println("testChedualThree:Schedule Begin !!!");
}
public static void threadSleep(){
Thread thread = new Thread();
try {
System.out.println("sleep before!");
thread.sleep(10000);
System.out.println("sleep after!");
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
spring-schedule.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:context="http://www.springframework.org/schema/context"
xmlns:task="http://www.springframework.org/schema/task"
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-3.0.xsd">
<!-- 通过包扫描实现对spring注解扫描-->
<context:component-scan base-package="com.test.schedule"></context:component-scan>
<!-- job启动 -->
<task:annotation-driven/>
</beans>
web.xml:与 Quartz配置的web.xml相同