使用Web应用程序启动/停止侦听器

时间:2014-04-04 03:25:43

标签: java java-ee servletcontextlistener

我在应用程序启动/停止时使用以下代码启动/停止监听器。当进程启动时,根据用户请求,它需要获取OoProcessor的单例实例来执行某些操作。但它错误地抛出错误,即OoProcessor即使启动也没有启动。这是在webapplication中启动/停止处理器的正确方法吗?

public class OoListener implements ServletContextListener {

    @Override
    public void contextDestroyed(ServletContextEvent event) {
        OoProcessor.destroy();
    }

    @Override
    public void contextInitialized(ServletContextEvent event) {
        OoProcessor.init();
    }

}


public class OoProcessor  {


    private static final OoProcessor INSTANCE = new OoProcessor();
    private final OManager oManager;


    public OpenOfficeProcessor(){
        ..set configuration
    }
    public static OoProcessor getInstance()
    {
        return INSTANCE;
    }


    protected static void init() {
        LOG.debug("Starting the listener...");
                OoProcessor.getInstance().oManager.start();
    }

    protected static void destroy() {
        LOG.debug("Stopping the listener...");
        OoProcessor.getInstance().oManager.stop();
    }

    public OManager getOManager() {
        return oManager;
    }

Web.xml中

<listener> 
    <listener-class>com.listener.OoListener</listener-class> 
</listener> 

0 个答案:

没有答案