Spring定时器

 

导入如下jar包:

commons-collections-3.2.jar
commons-logging.jar
jta.jar
log4j-1.2.15.jar
quartz-1.6.0.jar
spring.jar

配置time_bean.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:tx="http://www.springframework.org/schema/tx"

    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd

              http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">

 

    <!--任务类 -->

    <bean id="myService" class="net.service.MyService" />

 

    <!--调用任务类的对象,这个对象调用我们自己定义的任务类 -->

    <bean id="printTimerJob" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">

       <!--调用的任务类 -->

       <property name="targetObject" ref="myService" />

       <!--调用任务类的pringLog方法 -->

       <property name="targetMethod" value="printLog" />

       <!-- 是否并发 -->

       <property name="concurrent" value="false" />

    </bean>

    

    <!-- 定义好调用模式: 如每隔2秒钟调用一次或每天的哪个时间调用一次等 -->

    <bean id="printTimerTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">

       <!--将要调用任务类的对象引入进来 -->

       <property name="jobDetail" ref="printTimerJob" />

       <!--没隔2秒 执行一次调用任务类的对象 -->

       <property name="cronExpression" value="0/2 * * * * ?" />

    </bean>

 

    <!-- 启动定时器 -->

    <!--把定义好的任务放到调度(Scheduler)工厂里面-->

    <bean id="schedulerFactoryBean" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">

       <property name="applicationContextSchedulerContextKey" value="applicationContext" />

       <property name="triggers">

           <list>

              <!--将调度模式 引入进调度工厂中 -->

              <ref bean="printTimerTrigger" />

           </list>

       </property>

    </bean>

 

 

 

 

</beans>

配置web.xml文件(如果不配置此文件,直接运行测试类也可以,配置此文件就可以将定时器应用在web程序中)

<?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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 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>SpringQuartz</display-name>

  <!-- 加载spring的配置文件(一个或者多个) -->

  <context-param>

      <param-name>contextConfigLocation</param-name>

      <param-value>classpath:time-bean.xml</param-value>

  </context-param>

  <!-- 配置spring监听器(作用就是启动Web容器时,自动装配applicationContext.xml文件的配置信息) -->

  <listener>

      <listener-class>

          org.springframework.web.context.ContextLoaderListener

      </listener-class>

  </listener>  

</web-app>

创建MyService类:

package net.service;

 

public class MyService {

    public void printLog(){

       System.out.println("输出日志");

    }

}

创建测试类

package net.test;

 

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

 

public class Test {

    public static void main(String[] args) {

        ApplicationContext act = new ClassPathXmlApplicationContext("time-bean.xml");

   }

}

 

Spring定时器 

 

Spring定时器,布布扣,bubuko.com

Spring定时器

上一篇:Java解惑五:类之谜


下一篇:ios黑科技