Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to: Async Callbacks using Netty with Avro

I'm trying to implement Asynchronous Avro calls by using its NettyServer implementation. After digging the source code, I found an example on how to use NettyServer from TestNettyServerWithCallbacks.java

When running a few test, I realize that NettyServer never calls hello(Callback) method, instead it keeps calling the synchronous hello() method. The client program prints out "Hello" but I'm expecting "Hello-ASYNC" as a result. I really have no clue what's going on.

I hope someone can shine some light on me and perhaps point out the mistake. Below are the codes I use to perform a simple asynchronous avro test.

AvroClient.java - Client code.

public class AvroClient {
    public static void main(String[] args) throws InterruptedException, ExecutionException, TimeoutException {
        try {
            NettyTransceiver transceiver = new NettyTransceiver(new InetSocketAddress(6666));
            Chat.Callback client = SpecificRequestor.getClient(Chat.Callback.class, transceiver);

            final CallFuture<CharSequence> future1 = new CallFuture<CharSequence>();
            client.hello(future1);

            System.out.println(future1.get());

            transceiver.close();

        } catch (IOException ex) {
            System.err.println(ex);
        }
    }
}

AvroNetty.java - The Server Code

public class AvroNetty {
    public static void main(String[] args) {
        Index indexImpl = new AsyncIndexImpl();
        Chat chatImpl = new ChatImpl();

        Server server = new NettyServer(new SpecificResponder(Chat.class, chatImpl), new InetSocketAddress(6666));
        server.start();
        System.out.println("Server is listening at port " + server.getPort());
    }
}

ChatImpl.java

public class ChatImpl implements Chat.Callback {
    @Override
    public void hello(org.apache.avro.ipc.Callback<CharSequence> callback) throws IOException {
        callback.handleResult("Hello-ASYNC");    
    }

    @Override
    public CharSequence hello() throws AvroRemoteException {
        return new Utf8("Hello");    
    }
}

This interface is auto-generated by avro-tool Chat.java

@SuppressWarnings("all")
public interface Chat {
    public static final org.apache.avro.Protocol PROTOCOL = org.apache.avro.Protocol.parse("{\"protocol\":\"Chat\",\"namespace\":\"avro.test\",\"types\":[],\"messages\":{\"hello\":{\"request\":[],\"response\":\"string\"}}}");
    java.lang.CharSequence hello() throws org.apache.avro.AvroRemoteException;

    @SuppressWarnings("all")
    public interface Callback extends Chat {
        public static final org.apache.avro.Protocol PROTOCOL = avro.test.Chat.PROTOCOL;
        void hello(org.apache.avro.ipc.Callback<java.lang.CharSequence> callback) throws java.io.IOException;
    }
}

Here is the Avro Schema

{
  "namespace": "avro.test",
  "protocol": "Chat",

  "types" : [],

  "messages": {
      "hello": { 
                    "request": [],
                    "response": "string"
      }
  }
}
like image 958
carbotex Avatar asked Jan 23 '26 10:01

carbotex


1 Answers

The NettyServer implementation actually doesn't implement the Async style at all. It is a deficiency in the library. Instead you need to specify an asynchronous execution handler rather than try and chain services together through callbacks. Here is what I use to setup my NettyServer to allow for this:

ExecutorService es = Executors.newCachedThreadPool();
OrderedMemoryAwareThreadPoolExecutor executor = new OrderedMemoryAwareThreadPoolExecutor(Runtime.getRuntime().availableProcessors(), 0, 0);
ExecutionHandler executionHandler = new ExecutionHandler(executor);
final NettyServer server = new NettyServer(responder, addr, new NioServerSocketChannelFactory(es, es), executionHandler);
like image 184
Sam Pullara Avatar answered Jan 25 '26 08:01

Sam Pullara



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!