1.web.xml文件的配置
<!-- 该监听器主要定时任务(Quartz)、OpenSessionInViewFilter等引起的内存泄露 -->
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>
2.timer-spring文件配置
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"><beans default-lazy-init="true">
<bean id="tokenHandlerJobDetail" class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
<property name="jobClass" value="com.huaxia.webservice.telclient.TelTokenTask" ></property>
<property name="durability" value="true" />
</bean>
<bean id="tokenHandlerTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail" ref="tokenHandlerJobDetail" />
<property name="cronExpression">
<value>0 0/15 * * * ?</value>
<!-- <value>40 * * * * ? *</value>-->
</property>
</bean>
<!-- 电信接口的更新token的定时任务的专用开关 -->
<bean id="tokenHandlerSchedule"
class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="schedulerName">
<value>tokenHandlerSchedule</value>
</property>
<property name="triggers">
<list>
<ref bean="tokenHandlerTrigger" />
</list>
</property>
<property name="autoStartup">
<value>true</value>
</property>
</bean>
<!--
<bean id="enterApiOffJobDetail" class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
<property name="jobClass" value="com.huaxia.thread.EnterApiOffTask" ></property>
<property name="durability" value="true" />
</bean>
-->
<bean id="enterApiService" class="com.huaxia.custom.service.impl.EnterApiServiceImpl" >
</bean>
<bean id="enterApiOffJobDetail" class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
<property name="jobClass" value="com.huaxia.thread.EnterApiOffTask" ></property>
<property name="durability" value="true" />
<property name="jobDataAsMap">
<map>
<entry key="enterApiService" value-ref="enterApiService" >
</entry>
</map>
</property>
</bean>
<bean id="enterApiOffTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail" ref="enterApiOffJobDetail" />
<property name="cronExpression">
<value>1 0 * * * ?</value>
<!-- <value>40 * * * * ? *</value>-->
</property>
</bean>
<!-- 定时任务开关 -->
<bean id="mainSchedule"
class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="schedulerName">
<value>mainSchedule</value>
</property>
<property name="triggers">
<list>
<ref bean="enterApiOffTrigger" />
</list>
</property>
<property name="autoStartup">
<value>true</value>
</property>
</bean>
</beans>
3.TelTokenTask(实现类)
public class TelTokenTask extends QuartzJobBean implements org.quartz.Job{ //只有选其中一种就可,或者什么都不要,自己写方法实现就行
private static final Logger LOGGER = LogManager.getLogger(TelTokenTask.class);
@Override
protected void executeInternal(JobExecutionContext arg0) throws JobExecutionException {
// 若刚在10秒内更新的token则更新token的任务延迟10秒再执行
if(null!=TokenManager.getInstance().getUpdateTime() ){
boolean authCodeOverdue=(new Date().getTime()-TokenManager.getInstance().getUpdateTime())<=10*1000;
if(authCodeOverdue){
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
LOGGER.error(e);
}
}
}
TokenHandler handler=new TokenHandler();
handler.run();
}
}
private static final Logger LOGGER = LogManager.getLogger(TelTokenTask.class);
@Override
protected void executeInternal(JobExecutionContext arg0) throws JobExecutionException {
// 若刚在10秒内更新的token则更新token的任务延迟10秒再执行
if(null!=TokenManager.getInstance().getUpdateTime() ){
boolean authCodeOverdue=(new Date().getTime()-TokenManager.getInstance().getUpdateTime())<=10*1000;
if(authCodeOverdue){
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
LOGGER.error(e);
}
}
}
TokenHandler handler=new TokenHandler();
handler.run();
}
}