天天看点

使用spring scheduler完成定时调度案例

scheduler介绍

设计理念:每一个被调度的任务都会被线程池中的一个线程去执行,因此任务可以并发执行,而且相互之间不受影响。

scheduler案例

1.在spring-task.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:task="http://www.springframework.org/schema/task"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/task
        http://www.springframework.org/schema/task/spring-task.xsd
         http://www.springframework.org/schema/context
          http://www.springframework.org/schema/context/spring-context.xsd">

    <!--com.itxiaobai.app:表示所要运行类的的包名-->
    <context:component-scan base-package="com.itxiaobai.app"></context:component-scan>
    <!--pool-size:表示线程池的个数有100-->
    <task:scheduler id="taskScheduler" pool-size="100"/>
    <task:scheduled-tasks scheduler="taskScheduler">
        <!--ref:表示定时调度的类注入到spring中的名字(首字母小写其余按照驼峰法命名)
            method:实际运行类中的方法
            cron:表示多久运行一次,* * * * * ?:表示每秒运行一次-->
        <task:scheduled ref="task" method="hello" cron="* * * * * ?"/>
        <task:scheduled ref="task" method="word" cron="* * * * * ?"/>
    </task:scheduled-tasks>
</beans>
           

2.将spring-task.xml中的配置引入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:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
         http://www.springframework.org/schema/context/spring-context.xsd">
    
    <!--注入注释扫面com.itxiaobai下的文件-->
    <context:component-scan base-package="com.itxiaobai">
        <!--将不包含controller层的内容注入到Spring里面-->
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller">    </context:exclude-filter>
    </context:component-scan>

    <!--引入定时任务资源-->
    <import resource="spring-task.xml"/>
</beans>
           

3.编写测试类:Task,注意要将此类注入spring中(添加@Component)

package com.itxiaobai.app;

import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component;

/**
 * 用于测试定时调度
 */
@Component
public class Task {

    /**
     * hello:表示定时运行的方法
     */
    public void hello(){
        System.out.println("hello");
    }


    /**
     * word:表示定时运行的方法
     */
    public void word(){
        System.out.println("word");
    }

    /**
     * 测试方法
     * @param args
     */
    public static void main(String[] args) {
        new ClassPathXmlApplicationContext("spring.xml");
    }

}
           

4.运行结果如下

使用spring scheduler完成定时调度案例

继续阅读