Netty处理程序并不是每次调用都实例化的。

huangapple go评论88阅读模式
英文:

Netty handler is not instantiater for every call

问题

我对新的Handler()实现有一个问题。在处理程序中,我有一个类成员,它使用随机UUID进行初始化。但当我查看数据时,发现有多个具有相同UUID的请求。

public class HttpServerInitializer extends ChannelInitializer<SocketChannel> {
   @Override
   public void initChannel(SocketChannel ch) throws Exception {
       .....
       p.addLast("httprequest", new HttpServerHandler(config));
   }
}

HttpServerHandler类:

private String uuid;

public HttpServerHandler(final StaticConfig staticConfig) {
    this.uuid = UUID.randomUUID().toString();
}

对于每次调用,这个UUID并不唯一。

相反,如果我将random_uuid添加到attr中,就能正常工作。

所以,我确实有一种修复这个问题的方法。问题在于,这与我对处理程序创建方式的理解相冲突。

英文:

I have a problem with new Handler() implementation. I have a class member in the handler which is initialized with a random UUID. When I look at the data, there are multiple requests with the same UUID.

public class HttpServerInitializer extends ChannelInitializer&lt;SocketChannel&gt; {
   @Override
   public void initChannel(SocketChannel ch) throws Exception {
       .....
       p.addLast(&quot;httprequest&quot;, new HttpServerHandler(config));
   }
}

HttpServerHandler

private String uuid;

public HttpServerHandler(final StaticConfig staticConfig) {

    this.uuid = UUID.randomUUID().toString();

}

This uuid is not unique for every call.

Instead if I add random_uuid to attr , it works fine.

So, I do have a way to fix this. The problem is that this conflicts with my understanding of how the handlers are created.

答案1

得分: 2

initChannel(...) 被调用以处理每个新的连接。所以,如果多个请求通过同一连接发送,你将只会看到它一次。

英文:

initChannel(...) is called for each new connection. So if you multiple requests are send over the same connection you will see it once

huangapple
  • 本文由 发表于 2020年8月26日 00:13:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/63583007.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定