Spring的Quartz定时器同一时刻重复执行二次的问题解决
我的解决办法如下:
1.将quartz.xml lazy-init="true"值改成 true;
<bean id="startQuertz" lazy-init="true" autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean" destroy-method="destroy">
<property name="autoStartup" value="true"/>
<property name="triggers">
<list>
<ref local="doTimeSyncThumbnail"/>
<ref local="doTimeJobplanRemind" />
</list>
</property>
</bean>
2.增加AppInitializerListener.java代码如下:
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import net.jweb.security.ldap.job.InitializingCronTrigger;
import org.apache.log4j.Logger;
import org.quartz.impl.StdScheduler;
import org.springframework.web.context.WebApplicationContext;
public class AppInitializerListener implements ServletContextListener {
private Logger logger = Logger.getLogger(this.getClass().getName());
private StdScheduler scheduler = null;
/*
* (non-Javadoc)
*
* @see
* javax.servlet.ServletContextListener#contextInitialized(javax.servlet
* .ServletContextEvent)
*/
@SuppressWarnings("unchecked")
@Override
public void contextInitialized(ServletContextEvent sce) {
try
{
WebApplicationContext context = (WebApplicationContext) sce.getServletContext().getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
ServletContext servletContext = sce.getServletContext();
//启用Quertz
// Should the Scheduler being started now or later
String startOnLoad = servletContext.getInitParameter("quartz:start-scheduler-on-load");
int startDelay = 0;
String startDelayS = servletContext.getInitParameter("quartz:start-delay-seconds");
String ldapOnLoad = servletContext.getInitParameter("start-ldap-on-load");
/**//*
* If the "start-scheduler-on-load" init-parameter is not specified,
* the scheduler will be started. This is to maintain backwards
* compatability.
*/
if (startOnLoad != null && (Boolean.valueOf(startOnLoad).booleanValue())) { //需要启用Quertz
scheduler = (StdScheduler) context.getBean("startQuertz");
try {
if(startDelayS != null && startDelayS.trim().length() > 0)
startDelay = Integer.parseInt(startDelayS);
} catch(Exception e) {
System.out.println("Cannot parse value of 'start-delay-seconds' to an integer: " + startDelayS + ", defaulting to 5 seconds.");
startDelay = 5;
}
System.out.println("Auto Run Scheduler."+startOnLoad);
if(startDelay <= 0) {
// Start now
if(scheduler.isStarted() == false) {
scheduler.start();
System.out.println("Scheduler has been started");
}
}
else {
// Start delayed
if(scheduler.isStarted() == false) {
scheduler.startDelayed(startDelay);
System.out.println("Scheduler will start in " + startDelay + " seconds.");
}
}
InitializingCronTrigger cronTrigger = null;
if(cronTrigger != null)
{
cronTrigger.setStartLoad("true".equalsIgnoreCase(ldapOnLoad));
}
}
}
catch (Exception e)
{
logger.error("启动初始化异常:",e);
}
}
@Override
public void contextDestroyed(ServletContextEvent arg0) {
}
}
3.在web.xml中如下配置:
加在实例化spring容器之后;
<listener>
<listener-class>net.jcenter.listener.AppInitializerListener</listener-class>
<load-on-startup>2</load-on-startup>
</listener>
<context-param>
<param-name>quartz:start-scheduler-on-load</param-name>
<param-value>true</param-value>
</context-param>
<context-param>
<param-name>quartz:start-delay-seconds</param-name>
<param-value>30</param-value>
</context-param>
更多推荐
所有评论(0)