- A lower level class A that extends SimpleChannelUpstreamHandler. This class is the workhorse to send the message and received the response.
A top level class B that can be used by other part of the system to send and receive message (can simulate both Synchronous and Asynchronous). This class creates the ClientBootstrap, set the pipeline factory, invoke the bootstrap.connect() and eventually get a handle/reference of the class A through which to be used to send and receive message. Something like:
ChannelFuture future = bootstrap.connect(); Channel channel = future.awaitUninterruptibly().getChannel();
A handler = channel.getPipeline().get(A.class);
我知道在A类中,我可以重写public void channelClosed(ChannelHandlerContext ctx, ChannelStateEvent e)方法,这样当远程服务器挂掉时,我就能得到通知。
由于在通道关闭后,在B类中的原始A类引用(上面的处理程序)将不再有效,因此我需要用新引用替换它。
理想情况下,我希望A类有一个机制,在上述覆盖的channelClosed方法中通知B类,以便可以在B类中再次调用bootstrap.connect。实现这一点的一种方法是在A类中引用B类。为此,我需要将B类的引用传递给PipelineFactory,然后由PipelineFactory将B的引用传递给A。
是否有其他更简单的方法来实现同样的效果?
谢谢,