Spring Quartz Java工程版和Web工程版示例

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>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值