本文介绍了Netty - 我可以在Hash map中缓存那些ChannelHandlerContext并在以后响应它吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是实施步骤和问题:

Here is the step of implementation and questions:


  1. Netty服务器处理程序从客户端接收消息

  1. Netty server handler receives message from client

因为还有其他正在运行的线程来处理用户数据,我们需要将此请求放入队列并让工作进程处理

Because there are other running threads to process the user data, we need to put this request to a queue and let the worker process

在工作者处理数据之后,我们可以在工作线程中回复客户端吗?即使用HashMap缓存ChannelHandlerContext并稍后从工作线程获取响应?

After the worker process the data, can we response back to client in the worker thread? i.e. either using a HashMap to cache the ChannelHandlerContext and get it later from worker thread to response?

谢谢大家

推荐答案

是的,你可以。 ChannelHandlerContext 提供的操作都是线程安全的,因此您可以保留上下文实例供以后使用,并从其他线程使用它。

Yes, you can. The operations provided by ChannelHandlerContext are all thread-safe, and thus you can keep the context instance for later use and use it from other thread.

A ChannelHandlerContext 与它所属的频道具有相同的生命周期。当频道关闭时, ChannelHandlerContext 也会从管道中解除引用。如果你保留对 ChannelHandlerContext 的引用,你应该取消引用它或确保垃圾回收器可以回收它。

A ChannelHandlerContext has the same life cycle with the Channel it belongs to. When the Channel is closed, ChannelHandlerContext is also dereferenced from the pipeline. If you keep the reference to a ChannelHandlerContext, you should dereference it or make sure the garbage collector can reclaim it.

这篇关于Netty - 我可以在Hash map中缓存那些ChannelHandlerContext并在以后响应它吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-16 21:26