zoukankan      html  css  js  c++  java
  • Spring定时器实现(二)

    Spring结合quarzt可以实现更复杂的定时器,现做简单介绍相关配置:

    <?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:task="http://www.springframework.org/schema/task"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
            http://www.springframework.org/schema/task
            http://www.springframework.org/schema/task/spring-task-4.3.xsd ">
    
        <description>spring-configuration</description>
    
        <bean id="timerTask" class="com.charles.spring.service.impl.TimerTaskImpl"></bean>
        
        <bean id="timerDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
            <property name="targetObject" ref="timerTask"></property>
            <property name="targetMethod" value="doTimerTask"></property>
        </bean>
    
        <bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerFactoryBean">
            <property name="jobDetail" ref="timerDetail" />
            <property name="startDelay" value="10000" />
            <property name="repeatInterval" value="3000" />
        </bean>
    
        <bean id="startTimer" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
            <property name="triggers">
                <list>
                    <ref bean="simpleTrigger" />
                </list>
            </property>
        </bean>
    
    </beans>

    由于结合quartz的原因,需要相关quartz的Jar包,可到官网下载:http://www.quartz-scheduler.org/

    接口与其实现类与Spring定时器实现(一)相同

    package com.charles.spring.service.impl;
    
    import com.charles.spring.service.TimerTask;
    
    public class TimerTaskImpl implements TimerTask {
    
        @Override
        public void doTimerTask() throws Exception {
            System.out.println("Hello Timer");
        }
    
    }

    测试类:

    package com.charles.spring.handler;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class Timer {
    
        public static void main(String[] args) {
            
            @SuppressWarnings({ "unused", "resource" })
            ApplicationContext context = new ClassPathXmlApplicationContext("config/spring-config.xml");
            try {
                Thread.sleep(10*60*1000);
            } catch (Exception e) {
                
            }
    
        }
    
    }

    以上算是Spring定时器第二种实现方式了,不过、目前还有另一种配置,如下:

    <?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:task="http://www.springframework.org/schema/task"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
            http://www.springframework.org/schema/task
            http://www.springframework.org/schema/task/spring-task-4.3.xsd ">
    
        <description>spring-configuration</description>
    
        <bean id="timerTask" class="com.charles.spring.service.impl.TimerTaskImpl"></bean>
        
        <bean id="timerDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
            <property name="targetObject" ref="timerTask"></property>
            <property name="targetMethod" value="doTimerTask"></property>
        </bean>
    
        <bean id="cronTigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
            <property name="jobDetail" ref="timerDetail"></property>
            <property name="cronExpression">
                <value>0/2 * * * * ?</value>
            </property>
        </bean>
    
        <bean id="startTimer" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
            <property name="triggers">
                <list>
                    <ref bean="cronTigger" />
                </list>
            </property>
        </bean>
    
    </beans>

    此时、将原先配置文件中的

    <bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerFactoryBean">
        <property name="jobDetail" ref="timerDetail" />
        <property name="startDelay" value="10000" />
        <property name="repeatInterval" value="3000" />
    </bean>
    修改为:
    <bean id="cronTigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
        <property name="jobDetail" ref="timerDetail"></property>
        <property name="cronExpression">
            <value>0/2 * * * * ?</value>
        </property>
    </bean>  

    即可。

  • 相关阅读:
    (十三)子查询
    (十二)多表查询
    MFC读写配置ini文件
    (十一)分组函数(多行函数)
    Django(二十一)组合搜索
    Django(二十)model中的 class Meta
    (十)单行函数
    (九)逻辑运算,order by,desc
    类作为成员变量
    内部类——匿名内部类
  • 原文地址:https://www.cnblogs.com/itachy/p/7211612.html
Copyright © 2011-2022 走看看