• Spring(十)Spring任务调度


    一、计划任务

    需要定时执行一些计划(定时更新等),这样的计划称之为计划任务

    Spring抽象封装了Java提供的Timer与TimerTask类

    也可以使用拥有更多任务计划功能的Quartz

    二、TimerTask

    2.1、继承TimerTask类重写run方法

     

    实现类

    package com.pb.task.timertask;
    
    import java.util.Iterator;
    import java.util.List;
    import java.util.TimerTask;
    
    public class BatchUpdate extends TimerTask {
        //存放SQL
        private List commons;
        @Override
        public void run() {
            //输出语句
            for(Iterator it=commons.iterator();it.hasNext();){
                System.out.println(it.next());
            }
            System.out.println("timertask批量更新完毕");
        }
        public void setCommons(List commons) {
            this.commons = commons;
        }
    
        
        
    }

    配置文件:

    <?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"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
    
    <bean id="batch_update" class="com.pb.task.timertask.BatchUpdate">
    <property name="commons">
    <list>
    <value>update personn set onduty="出勤" where date=${ontime}</value>
    <value>update personn set onduty="缺勤" where date=${miss}</value>
    <value>update personn set onduty="迟到" where date=${late}</value>
    <value>update personn set onduty="早退" where date=${early}</value>
    </list>
    </property>
    </bean>
    
    <!-- 配置任务调度的类 -->
    <bean id="update_schuleTask" class="org.springframework.scheduling.timer.ScheduledTimerTask">
    <property name="timerTask" ref="batch_update"/>
    <!--启动任务后多久开始执行  -->
    <property name="delay">
    <value>1000</value>
    </property>
    <!--第一次记动后后多久执行一次  2秒重复一次-->
    <property name="period">
    <value>2000</value>
    </property>
    </bean>
    <!--启动任务  -->
    <bean id="timerFactory" class="org.springframework.scheduling.timer.TimerFactoryBean">
    <!--任务调度类  -->
    <property name="scheduledTimerTasks">
    <list>
    <ref local="update_schuleTask"/>
    </list>
    </property>
    </bean>
    </beans>

    测试类只需要调用

    ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");

    2.2、使用Spring提供的MethodInvokingTimerTaskFactoryBean定义计划任务

    package com.pb.task.timertask;
    
    import java.util.Iterator;
    import java.util.List;
    import java.util.TimerTask;
    
    public class MethodInvokingBatchUpdate {
        //存放SQL
        private List commons;
        
        public void run() {
            //输出语句
            for(Iterator it=commons.iterator();it.hasNext();){
                System.out.println(it.next());
            }
            System.out.println("MethodInvoking批量更新完毕");
        }
        public void setCommons(List commons) {
            this.commons = commons;
        }
    
        
        
    }

    配置文件

    <?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"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
    
    <bean id="batch_update" class="com.pb.task.timertask.MethodInvokingBatchUpdate">
    <property name="commons">
    <list>
    <value>update personn set onduty="出勤" where date=${ontime}</value>
    <value>update personn set onduty="缺勤" where date=${miss}</value>
    <value>update personn set onduty="迟到" where date=${late}</value>
    <value>update personn set onduty="早退" where date=${early}</value>
    </list>
    </property>
    </bean>
    <!--配置方法的调用类  -->
    <bean id="methodInvoking" class="org.springframework.scheduling.timer.MethodInvokingTimerTaskFactoryBean">
    <property name="targetObject" ref="batch_update"></property>
    <property name="targetMethod" value="run"/>
    
    </bean>
    
    <!-- 配置任务调度的类 -->
    <bean id="update_schuleTask" class="org.springframework.scheduling.timer.ScheduledTimerTask">
    <property name="timerTask" ref="methodInvoking"/>
    <!--启动任务后多久开始执行  -->
    <property name="delay">
    <value>1000</value>
    </property>
    <!--第一次记动后后多久执行一次  2秒重复一次-->
    <property name="period">
    <value>2000</value>
    </property>
    </bean>
    <!--启动任务  -->
    <bean id="timerFactory" class="org.springframework.scheduling.timer.TimerFactoryBean">
    <!--任务调度类  -->
    <property name="scheduledTimerTasks">
    <list>
    <ref local="update_schuleTask"/>
    </list>
    </property>
    </bean>
    </beans>

    三、QuartzJobBean实现

    传统QuartzJob

    需要jta.jar 和quartz-all-1.6.0.jar2个jar包

    package com.pb.quartz.job;
    
    import java.util.Date;
    
    import org.quartz.JobExecutionContext;
    import org.quartz.JobExecutionException;
    import org.springframework.scheduling.quartz.QuartzJobBean;
    
    /**
     * 传统的使用Quartz
     *
     */
    public  class QuartzUpdate extends QuartzJobBean {
        private String command;
        
        @Override
        protected void executeInternal(JobExecutionContext context)
                throws JobExecutionException {
            System.out.println(new Date()+":"+"传统Quartz任务被高度");
            for (int i = 1; i <= 10; i++) {
                System.out.println("命令:"+command+"第"+i+"次被谳用");
                
            }
            System.out.println(new Date()+"传统Quartz调度完成");
        }
        public void setCommand(String command) {
            this.command = command;
        }
    
        
    
    }

    配置文件

    <?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"
        xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
    
    <!--配置传统方式创建Quartz  -->
    <bean id="t_quartz" class="org.springframework.scheduling.quartz.JobDetailBean">
    <!--配置哪个类  -->
    <property name="jobClass">
    <!--关联  -->
    <value>com.pb.quartz.job.QuartzUpdate</value>
    </property>
    
    <!-- 赋值-->
    <property name="jobDataAsMap">
    <map>
    <entry key="command">
    <value>更新</value>
    </entry>
    </map>
    </property>
    </bean>
    
    <!--配置触发器简单触发器  -->
    <bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
    <property name="jobDetail" ref="t_quartz"/>
    <!--启动时间  -->
    <property name="startDelay">
    <value>1000</value>
    </property>
    <!--间隔  -->
    <property name="repeatInterval">
    <value>2000</value>
    </property>
    </bean>
    <!--启动任务  -->
    <bean id="quartzFactory" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
    <property name="triggers">
    <list>
    <ref local="simpleTrigger"/>
    </list>
    </property>
    </bean>
    </beans>

    使用MethodInvoking方式

    package com.pb.quartz.methodinvoking.update;
    
    public class UpdateQuartzJob {
        
        private String command;
        
        public void show(){
            System.out.println("MethodInvoking方式调度开始"+command);
            for (int i = 1; i <=10; i++) {
                System.out.println("命令:"+command+"第"+i+"次调用");
            }
            System.out.println("MethodInvoking方式调度结束"+command);
        }
    
        public String getCommand() {
            return command;
        }
    
        public void setCommand(String command) {
            this.command = command;
        }
        
    
    }

    配置文件

    <?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"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
    <!-- 执行的Bean -->
    <bean id="updateQuartzJob" class="com.pb.quartz.methodinvoking.update.UpdateQuartzJob">
    <property name="command">
    <value>Spring新型更新或者插入</value>
    </property>
    </bean>
    <!-- 新的通过方式调用的 -->
    <bean id="methodInvoking" class="    org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
    <!--哪个类要被调用  -->
    <property name="targetObject" ref="updateQuartzJob"/>
    <!--哪个方法要执行  -->
    <property name="targetMethod" value="show"/>
    </bean>
    <!--配置触发器定时触发器  -->
    <bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
    <property name="jobDetail" ref="methodInvoking"/>
    <!--表达式  -->
    <property name="cronExpression">
    <!-- 每天23:17:01秒调用 -->
    <value>02 17 23 * * ?</value>
    </property>
    </bean>
    
    <!--启动任  -->
    <bean id="quartzFactory" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
    <!--触发器名字  -->
    <property name="triggers">
    <list>
    <ref local="cronTrigger" />
    </list>
    </property>
    </bean>
    
    
    </beans>
  • 相关阅读:
    jQuery 1.6 正式版发布
    EXT.NET Toolbar GridPanel自动宽度和高度的解决方案,引入Viewport
    sql server 2005 数据库状态 变成 可疑的解决方案
    将远程图片读取到本地,并保存
    ^M 替换 VI
    php ctags
    闲来无聊,想了下秒杀、抢购实现方法
    mysql 'OR','IN',‘union’效率分析
    js 全选
    yii rule
  • 原文地址:https://www.cnblogs.com/liunanjava/p/4419913.html
Copyright © 2020-2023  润新知