spring quartz 初級配置

第一種方法通過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-4.0.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">
    <!-- job 的包 加載定時任務實現類-->
    <context:component-scan base-package="com.galaxy.fym.job"></context:component-scan>
    <!-- 任務調度器配置 配置任務線性池 pool-size
    指配的一個scheduled-tasks中所有的運行方法的線程總數
    不同的scheduled-tasks配置了相同的task:scheduler會有相同的總數限制,而不是和的限制-->
    <task:scheduler id="scheduler" pool-size="2" />
    <!-- 指定運行的方法和運行時間規則時間
    task:scheduler/@pool-size:調度線程池的大小,調度線程在被調度任務完成前不會空閒
    task:scheduled/@cron:cron表達式,注意,若上次任務未完成,即使到了下一次調度時間,任務也不會重複調度 -->
    <task:scheduled-tasks scheduler="scheduler">
        <!-- 一秒一次 -->
        <task:scheduled ref="jobTest" method="jobPrint" cron="*/1 * * * * ?"/>
        <task:scheduled ref="jobTest" method="jobPrint2" cron="*/1 * * * * ?"/>
    </task:scheduled-tasks>
    <task:scheduled-tasks scheduler="scheduler">
        <task:scheduled ref="job2Test" method="jobPrint" cron="*/1 * * * * ?"/>
        <task:scheduled ref="job2Test" method="jobPrint2" cron="*/1 * * * * ?"/>
    </task:scheduled-tasks>
</beans>
package com.galaxy.fym.job;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * Created by fengyiming on 2016/6/24.
 */
@Service
public class JobTest {

    private static Logger logger = LoggerFactory.getLogger(JobTest.class);

    public static void out(){
        logger.debug("JobTest debug");
        logger.info("JobTest info");
        logger.warn("JobTest warm");
        logger.error("JobTest error");
    }

    public void jobPrint() throws Exception{
        System.out.println("--------Job1111Test jobPrint11111 thread time:" + new Date().getTime() + "-------------");
        Thread.sleep(2000l);
        System.out.println("--------Job1111Test jobPrint11111 thread time:" + new Date().getTime() + "-------------");
    }

    public void jobPrint2(){
        System.out.println("--------Job1111Test jobPrint22222 time:" + new Date().getTime()+"-------------");
    }
}
package com.galaxy.fym.job;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * Created by fengyiming on 2016/6/24.
 */
@Service
public class Job2Test {

    private static Logger logger = LoggerFactory.getLogger(JobTest.class);

    public static void out(){
        logger.debug("Job2Test debug");
        logger.info("Job2Test info");
        logger.warn("Job2Test warm");
        logger.error("Job2Test error");
    }

    public void jobPrint() throws Exception{
        System.out.println("--------Job2222Test jobPrint11111 thread time:" + new Date().getTime() + "-------------");
        Thread.sleep(2000l);
        System.out.println("--------Job2222Test jobPrint11111 thread time:" + new Date().getTime() + "-------------");
    }

    public void jobPrint2(){
        System.out.println("--------Job2222Test jobPrint22222 time:" + new Date().getTime()+"-------------");
    }
}

第二種方法通過自動讀取註解管理定時任務,這種方法不利於管理,因爲配置都在各個類中,無法統一管理

    <!-- job 的包 加載定時任務實現類-->
    <context:component-scan base-package="com.galaxy.fym.job"></context:component-scan>
    <!-- 第二種方法 start -->
    <!-- Spring定時器註解開關-->
    <task:annotation-driven executor="executor" scheduler="scheduler" proxy-target-class="true"></task:annotation-driven>
    <task:scheduler id="scheduler" pool-size="2" />
    <!--
    task:executor/@pool-size:可以指定執行線程池的初始大小、最大大小
    task:executor/@queue-capacity:等待執行的任務隊列的容量
    task:executor/@rejection-policy:當等待隊列爆了時的策略,分爲丟棄、由任務執行器直接運行等方式
    ABORT 終止; CALLER_RUNS 調用運行; DISCARD丟棄;DISCARD_OLDEST 丟棄最老的-->
    <task:executor id="executor" pool-size="100-200" keep-alive="3600" queue-capacity="500" rejection-policy="CALLER_RUNS"/>
    <!-- 第二種方法 end -->
package com.galaxy.fym.job;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * Created by fengyiming on 2016/6/24.
 */
@Service
public class JobTest {

    private static Logger logger = LoggerFactory.getLogger(JobTest.class);

    public static void out(){
        logger.debug("JobTest debug");
        logger.info("JobTest info");
        logger.warn("JobTest warm");
        logger.error("JobTest error");
    }

    @Scheduled(cron = "*/1 * * * * ?")
    public void jobPrint() throws Exception{
        System.out.println("--------Job1111Test jobPrint11111 thread time:" + new Date().getTime() + "-------------");
        Thread.sleep(2000l);
        System.out.println("--------Job1111Test jobPrint11111 thread time:" + new Date().getTime() + "-------------");
    }

    @Scheduled(cron = "*/1 * * * * ?")
    public void jobPrint2(){
        System.out.println("--------Job1111Test jobPrint22222 time:" + new Date().getTime()+"-------------");
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章