码迷,mamicode.com
首页 > 编程语言 > 详细

quartz---springmvc的配置文件正合

时间:2018-05-17 11:45:02      阅读:137      评论:0      收藏:0      [点我收藏+]

标签:util   form   component   ack   web   span   定时任务   sim   void   

            quartz---springmvc的配置文件正合

XML

<beans xmlns="http://www.springframework.org/schema/beans"
    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:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
    <!-- first -->
    <!-- <bean name="firstSimpleJobDetail"
        class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
        <property name="targetObject" ref="myBean"></property>
        <property name="targetMethod" value="helloBean"></property>
    </bean> -->
    <!-- secodent -->
    <bean name="simpleJobDetail"
        class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
        <property name="jobClass" value="com.taotao.web.bean.FirstScheduledJob"></property>
        <property name="jobDataMap">
            <map>
                <entry key="anotherBean" value-ref="anotherBean"></entry>
            </map>
        </property>
        <property name="Durability" value="true" />
    </bean>
    <!-- 距离当前时间2分钟后执行,之后每隔两秒钟执行一次 -->
    <!-- <bean id="SimpleTrigger"
        class="org.springframework.scheduling.quartz.SimpleTriggerFactoryBean">
        <property name="jobDetail" ref="simpleJobDetail"></property>
        <property name="startDelay" value="1000"></property>
        <property name="repeatInterval" value="2000"></property>
    </bean> -->
    <!-- cronTrigger -->
    <bean id="firstSimlpeTrigger"
        class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
        <property name="jobDetail" ref="simpleJobDetail"></property>
        <!-- 每30秒每分每月每周执行一次 -->
        <property name="cronExpression" value="0/5 * * ? * *"/>
    </bean>
    <!-- 触发器启动 -->
    <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <property name="jobDetails">
            <list>
                <ref bean="firstSimpleJobDetail"></ref>
                <ref bean="simpleJobDetail"></ref>
            </list>
        </property>
        <property name="triggers">
            <list>
                <ref bean="SimpleTrigger"></ref>
                <ref bean="firstSimlpeTrigger"></ref>
            </list>
        </property>
    </bean>
</beans>

myBean

package com.taotao.web.bean;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.stereotype.Component;

@Component("myBean")
public class MyBean {

    public void helloBean(){
        Date date=new Date();
        SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println("MyBean"+sf.format(date));
    }
}

AnotherBean

package com.taotao.web.bean;

import org.springframework.stereotype.Component;

@Component("anotherBean")
public class AnotherBean {

    public void printlnL() {
        System.out.println("HUHU");
    }
}

FirstScheduledJob

package com.taotao.web.bean;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;

public class FirstScheduledJob extends QuartzJobBean {

    private AnotherBean anotherBean;

    public void setAnotherBean(AnotherBean anotherBean) {
        this.anotherBean = anotherBean;
    }

    @Override
    protected void executeInternal(JobExecutionContext arg0) throws JobExecutionException {
        Date date = new Date();
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println("hello FirstScheduledJob Executes!" + sf.format(date));
        this.anotherBean.printlnL();
    }

}

      以上代码使用了2中方法实现了定时任务器:

    1)两种绑定方法

        1.org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean

        2.org.springframework.scheduling.quartz.JobDetailFactoryBean

      都利用了工厂模式。

    2)两种触发方式:

        1.org.springframework.scheduling.quartz.SimpleTriggerFactoryBean

        2.org.springframework.scheduling.quartz.CronTriggerFactoryBean

    3)一个触发器:

         org.springframework.scheduling.quartz.SchedulerFactoryBean

        

quartz---springmvc的配置文件正合

标签:util   form   component   ack   web   span   定时任务   sim   void   

原文地址:https://www.cnblogs.com/meiLinYa/p/9049172.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!