【问题标题】:Assign a name to thread on request arrival在请求到达时为线程分配名称
【发布时间】:2014-01-21 23:30:14
【问题描述】:

我有一个 JEE7 webapp,它使用 jax-ws 注释来定义一些服务,并使用 spring 框架。

我想为在请求到达时提供服务的线程指定一个选择名称。

我考虑过将 Thread.currentThread().setName("") 放入每个 @path 注释的方法中,如果我将这一行放入每个方法中,这会很好,但是在每个方法中添加相同的代码有点没有意义。我想把它放在更早的阶段。

是否有某种“@onrequest”注解来定义所有请求通用的代码?

提前致谢。

【问题讨论】:

    标签: spring spring-security annotations request jax-ws


    【解决方案1】:

    我用过滤器来解决这个问题。我刚刚在 web.xml 中声明了一个新的过滤器,然后我实现了过滤器类。

    在 web.xml 中:

    <filter>
        <filter-name>threadRenamingFilter</filter-name>
        <filter-class>RestRequestFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>threadRenamingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    

    在过滤器类中:

    import java.io.IOException;
    import java.util.Date;
    
    import javax.servlet.Filter;
    import javax.servlet.FilterChain;
    import javax.servlet.FilterConfig;
    import javax.servlet.ServletException;
    import javax.servlet.ServletRequest;
    import javax.servlet.ServletResponse;
    
    import org.apache.commons.logging.Log;
    import org.apache.commons.logging.LogFactory;
    
    public class RestRequestFilter implements Filter {
    
        private final Log log = LogFactory.getLog(this.getClass());
    
        @Override
        public void destroy() {
        }
    
        @Override
        public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2) throws IOException, ServletException {
            //Before request execution
            Date now = new Date();
            Long startTime = now.getTime();
            String newName = startTime.toString();
            log.trace("Thread " + newName + " started at: " + now.toString());
            //renaming of the thread with the time of spawn
            Thread.currentThread().setName(newName);
            //Request execution
            arg2.doFilter(arg0, arg1);
            //After request execution
            now = new Date();
            Long endTime = now.getTime();
            Long executionTime = endTime - startTime;
            log.trace("Thread " + newName + " end at: " + now.toString());
            log.debug("Thread " + newName + " completed in: " + executionTime + "ms");
        }
    
        @Override
        public void init(FilterConfig arg0) throws ServletException {
        }
    
    }
    

    【讨论】:

      【解决方案2】:

      你可以使用拦截器来处理它,基本上你需要编写一个类来实现: http://docs.spring.io/spring/docs/3.2.x/javadoc-api/org/springframework/web/servlet/HandlerInterceptor.html

      并用弹簧连接它。然后在方法 postHandle 或 preHandle 你做你想做的事; )

      这里有一些教程如何做到这一点: http://viralpatel.net/blogs/spring-mvc-interceptor-example/

      您还可以查看过滤器。

      【讨论】:

      • 我只是在想这个。我一定会尝试让你知道。非常感谢。
      • 我试图将该拦截器添加到 cxf servlet,但它不起作用。我需要 cxf 的东西。
      • 那么也许过滤器会在你的代码有用之前运行?
      • 似乎拦截器可以绑定到托管类,但其余类不是托管类。您是否有任何应用于 REST(cxf 或 jersey 不重要)类的拦截器示例?我希望拦截器在调用 cxf/jersey 和安全层之前拦截调用。我需要它来记录其余请求的整个性能并更改线程名称以进行跟踪。
      • 我不知道。但是我为 spring mvc 控制器使用拦截器。如果您使用的是弹簧和弹簧安全,那么拦截器对您来说运行得太晚了——在弹簧安全逻辑之后。在这种情况下,您可能需要实现过滤器,它将在 Spring 侦听器之前注入。你的项目中有 web.xml 吗?如果是这样,那么它应该很容易设置。
      猜你喜欢
      • 2018-03-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-27
      • 1970-01-01
      • 1970-01-01
      • 2011-06-26
      • 2018-10-31
      相关资源
      最近更新 更多