zoukankan      html  css  js  c++  java
  • spring 10-Spring框架基于Spring Task调度任务

    Spring Task基于配置文件的任务定义

    1、定义任务执行类

    package cn.liang.forget;
    import java.text.SimpleDateFormat;
    public class TaskDemo3{
    	public void task(){
    		System.out.println("【当前日期时间】" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(new java.util.Date()));
    	}
    }
    

    2、修改applicationContext.xml配置文件

    xmlns:task="http://www.springframework.org/schema/task"
    xsi:schemaLocation="http://www.springframework.org/schema/task 
    http://www.springframework.org/schema/task/spring-task-4.3.xsd"
    

    配置间隔触发

    <bean id="TaskDemo3" class="cn.liang.forget.TaskDemo3"/>
    <task:scheduled-tasks>
    	<task:scheduled ref="TaskDemo3" method="task" fixed-rate="2000"/>
    </task:scheduled-tasks>
    

    配置cron定时触发

    <bean id="TaskDemo3" class="cn.liang.forget.TaskDemo3"/>
    <task:scheduled-tasks>
    	<task:scheduled ref="TaskDemo3" method="task" cron="* * * * * ?"/>
    </task:scheduled-tasks>
    

    3、启动Spring容器实现间隔触发任务

    package cn.liang.test;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    public class TaskTest2 {
    	public static void main(String[] args) {
    		ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
    	}
    }
    

    Spring Task基于Annotation的配置

    1、修改applicationContext.xml配置文件

    xmlns:task="http://www.springframework.org/schema/task"
    xsi:schemaLocation="http://www.springframework.org/schema/task 
    http://www.springframework.org/schema/task/spring-task-4.3.xsd"
    
    <task:annotation-driven/>
    <context:annotation-config/>
    <context:component-scan base-package="cn.liang"/>
    

    2、配置间隔触发代码

    package cn.liang.forget;
    import java.text.SimpleDateFormat;
    import org.springframework.scheduling.annotation.Scheduled;
    import org.springframework.stereotype.Component;
    @Component
    public class TaskDemo3{
    	@Scheduled(fixedRate=2000)	
    	public void task(){
    		System.out.println("【当前日期时间】" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(new java.util.Date()));
    	}
    }
    

    3、配置cron定时触发

    package cn.liang.forget;
    import java.text.SimpleDateFormat;
    import org.springframework.scheduling.annotation.Scheduled;
    import org.springframework.stereotype.Component;
    @Component
    public class TaskDemo3{
    	@Scheduled(cron="* * * * * ?")	
    	public void task(){
    		System.out.println("【当前日期时间】" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(new java.util.Date()));
    	}
    }
    

    Spring Task任务调度池

    • 之前都只是实现单个的任务配置,如果是多任务则会按顺序执行
    • 此状态下,所有的任务的执行就像队列一样依次向下执行每一个具体的任务
    • 但是如果其中一个任务执行的时间比较长,容易影响到另外一个任务的执行
    • 因此需要建议建立一个任务调度池
    • 任务调度池使得所有的任务不再是单线程的形式完成,属于多线程的方式

    1、编写测试代码

    任务A代码

    package cn.liang.forget;
    import java.text.SimpleDateFormat;
    import org.springframework.scheduling.annotation.Scheduled;
    @Component
    public class TaskDemo2{
    	@Scheduled(fixedRate=2000) 
    	public void runJob() {
    		System.out.println("【TaskDemoA - 当前日期时间】" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(new java.util.Date()));
    	}
    	
    }
    

    任务B代码

    package cn.liang.forget;
    import java.text.SimpleDateFormat;
    import java.util.concurrent.TimeUnit;
    import org.springframework.scheduling.annotation.Scheduled;
    import org.springframework.stereotype.Component;
    @Component
    public class TaskDemo3{
    	@Scheduled(cron="* * * * * ?")	
    	public void task(){
    		try {
    			TimeUnit.SECONDS.sleep(5);
    		} catch (InterruptedException e) {
    			e.printStackTrace();
    		} 
    		System.out.println("【TaskDemoB - 当前日期时间】" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(new java.util.Date()));	}
    }
    

    2、修改applicationContext.xml配置文件

    <task:scheduler id="schedulerPool" pool-size="20"/>
    

    3、启动Spring容器实现间隔触发任务

    package cn.liang.test;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    public class TaskTest2 {
    	public static void main(String[] args) {
    		ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
    	}
    }
    

    4、输出结果

    【MyTaskA - 当前日期时间】2018-12-04 16:33:17.899
    【MyTaskA - 当前日期时间】2018-12-04 16:33:19.892
    【MyTaskA - 当前日期时间】2018-12-04 16:33:21.889
    【MyTaskB - 当前日期时间】2018-12-04 16:33:23.011
    【MyTaskA - 当前日期时间】2018-12-04 16:33:23.890
    【MyTaskA - 当前日期时间】2018-12-04 16:33:25.889
    【MyTaskA - 当前日期时间】2018-12-04 16:33:27.892
    【MyTaskB - 当前日期时间】2018-12-04 16:33:29.005
    【MyTaskA - 当前日期时间】2018-12-04 16:33:29.888
    【MyTaskA - 当前日期时间】2018-12-04 16:33:31.891
    【MyTaskA - 当前日期时间】2018-12-04 16:33:33.891
    
  • 相关阅读:
    《HTTP权威指南》之HTTP连接管理及对TCP性能的考虑
    TCP/ip
    java scoket http TCP udp
    java ==和equals区别
    Java 流
    android ViewPager,ViewFlipper,ViewFlow实现左右滑动
    java反射详解
    退出清理所有Activity方法
    Android UI 使用Merge,include和 ViewStub--优化(1)
    android resources使用总结
  • 原文地址:https://www.cnblogs.com/liangjingfu/p/10065251.html
Copyright © 2011-2022 走看看