第一种方法通过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()+"-------------");
}
}