下面列出了com.google.protobuf.BlockingRpcChannel#com.google.protobuf.RpcChannel 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void doTest(RpcServer rpcServer) throws InterruptedException,
ServiceException, IOException {
BlockingRpcChannel blockingChannel = RpcChannels
.newBlockingRpcChannel(clientConnectionFactory);
RpcChannel channel = RpcChannels.newRpcChannel(clientConnectionFactory,
threadPool);
BlockingInterface blockingStub = TestService
.newBlockingStub(blockingChannel);
TestService stub = TestService.newStub(channel);
try {
rpcServer.startServer();
Thread.sleep(500);
doRpc(stub);
doBlockingRpc(blockingStub);
doBlockingRpc(blockingStub);
doRpc(stub);
} finally {
Thread.sleep(500);
System.out.println("Closing Client");
if (clientConnectionFactory instanceof Closeable) {
((PersistentRpcConnectionFactory) clientConnectionFactory).close();
}
Thread.sleep(100);
System.out.println("Closing Server");
rpcServer.shutDown();
}
}
private AddressBookProtos.AddressBookService connectExportedProtobufAddress(ExportDeclaration declaration) throws EndpointException, NoSuchMethodException, InvocationTargetException, IllegalAccessException, BinderException {
ProtobufferExportDeclarationWrapper pojo = ProtobufferExportDeclarationWrapper.create(declaration);
Bus cxfbus = BusFactory.getThreadDefaultBus();
BindingFactoryManager mgr = cxfbus.getExtension(BindingFactoryManager.class);
mgr.registerBindingFactory(ProtobufBindingFactory.PROTOBUF_BINDING_ID, new ProtobufBindingFactory(cxfbus));
Class<?> bufferService = AddressBookProtos.AddressBookService.class;
Class<?> bufferMessage = AddressBookProtos.AddressBookServiceMessage.class;
Class<? extends Message> generic = bufferMessage.asSubclass(Message.class);
RpcChannel channel = new SimpleRpcChannel(pojo.getAddress(), generic);
Method method = bufferService.getMethod("newStub", RpcChannel.class);
Object service = method.invoke(bufferService, channel);
AddressBookProtos.AddressBookService addressBook = (AddressBookProtos.AddressBookService) service;
return addressBook;
}
/**
* Create a {@link RpcChannel} that uses the given
* {@link RpcConnectionFactory} to connect to the RPC server and the given
* {@link Executor} to listen for the RPC response after sending the request.
* RPCs made using this {@link RpcChannel} will not block the thread calling
* the RPC method. Use {@link #newBlockingRpcChannel(RpcConnectionFactory)} if
* you want the RPC method to block.
* <p>
* This channel doesn't call the callback if the server-side implementation
* did not call the callback. If any error occurs, it will call the callback
* with null and update the controller with the error.
*/
public static RpcChannel newRpcChannel(
RpcConnectionFactory connectionFactory, Executor executor) {
return new RpcChannelImpl(connectionFactory, executor);
}