spring定时练习

1.配置文件

<?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:p="http://www.springframework.org/schema/p"
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:jaxws="http://cxf.apache.org/jaxws"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
           http://cxf.apache.org/jaxws 
           http://cxf.apache.org/schemas/jaxws.xsd">
    <!-- 要调用的工作类 -->
    <bean id="TestJob" class="com.kt.test.TestJob"></bean>
    <!-- 定义调用对象和调用对象的方法****************************** -->
    <bean id="TestJobTask" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
        <property name="targetObject">
            <ref bean="TestJob"/>
        </property>
        <property name="targetMethod">
            <!-- 指定定时执行的方法 -->
            <value>test</value>
        </property>
        <property name="concurrent">
            <value>false</value>
        </property>
    </bean>
    
    <!-- 定义触发时间***************************** -->
    <bean id="testJobTime" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
        <property name="jobDetail">
            <ref bean="TestJobTask"/>
        </property>
        <property name="cronExpression">
            <value>0/2 * * * * ?</value>
        </property>
    </bean>
    
    <!-- 启动工作 -->
    <bean id="startQuertz" lazy-init="false" autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <property name="triggers">
            <list>
                <ref bean="testJobTime"/>
            </list>
        </property>
    </bean>
    </beans>

2.Java代码

package com.kt.test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class TestJob {
     protected Logger log = LoggerFactory.getLogger(this.getClass());
        public  void test(){
            log.info("test:"+System.currentTimeMillis());
            //System.out.println("test"+System.currentTimeMillis());
        }
}

3.web.xml配置

<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" version="2.5">
  <display-name>ES1</display-name>
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
              classpath*:/applicationContext.xml
              classpath*:/applicationContext-quartz.xml
              classpath*:/cacheContext.xml
          </param-value>
  </context-param>

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。