zoukankan      html  css  js  c++  java
  • 04_Spring中使用Quartz

    【Spring中使用SimplerTrigger】

    【QuartzTask.java】

    package com.higgin.task;
    
    import java.text.SimpleDateFormat;
    import java.util.Date;
    
    public class QuartzTask {
        
        public QuartzTask() {
            System.out.println("QuartzTask 构造方法---");
        }
        
        //这个就是要被SimpleTrigger定时触发执行的方法
        public void doSimpleTriggerTask(){
            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            System.out.println("doSimpleTriggerTask()定时执行方法:"+sdf.format(new Date()));
        }
        //这个就是要被CronTrigger定时触发执行的方法
        public void doCronTriggerTask(){
            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            System.out.println("doCronTriggerTask()定时执行方法:"+sdf.format(new Date()));
        }
    }

    【simpleTriggerTask-spring.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:p="http://www.springframework.org/schema/p"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
        xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
        
        <!-- 注册一个普通的bean -->
        <bean id="quartzTask" class="com.higgin.task.QuartzTask"></bean>
            
        <!-- 1.制定任务信息 -->
        <bean id="myJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
            <!-- 设置执行对象 -->
            <property name="targetObject" ref="quartzTask"></property>
            <!-- 设置执行对象中对应的执行方法 -->
            <property name="targetMethod" value="doSimpleTriggerTask"></property>
            <!-- 是否可以同步执行:不可同步执行 -->
            <property name="concurrent" value="false"></property>
        </bean>
        
        <!-- 2.制定任务执行时机(任务执行触发器) -->
        <bean id="simplerTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
            <!-- 设置任务详细 -->
            <property name="jobDetail" ref="myJobDetail"></property>
            <!-- 设置任务延迟执行时间 :延迟2秒后执行(每隔2秒执行一次)-->
            <property name="repeatInterval" value="2000"></property>
        </bean>
        
        <!-- 3.设置调度工厂 -->
        <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
            <!-- 可以通过该属性注册多个Trigger -->
            <property name="triggers">
                <list>
                    <ref bean="simplerTrigger"/>
                </list>
            </property>
        </bean>
        
    </beans>

    【CronTriggerTest.java】

    package com.higgin.test;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class CronTriggerTest {
        public static void main(String[] args) {
            ApplicationContext context =new ClassPathXmlApplicationContext("cronTriggerTask-spring.xml");
        }
    }

    【运行结果】

     

    【Spring中使用CronTrigger】

    【QuartzTask.java,同上】

    package com.higgin.task;
    
    import java.text.SimpleDateFormat;
    import java.util.Date;
    
    public class QuartzTask {
        
        public QuartzTask() {
            System.out.println("QuartzTask 构造方法---");
        }
        
        //这个就是要被SimpleTrigger定时触发执行的方法
        public void doSimpleTriggerTask(){
            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            System.out.println("doSimpleTriggerTask()定时执行方法:"+sdf.format(new Date()));
        }
        //这个就是要被CronTrigger定时触发执行的方法
        public void doCronTriggerTask(){
            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            System.out.println("doCronTriggerTask()定时执行方法:"+sdf.format(new Date()));
        }
    }

    【cronTriggerTask-spring.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:p="http://www.springframework.org/schema/p"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
        xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
        
        <!-- 注册一个普通的bean -->
        <bean id="quartzTask" class="com.higgin.task.QuartzTask"></bean>
            
        <!-- 1.制定任务信息 -->
        <bean id="myJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
            <!-- 设置执行对象 -->
            <property name="targetObject" ref="quartzTask"></property>
            <!-- 设置执行对象中对应的执行方法 -->
            <property name="targetMethod" value="doCronTriggerTask"></property>
            <!-- 是否可以同步执行:不可同步执行 -->
            <property name="concurrent" value="false"></property>
        </bean>
        
        <!-- 2.制定任务执行时机(任务执行触发器) -->
        <bean id="cronTrigger1" class="org.springframework.scheduling.quartz.CronTriggerBean">
            <!-- 设置任务详细 -->
            <property name="jobDetail" ref="myJobDetail"></property>
            <!-- 设置任务执行时机,cron表达式-->
            <property name="cronExpression" value="0/3 * * * * ?"></property>
        </bean>
        
        
        <bean id="cronTrigger2" class="org.springframework.scheduling.quartz.CronTriggerBean">
            <!-- 设置任务详细 -->
            <property name="jobDetail" ref="myJobDetail"></property>
            <!-- 设置任务执行时机,cron表达式 :秒 分 时 日 月 周 年(可选) -->
            <property name="cronExpression" value="0 21 16 20C * ?"></property>
        </bean>
        
        
        <!-- 3.设置调度工厂 -->
        <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
            <property name="triggers">
                <list>
                    <ref bean="cronTrigger1"/>
    <!--                 <ref bean="cronTrigger2"/> -->
                </list>
            </property>
        </bean>
        
    </beans>

    【CronTriggerTest.java】

    package com.higgin.test;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class CronTriggerTest {
        public static void main(String[] args) {
            ApplicationContext context =new ClassPathXmlApplicationContext("cronTriggerTask-spring.xml");
        }
    }

    【运行结果】

  • 相关阅读:
    jquery 左右滚动插件
    MagicAjax使用及注意事项~!!!AJAX无刷新的DLL文件!!
    android 中播放声音要注意的细节
    JNIEnv*的常用函数详解
    http协议上传数据的 优化 方法
    jni 设计的哲学
    listview动态加载数据,并更新数据列表
    Android HOME键的屏蔽
    Django在pycharm业余版进行创建项目文件
    SQL Server 日常维护查询当前正在执行的语句、死锁、堵塞
  • 原文地址:https://www.cnblogs.com/HigginCui/p/6627212.html
Copyright © 2011-2022 走看看