Hi James,

Thank you for your quick response. I'm still fairly new to the async stuff.
I fixed the ChatImpl as suggested to implement Chat instead of
Chat.Callback. I also added a 5 secs delay in the method hello().

There is still something missing, I can't really see the non-blocking
(async) part from Netty implementation. Please take a look at the
AvroClient.java code below, I understand when the client.hello() gets
called, this is the synchronous (blocking) part of the code. It blocked for
5 seconds as expected. Now, when I'm testing the async method by creating
future1 then pass it in client.hello(future1), this method also blocks for
5 seconds. I do not know how to implement the async part properly.

I appreciate the link you provided but it will take me sometime to digest
your sample code. In the mean time, it would be great if you can set me
straight by explaining why the async method is also blocking.

Thanks,

William


=======================
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);

            System.out.println(client.hello()); //This should block
for 5 seconds
            System.out.println("This should print out 5 seconds later");

            final CallFuture<CharSequence> future1 = new
CallFuture<CharSequence>();            client.hello(future1); //This
should not block.            System.out.println("This should print out
immediately");            System.out.println(future1.get());

            transceiver.close();

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



===========
ChatImpl.java
===========

public class ChatImpl implements Chat {
    @Override
    public CharSequence hello() throws AvroRemoteException {        try {
            Thread.sleep(5000);
        } catch (InterruptedException ex) {}        return new
Utf8("Hello");        }}







On Wed, Feb 1, 2012 at 11:09 AM, James Baldassari <[email protected]>wrote:

> Hi William,
>
> The documentation around the async interface is definitely lacking.  There
> should probably be a separate page on the Avro site for that.  I'll try to
> find some time to work on it.  In the meantime you can see some examples I
> put up on github:https://github.com/jbaldassari/Avro-RPC
>
> As for the problem you're having, there are no major issues with your
> code.  The only thing wrong is that the server side (ChatImpl) should
> implement Chat, not Chat.Callback.  One of the nice things about the async
> interface is that it only affects the client side of the RPC; the server
> doesn't have to have any knowledge that it's async.  So the server
> implements the regular sync interface (Chat), and then the client is free
> to use either the sync or async version when invoking RPCs.  Does that
> answer your question?
>
> -James
>
>
>
> On Tue, Jan 31, 2012 at 8:50 PM, William Afendy <[email protected]>wrote:
>
>> Hi,
>>
>> I'm trying to implement Asynchronous calls by using 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 or correct my logic. Below are the codes I use to perform a simple
>> asynchronous 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");
>>     }
>> }
>>
>> =============================================
>> Chat.java - This interface is auto-generated by avro-tool
>> =============================================
>>
>> @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"
>>       }
>>   }
>> }
>>
>>
>> Thanks,
>>
>> --
>> William Afendy
>>
>
>


-- 
William Afendy

Reply via email to