简体   繁体   English

如何以编程方式停止ServletRequestListener?

[英]How to stop ServletRequestListener programmatically?

I am using ServletRequestListener to execute certain logic when receiving requests from a web service java application. 当接收来自Web服务Java应用程序的请求时,我正在使用ServletRequestListener执行某些逻辑。 At some point, i want to stop listening to each request received. 在某个时候,我想停止监听收到的每个请求。 Is there a way to stop or disable the ServletRequestListener listener programmatically? 有没有办法以编程方式停止或禁用ServletRequestListener侦听器?

Whilst it's possible to add a listener programmatically , it is not possible to stop or remove a listener programmatically. 虽然可以通过编程方式添加侦听器 ,但是无法通过编程方式停止或删除侦听器。 Best what you can do is to let it check some application wide variable before continuing and then toggle that application wide variable instead. 最好的办法是让它在继续之前检查某个应用程序范围的变量,然后切换该应用程序范围的变量。

Eg 例如

@Override
public void requestInitialized(ServletRequestEvent event) {
    if (event.getServletContext().getAttribute("disableListener") == Boolean.TRUE) {
        return;
    }

    // Do the original job here.
}

@Override
public void requestDestroyed(ServletRequestEvent event) {
    if (event.getServletContext().getAttribute("disableListener") == Boolean.TRUE) {
        return;
    }

    // Do the original job here.
}

and to disable it, just do 并禁用它,只要做

servletContext.setAttribute("disableListener", true);

and to re-enable it, just do 并重新启用它,只需执行

servletContext.removeAttribute("disableListener");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM