英文:
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<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();
}
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论