一,需要的依赖
<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-core</artifactId>
<version>4.1.6.RELEASE</version>
</dependency>
二,定时任务类
package demo.springintegration;
/**
* 定时任务类
* @author xiaohui
*
*/
public class TimedTask {
public String test() {
System.out.println("hello world!");
return "";
}
}
三,配置文件
<?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:int="http://www.springframework.org/schema/integration"
xmlns:int-jdbc="http://www.springframework.org/schema/integration/jdbc"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
http://www.springframework.org/schema/integration/jdbc http://www.springframework.org/schema/integration/jdbc/spring-integration-jdbc.xsd
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">
<!-- 执行的次数(次数=capacity+1) -->
<int:channel id="count">
<int:queue capacity="1000"/>
</int:channel>
<!-- 要执行的类 -->
<bean id="hello" class="demo.springintegration.TimedTask"/>
<!-- 指定要执行的类的方法及执行的次数 -->
<int:inbound-channel-adapter channel="count" ref="hello" method="test">
<!-- 执行时间 -->
<!-- 每秒执行一次 -->
<int:poller cron="1 * * * * ? " />
<!--
秒(0~59)
分钟(0~59)
小时(0~23)
天(月)(0~31,但是你需要考虑你月的天数)
月(0~11)
星期(1~7 1=SUN 或 SUN,MON,TUE,WED,THU,FRI,SAT)
年份(1970-2099)
特殊符号含义
* :任意值,可以理解(每)
? :只能在 天和星期中用。其中有一个必须为?(避免冲突)
- :表示范围 如 1-10 如:* * * * ? 表示每分钟1-10秒每秒触发
/ :从多少开始每隔多少 如: 0/1 * * * * ? 表示从0开始每秒触发
, :表示指定的 如:2,4,6 * * * * ? 表示每分钟第2第4第6秒触发
L :表示最后 只能在星期和月使用,如:在星期中使用5L表示最后一个星期四触发
W :表示有效工作日(周一到周五),只能出现在月中,系统将在离指定日期的最近的有效工作日触发事件
LW :这两个字符可以连用,表示在某个月最后一个工作日,即最后一个星期五。
# :用于确定每个月第几个星期几,只能出现在月中。例如在4#2,表示某月的第二个星期三。
-->
</int:inbound-channel-adapter>
</beans>
四,cron表达式
如 * * * * * ? 2018 按顺序表示
秒(0~59)
分钟(0~59)
小时(0~23)
天(月)(0~31,但是你需要考虑你月的天数)
月(0~11)
星期(1~7 1=SUN 或 SUN,MON,TUE,WED,THU,FRI,SAT)
年份(1970-2099)(可)
特殊符号含义
* :任意值,可以理解(每)
? :只能在 天和星期中用。其中有一个必须为?(避免冲突)
- :表示范围 如 1-10 如:* * * * ? 表示每分钟1-10秒每秒触发
/ :从多少开始每隔多少 如: 0/1 * * * * ? 表示从0开始每秒触发
, :表示指定的 如:2,4,6 * * * * ? 表示每分钟第2第4第6秒触发
L :表示最后 只能在星期和月使用,如:在星期中使用5L表示最后一个星期四触发
W :表示有效工作日(周一到周五),只能出现在月中,系统将在离指定日期的最近的有效工作日触发事件
LW :这两个字符可以连用,表示在某个月最后一个工作日,即最后一个星期五。
# :用于确定每个月第几个星期几,只能出现在月中。例如在4#2,表示某月的第二个星期三。