Home | About | Sematext search-lucene.com search-hadoop.com
 Search Hadoop and all its subprojects:

Switch to Plain View
Avro, mail # user - Async Client Not Working


+
Mike Machado 2012-12-21, 02:19
+
Doug Cutting 2012-12-21, 17:41
Copy link to this message
-
Re: Async Client Not Working
Mike Machado 2012-12-22, 00:56

> Can you please provide complete code for a test that fails? Thanks!
Here is a single class that builds upon one of the common avro examples out there on the web, showing that async is not working:

package com.example.avrotest1;

import java.io.IOException;
import java.net.InetSocketAddress;

import org.apache.avro.ipc.specific.SpecificRequestor;
import org.apache.avro.ipc.specific.SpecificResponder;
import org.apache.avro.util.Utf8;

import example.proto.Mail;
import example.proto.Message;

import org.apache.avro.ipc.Callback;
import org.apache.avro.ipc.NettyServer;
import org.apache.avro.ipc.NettyTransceiver;
import org.apache.avro.ipc.Server;

/**
 * Start a server, attach a client, and send a message.
 */
public class ExampleProto {
    public static class MailImpl implements Mail {
        // in this simple example just return details of the message
        public Utf8 send(Message message) {
            System.out.println("Sending message");
            try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
            return new Utf8("Sending message to " + message.getTo().toString()
                    + " from " + message.getFrom().toString()
                    + " with body " + message.getBody().toString());
        }
    }

    private static Server server;

    private static void startServer() throws IOException {
        server = new NettyServer(new SpecificResponder(Mail.class, new MailImpl()), new InetSocketAddress(65111));
        // the server implements the Mail protocol (MailImpl)
    }

    public static void main(String[] args) throws IOException {
        if (args.length != 3) {
            System.out.println("Usage: <to> <from> <body>");
            System.exit(1);
        }

        System.out.println("Starting server");
        // usually this would be another app, but for simplicity
        startServer();
        System.out.println("Server started");

        NettyTransceiver client = new NettyTransceiver(new InetSocketAddress(65111));
        // client code - attach to the server and send a message
        Mail.Callback proxy = SpecificRequestor.getClient(Mail.Callback.class, client);
        System.out.println("Client built, got proxy");

        // fill in the Message record and send it
        Message message = new Message();
        message.setTo(new Utf8(args[0]));
        message.setFrom(new Utf8(args[1]));
        message.setBody(new Utf8(args[2]));
        System.out.println("Calling proxy.send with message:  " + message.toString());
        proxy.send(message, new Callback<CharSequence>() {

@Override
public void handleError(Throwable arg0) {
System.out.println("Error: " + arg0.getMessage());
}

@Override
public void handleResult(CharSequence arg0) {
System.out.println("Got async result " + arg0);
}
        });
        
        System.out.println("Should see this log before Closure 'Got async result' message - but do not.");

        try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
        
        
        // cleanup
        client.close();
        server.close();
    }
}
Schema:
{"namespace": "example.proto",
 "protocol": "Mail",
 "types": [
     {"name": "Message", "type": "record",
      "fields": [
          {"name": "to",   "type": "string"},
          {"name": "from", "type": "string"},
          {"name": "body", "type": "string"}
      ]
     }
 ],
 "messages": {
     "send": {
         "request": [{"name": "message", "type": "Message"}],
         "response": "string"
     }
 }
}
Output:  

Starting server
Server started
Client built, got proxy
Calling proxy.send with message:  {"to": "some_user", "from": "joe", "body": "Hello_World"}
Sending message
Got async result Sending message to some_user from joe with body Hello_World
Should see this log before Closure 'Got async result' message - but do not.

So clearly, proxy.send() is not acting asynchronous otherwise we would see the "Should see…" log before the "Got async…" log.