天天看點

使用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完成定時排程案例

繼續閱讀