Asynchronous 如何:使用Netty和Avro进行异步回调
我试图通过使用NettyServer实现来实现异步Avro调用。在挖掘源代码之后,我从TestNettyServerWithCallbacks.java中找到了一个关于如何使用NettyServer的示例 在运行一些测试时,我意识到NettyServer从不调用hello(回调)方法,而是一直调用同步hello()方法。客户端程序打印出“Hello”,但我希望结果是“Hello ASYNC”。我真的不知道发生了什么事 我希望有人能给我一些启示,也许能指出错误。下面是我用来执行简单异步avro测试的代码 java-客户端代码Asynchronous 如何:使用Netty和Avro进行异步回调,asynchronous,callback,netty,avro,Asynchronous,Callback,Netty,Avro,我试图通过使用NettyServer实现来实现异步Avro调用。在挖掘源代码之后,我从TestNettyServerWithCallbacks.java中找到了一个关于如何使用NettyServer的示例 在运行一些测试时,我意识到NettyServer从不调用hello(回调)方法,而是一直调用同步hello()方法。客户端程序打印出“Hello”,但我希望结果是“Hello ASYNC”。我真的不知道发生了什么事 我希望有人能给我一些启示,也许能指出错误。下面是我用来执行简单异步avro测试
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);
}
}
}
chaimpl.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");
}
}
NettyServer实现实际上根本没有实现异步样式。这是图书馆的不足之处。相反,您需要指定一个异步执行处理程序,而不是尝试通过回调将服务链接在一起。以下是我用来设置NetyServer的方法,以实现这一点:
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);
嘿Carbotex,我也有同样的问题。你有没有想过为什么会发生这种情况??
@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;
}
}
{
"namespace": "avro.test",
"protocol": "Chat",
"types" : [],
"messages": {
"hello": {
"request": [],
"response": "string"
}
}
}
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);