Quartz调度程序在struts2中运行两次

时间:2013-12-02 02:01:21

标签: java struts2 quartz-scheduler

我在Struts2 Web应用程序中使用Quartz调度程序安排了一个JOB。一切都按照我的预期正常工作,但触发器触发两次:在指定的时间。经过一些研究后,我发现ApplicationContext正在加载两次。我发现这些链接很有用。 Job Triggered twice Application Context loading Twice但这不是那么具有描述性,或者我无法理解。请帮忙解决这个问题。

将在请求时提供代码。

我的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>MyAPP</display-name>
  <welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
  <filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>
  <filter-mapping>
     <filter-name>struts2</filter-name>
   <url-pattern>/*</url-pattern>
     </filter-mapping>
     <listener>
         <listener-class>
           org.apache.struts2.tiles.StrutsTilesListener
     </listener-class>
    </listener>
   <context-param>
      <param-name>tilesDefinitions</param-name>
      <param-value>/WEB-INF/tiles.xml</param-value>
   </context-param>
  <welcome-file-list>
     <welcome-file>Login.jsp</welcome-file>
  </welcome-file-list>
     <session-config>
<session-timeout>30</session-timeout>
  </session-config>
    <error-page>
    <error-code>404</error-code>
    <location>/jsp/error/pagenotfound.jsp</location>
  </error-page>
 <listener>
    <listener-class>
         com.MYAPP.listners.QuartzListners
    </listener-class>
 </listener>
</web-app>

我的QuartzListner.java

public class QuartzListner implements ServletContextListener {
private static final Logger log=Logger.getLogger(QuartzListner.class);

/* (non-Javadoc)
 * @see javax.servlet.ServletContextListener#contextDestroyed(javax.servlet.ServletContextEvent)
 */
@Override
public void contextDestroyed(ServletContextEvent arg0) {
    // TODO Auto-generated method stub

    log.info("----Quartz Scheduler destroyer Intialized----");
     try{  
      ServletContext context =arg0.getServletContext();
      Scheduler scheduler =(Scheduler)context.getAttribute("DocRevScheduler");
       scheduler.shutdown(true);

       log.info("----Scheduler Shutting downnn ---- "+scheduler.isShutdown()); 
     }catch(Exception se)
     {
         log.warn("--Exception while Shutdowning the Scheduler..."+se);
         se.printStackTrace();
     }


}

/* (non-Javadoc)
 * @see javax.servlet.ServletContextListener#contextInitialized(javax.servlet.ServletContextEvent)
 */
@Override
public void contextInitialized(ServletContextEvent arg0) {
    // TODO Auto-generated method stub


    log.info("--Qurz Scheduler intialized----");

    JobDetail job = JobBuilder.newJob(MailSendingJOB.class)
            .withIdentity("mailJob", "group1").build();
    JobDetail jobDetail =JobBuilder.newJob(DBOperationsJOB.class)
            .withIdentity("DBJOB", "group2").build();


        try {

            ResourceBundle resource = ResourceBundle.getBundle("resources.application");


            Trigger trigger = TriggerBuilder
              .newTrigger()
              .withIdentity("anyTriggerName", "group1")
              .withSchedule(
                 CronScheduleBuilder.cronSchedule(resource.getString("triggerTime")))
              .build();

            Trigger trigger1 = TriggerBuilder
                      .newTrigger()
                      .withIdentity("sampleTrigger", "group2")
                      .withSchedule(
                         CronScheduleBuilder.cronSchedule(resource.getString("triggetTime")))
                      .build();

            Scheduler scheduler = new StdSchedulerFactory().getScheduler();
            scheduler.start();
            scheduler.scheduleJob(job, trigger);
            scheduler.scheduleJob(jobDetail, trigger1);

            ServletContext context = arg0.getServletContext();
            context.setAttribute("MyJOBScheduler", scheduler);


        } catch (SchedulerException e) {
            e.printStackTrace();

        }

}

}

在我的属性文件triggerTime=0 19 11 ? * MON *

0 个答案:

没有答案