SSM框架使用quartz處理定時任務問題(方法二)

首先,配置maven的pom.xml:

<!-- 定時任務 -->
<dependency>
        <groupId>org.quartz-scheduler</groupId>
        <artifactId>quartz</artifactId>
        <version>2.1.1</version>
</dependency>
<dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context-support</artifactId>
        <version>4.3.2.RELEASE</version>
</dependency>

然後,在spring-service.xml中,添加配置文件頭部:

xmlns:task="http://www.springframework.org/schema/task"

在xsi:schemaLocation,添加:

http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task.xsd

在spring-service.xml中的配置信息:

<!-- test定時任務更新-->
<bean id="testJobDetail" class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
        <property name="jobClass" value="com.text.task.Test"></property>
        <property name="applicationContextJobDataKey" value="applicationContext"></property>
</bean>

<!-- 觸發器 -->
<bean id="testTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
        <property name= "jobDetail" ref="testJobDetail"></property>
        <property name="cronExpression" value="*/5 * * * * ?"></property>
</bean> 
    
<!-- 調度器 -->
<bean id="scheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <property name="triggers">
            <list>
               	<ref bean="testTrigger"/>
            </list>
        </property> 
</bean>

在com.text.task包中的Test類:

package com.text.task;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

public class Test  implements Job{
public void execute(JobExecutionContext paramJobExecutionContext) throws JobExecutionException {
		 //打印當前的執行時間,格式爲2017-01-01 00:00:00 
		Date date = new Date(); 
		SimpleDateFormat sf =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
		System.out.println("Current Exec Time Is :" + sf.format(date));
	}
}

運行Tomcat,Eclipse控制檯輸出結果:

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章