zoukankan      html  css  js  c++  java
  • quartz---springmvc的配置文件正合

                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

            

  • 相关阅读:
    关于html5的一些知识。
    常见的http状态码总结。
    踩坑记录-安装node-sass运行报错TypeError: this.getResolve is not a function at Object.loader
    踩坑记录-!!vue-style-loader!css-loader错误
    koa-passport做登录注册验证
    nuxt项目里使用vuex状态树
    node(koa、nuxt等项目)中使用import报错问题
    koa+nodemailer实现邮箱验证注册功能
    踩坑记录-nuxt引入vuex报错store/index.js should export a method that returns a Vuex instance.
    常用shell命令积累
  • 原文地址:https://www.cnblogs.com/meiLinYa/p/9049172.html
Copyright © 2011-2022 走看看