0

Since all the examples I have seen just output to console in the ClientHandler I'm wondering about a proper way to get this data into a specific object. Let's say in some controller class the user can press a button to request or refresh some data from the server. I have a reference to the channel in this object that sends the request to the server and once the server done the work it sends it back where it ends up in the clients pipeline. Now I need to get the data out and into the object that requested it.

I can think of two options. Implement a observer pattern and send the data back to the object once it's complete and decoded. I'll add the requesting object as a listener once it has send the data and remove it once it received it.

Or perhaps I can just implement a handler interface for this controller class? When it expects data I add it to the pipeline and when it has received the data I remove it from the pipeline? Some pseudo:

public class SomeControllerClass extends ChannelInboundHandlerAdapter {

    //...

    public void onButtonClick() {
        // Send a request to the server.
        channel().writeAndFlush(new someDataRequest);
        // Add this controller to the pipeline to handle the response once it arrived and decoded.
        channel().pipeline().addLast("someHandler", this);
    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) {
        // Do stuff with the data in this class
        // Remove this from the pipeline.
        channel().pipeline().remove("someHandler");
    }

    //...
}

Is this anywhere along the lines on how I need to approach this problem? I'm removing it from the pipeline because I'm worried other objects that expect similar data will handle the data too.

In other words, all I'm trying to make is a popup window that shows "loading" until data is received from server and ready to present it in that popup.

Madmenyo
  • 8,389
  • 7
  • 52
  • 99
  • The better approach would be to use the observer pattern. – Riyafa Abdul Hameed Mar 01 '19 at 06:33
  • Would a solution like https://stackoverflow.com/questions/23128232/how-to-get-server-response-with-netty-client/35318079#35318079 work for you (disclaimer: its one of my previous answers) – Ferrybig Mar 01 '19 at 10:14
  • @RiyafaAbdulHameed can you elaborate on why the observer pattern is a better? I haven't seen anything like it in the examples. Isn't there something out of the box that I can do to retrieve a server response in an object outside the handlers? – Madmenyo Mar 01 '19 at 12:15

0 Answers0