在 Tomcat 6 中自动执行石英计划作业



我能够使用Quartz 2 Scheduler在Java中调度程序。每当 Apache 服务器启动时,我都需要启动该调度程序。怎么做?

有几种方法可以做到这一点。您可以使用带有静态初始值设定项块的纯 Java 类来初始化 Quartz 计时器。如果你想用JavaEE的方式做到这一点,那么你可以使用EJB3.x或Servlets。

使用 EJB3.x 单例的示例-

import javax.annotation.PostConstruct;
import javax.ejb.Singleton;
import javax.ejb.Startup;
@Startup
@Singleton
public class QuartzTimerBean{
    @PostConstruct
    public void init() {
            ...
            // Start Quartz timer here
            ...
    }
    @PreDestroy
    public void cleanup(){
            ...
            // Clean up Quartz timer
            ...
    }
}

使用 ServletContextListener 的示例

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
public class QuartzTimerListener implements ServletContextListener{
@Override
public void contextInitialized(ServletContextEvent arg0) {
            ...
            // Start Quartz timer here
            ...
}
@Override
public void contextDestroyed(ServletContextEvent arg0) {
            ...
            // Clean up Quartz timer
            ...
}
}

网络.xml

<web-app ...>
   <listener>
       <listener-class><fully qualified path>.QuartzTimerListener</listener-class>
   </listener>
</web-app>

最新更新