Spring Quartz Java工程版和Web工程版示例
環境: MyEclipse8.6 + Tomcat6.0.18 + Spring2.5.6
最近研究Spring Quartz定時器任務排程,寫了兩個Demo,歡迎閱讀指正。
Spring Quartz Java工程版
Mission.java(任務原始檔)
package com.springquartz.bean;
import org.apache.log4j.Logger;
/**
* @className:Mission.java
* @classDescription:
* @author:Wentasy
* @createTime:2012-7-15 下午07:14:36
* @since:JDK 1.6
*/
public class Mission {
private Logger logger = Logger.getLogger(this.getClass().getName());
public void print(){
logger.info("開始進行任務排程......");
}
}
SpringQuartzTest.java(測試原始檔)
package com.springquartz.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @className:SpringQuartzTest.java
* @classDescription:
* @author:Wentasy
* @createTime:2012-7-15 下午07:14:55
* @since:JDK 1.6
*/
public class SpringQuartzTest {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("測試開始......");
ApplicationContext actx = new ClassPathXmlApplicationContext("applicationContext.xml");
System.out.println("測試結束......");
}
}
applicationContext.xml(Spring Quartz配置檔案)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<!-- 要排程的物件 -->
<bean id="job" class="com.springquartz.bean.Mission"></bean>
<!-- 定義目標bean和bean中的方法 -->
<bean id="jobTask" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject">
<ref local="job"/>
</property>
<property name="targetMethod">
<value>print</value>
</property>
</bean>
<!-- 定義觸發的時間 -->
<bean id="cron" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail">
<ref bean="jobTask"/>
</property>
<property name="cronExpression">
<value>10,15,20,25,30,35,40,45,50,55,00 * * * * ?</value>
<!-- <value>00,05 53,54 * * * ?</value>-->
</property>
</bean>
<!-- 總管理 -->
<bean autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref local="cron"/>
</list>
</property>
</bean>
</beans>
執行效果截圖:
Spring Quartz Web工程版
PrintInfoJob.java(任務原始檔)
package com.springquartz.bean;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;
import com.springquartz.service.IPrintInfoService;
/**
* @className:Mission.java
* @classDescription:
* @author:Wentasy
* @createTime:2012-7-15 下午07:14:36
* @since:JDK 1.6
*/
public class PrintInfoJob extends QuartzJobBean{
private IPrintInfoService prinfInfoService = null;
public IPrintInfoService getPrinfInfoService() {
return prinfInfoService;
}
public void setPrinfInfoService(IPrintInfoService prinfInfoService) {
this.prinfInfoService = prinfInfoService;
}
@Override
protected void executeInternal(JobExecutionContext arg0)
throws JobExecutionException {
// TODO Auto-generated method stub
this.prinfInfoService.print();
}
}
IprintInfoService.java(任務介面原始檔)
package com.springquartz.service;
/**
* @className:IPrintInfoService.java
* @classDescription:
* @author:Wentasy
* @createTime:2012-7-15 下午08:00:31
* @since:JDK 1.6
*/
public interface IPrintInfoService {
/**
* 方法名:print
* 功能:列印資訊
*/
public void print();
}
PrintInfoServiceImpl.java(任務實現原始檔)
package com.springquartz.service.impl;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import com.springquartz.service.IPrintInfoService;
/**
* @className:PrintInfoServiceImpl.java
* @classDescription:
* @author:Wentasy
* @createTime:2012-7-15 下午07:59:33
* @since:JDK 1.6
*/
public class PrintInfoServiceImpl implements IPrintInfoService{
public void print() {
// TODO Auto-generated method stub
Calendar now = Calendar.getInstance();
System.out.println("現在是北京時間:" + this.format(now.getTime()));
}
public String format(Date date){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return sdf.format(date);
}
}
applicationContext.xml(Spring Quartz配置檔案)
<?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:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<bean id="printInfoService" class="com.springquartz.service.impl.PrintInfoServiceImpl"/>
<!-- 配置一個Job -->
<bean id="printInfoJob" class="org.springframework.scheduling.quartz.JobDetailBean">
<property name="jobClass" value="com.springquartz.bean.PrintInfoJob"/>
<property name="jobDataAsMap">
<map>
<entry key="prinfInfoService" value-ref="printInfoService"></entry>
</map>
</property>
</bean>
<!-- 簡單的觸發器 -->
<bean id="simplePrintInfoTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
<property name="jobDetail">
<ref bean="printInfoJob"/>
</property>
<property name="startDelay">
<value>6000</value>
</property>
<property name="repeatInterval">
<value>6000</value>
</property>
</bean>
<!--複雜的觸發器 -->
<bean id="complexPrintInfoTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail">
<ref bean="printInfoJob"/>
</property>
<property name="cronExpression">
<!-- <value>0 0/1 * * * ?</value> -->
<value>00,05,10,15,20,25,30,35,40,45,50,55 * * * * ?</value>
</property>
</bean>
<!-- spring觸發工廠 -->
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="complexPrintInfoTrigger"/>
</list>
</property>
</bean>
</beans>
web.xml(Web工程配置檔案)
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<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>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
執行效果截圖:
專案注意事項:
1.所需Jar包:
commons-collections-3.2.jar commons-logging-1.1.jar jta.jar log4j-1.2.15.jarquartz-all-1.6.5.jar spring.jar
本文原始碼,歡迎下載:
參考資料:
相關文章
- 英偉達 TITAN X 顯示卡工程版細節全面曝光
- “軟體工程版”網友自律守則(第二稿) (轉)軟體工程
- “軟體工程版”網友行為守則(討論稿) (轉)軟體工程
- Quartz - Spring和Quartz的整合quartzSpring
- spring-quartz整合Springquartz
- 使用Java和Spring MVC構建Web應用JavaSpringMVCWeb
- 如何實現Spring Boot和Quartz整合? - Nguyen Phuc HaiSpring BootquartzAI
- Java Web之Spring BootJavaWebSpring Boot
- Spring Boot Crud操作示例 | Java Code GeeksSpring BootJava
- quartz在web中時有罷工(轉)quartzWeb
- Spring Quartz定時器&n…Springquartz定時器
- Quartz在Spring中叢集quartzSpring
- Quartz - Spring整合Quartz實現叢集的定時任務quartzSpring
- Spring quartz 叢集模式的坑Springquartz模式
- Spring Boot 參考指南(Quartz Scheduler)Spring Bootquartz
- Spring Scheduler定時任務 + QuartzSpringquartz
- Java,Spring,SpringBoot和Axon實現CQRS深度示例 -jofisaes@gmail.comJavaSpring BootAI
- Java-Spring-WebService最基礎的配置示例JavaSpringWeb
- Java Web系列:Spring Boot 基礎JavaWebSpring Boot
- Java Web系列:Spring Security 基礎JavaWebSpring
- Java Web系列:Spring MVC基礎JavaWebSpringMVC
- Spring 文件 - 示例Spring
- 普通web整合quartz跑定時任務Webquartz
- Spring Boot 配置 Quartz 定時任務Spring Bootquartz
- Spring中Quartz排程器的使用Springquartz
- java web技術比較好的spring和hibernate書籍JavaWebSpring
- 用Java拆分字串示例和技巧 -DreamixJava字串
- 用Spring Web Flow和Terracotta搭建Web應用SpringWeb
- Spring - constructor-arg和property的使用示例SpringStruct
- Spring整合Quartz案例使用RAM儲存方式Springquartz
- Spring整合Quartz案例使用JDBC儲存方式SpringquartzJDBC
- Spring+quartz 實現定時任務Springquartz
- spring整合quartz的叢集配置例項Springquartz
- spring quartz如何實現多工定時Springquartz
- Spring MVC 完整示例SpringMVC
- Java Web系列:Spring依賴注入基礎JavaWebSpring依賴注入
- java Quartz 定時任務管理類Javaquartz
- java反射示例Java反射