<?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:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<!-- 定时任务 下游回调任务-->
<!-- ======================== 1、定义需要执行的任务类。 ======================== -->
<bean id="notifyQuartz" class="action.NotifyTimerAction">
</bean>
<bean id="autoSendMailQuartz1"
class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="notifyQuartz" />
<property name="targetMethod" value="remoteNotify" />
<property name="concurrent" value="false" />
</bean>
<bean id="notifyQuartztar" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail" ref="autoSendMailQuartz1" />
<property name="cronExpression">
<!-- 每10秒钟执行一次 -->
<value>*/10 * * * * ?</value>
</property>
</bean>
<!-- ======================== 4、调度工厂 ======================== -->
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="notifyTrigger"/>
</list>
</property>
</bean>
<bean id="threadPoolTaskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
<!-- 线程池维护线程的最少数量 -->
<property name="corePoolSize" value="10" />
<!-- 线程池维护线程所允许的空闲时间 -->
<property name="keepAliveSeconds" value="200" />
<!-- 线程池维护线程的最大数量 -->
<property name="maxPoolSize" value="50" />
<!-- 线程池所使用的缓冲队列 -->
<property name="queueCapacity" value="100" />
<property name="rejectedExecutionHandler">
<!-- AbortPolicy:直接抛出java.util.concurrent.RejectedExecutionException异常 -->
<!-- CallerRunsPolicy:主线程直接执行该任务,执行完之后尝试添加下一个任务到线程池中,可以有效降低向线程池内添加任务的速度 -->
<!-- DiscardOldestPolicy:抛弃旧的任务、暂不支持;会导致被丢弃的任务无法再次被执行 -->
<!-- DiscardPolicy:抛弃当前任务、暂不支持;会导致被丢弃的任务无法再次被执行 -->
<bean class="java.util.concurrent.ThreadPoolExecutor$CallerRunsPolicy" />
</property>
</bean>
</beans>
在applicationContext.xml中注入springquartz
<import resource="classpath:spring-quartz.xml" />
控制层代码:
package action;
import org.springframework.stereotype.Controller;
@Controller("notifyTimerAction")
public class NotifyTimerAction {
public void remoteNotify() throws Exception{
System.out.println("test定时任务启动了!!!!!!!!");
System.out.println("进入1111111111111");
}
}
结果: