private T openService(HostAndPort host, ThriftClient<T> client, Transport transport) throws TTransportException { try { NiftyClientConnector<? extends NiftyClientChannel> connector; if (Transport.FRAMED.equals(transport)) { connector = new FramedClientConnector(host); } else if (Transport.BUFFERED.equals(transport)) { connector = new UnframedClientConnector(host); } else { throw new ErrorMessage("Unexpected thrift transport type: %s", transport); } return client.open(connector).get(); } catch (InterruptedException e) { Thread.currentThread().interrupt(); throw new TTransportException(TIMED_OUT, "Interrupted opening connection to " + host, e); } catch (ExecutionException e) { Throwable cause = e.getCause(); Throwables.propagateIfInstanceOf(cause, TTransportException.class); throw new TTransportException(UNKNOWN, "Exception opening connection to " + host, cause); } } }
@Override public PooledObject<T> makeObject() throws Exception { T obj = getThriftClient(interfaceClass).open(getClientManager().createChannel(getConnector()).get()); return new DefaultPooledObject<T>(obj); }
try (Scribe scribe = scribeClient.open(localFramedConnector(server.getPort())).get()) { assertEquals(scribe.log(MESSAGES), ResultCode.OK); assertEquals(injector.getInstance(Key.get(SwiftScribe.class, TestAnnotation.class)).getMessages(), newArrayList(MESSAGES)); try (PumaReadService puma = pumaClient.open(localFramedConnector(server.getPort())).get()) { List<ReadResultQueryInfoTimeString> results = puma.getResultTimeString(TestPuma.PUMA_REQUEST); verifyPumaResults(results);
try (Scribe scribe = scribeClient.open(localFramedConnector(server.getPort())).get()) { assertEquals(scribe.log(MESSAGES), ResultCode.OK); assertEquals(injector.getInstance(SwiftScribe.class).getMessages(), newArrayList(MESSAGES)); try (PumaReadService puma = pumaClient.open(localFramedConnector(server.getPort())).get()) { List<ReadResultQueryInfoTimeString> results = puma.getResultTimeString(TestPuma.PUMA_REQUEST); verifyPumaResults(results);
try (Scribe scribe = scribeClient.open(localFramedConnector(server.getPort())).get()) { assertEquals(scribe.log(MESSAGES), ResultCode.OK); assertEquals(injector.getInstance(Key.get(SwiftScribe.class, TestAnnotation.class)).getMessages(), newArrayList(MESSAGES)); try (PumaReadService puma = pumaClient.open(localFramedConnector(server.getPort())).get()) { List<ReadResultQueryInfoTimeString> results = puma.getResultTimeString(TestPuma.PUMA_REQUEST); verifyPumaResults(results);
private T openService(HostAndPort host, ThriftClient<T> client, Transport transport) throws TTransportException { try { NiftyClientConnector<? extends NiftyClientChannel> connector; if (Transport.FRAMED.equals(transport)) { connector = new FramedClientConnector(host); } else if (Transport.BUFFERED.equals(transport)) { connector = new UnframedClientConnector(host); } else { throw new ErrorMessage("Unexpected thrift transport type: %s", transport); } return client.open(connector).get(); } catch (InterruptedException e) { Thread.currentThread().interrupt(); throw new TTransportException(TIMED_OUT, "Interrupted opening connection to " + host, e); } catch (ExecutionException e) { Throwable cause = e.getCause(); Throwables.propagateIfInstanceOf(cause, TTransportException.class); throw new TTransportException(UNKNOWN, "Exception opening connection to " + host, cause); } } }
private Scribe createScribeClient( ThriftClientManager manager, ScopedServer server, TProtocolFactory protocolFactory) throws ExecutionException, InterruptedException, TException { ThriftClientConfig config = new ThriftClientConfig().setConnectTimeout(Duration.valueOf("1s")) .setReceiveTimeout(Duration.valueOf("10s")) .setReadTimeout(Duration.valueOf("1s")) .setWriteTimeout(Duration.valueOf("1s")); ThriftClient<Scribe> thriftClient = new ThriftClient<>(manager, Scribe.class, config, "ScribeClient"); return thriftClient.open(new FramedClientConnector(HostAndPort.fromParts("localhost", server.getPort()), TDuplexProtocolFactory.fromSingleFactory(protocolFactory))).get(); }
@Test public void testGenericProcessor() throws ExecutionException, InterruptedException { ThriftCodecManager codecManager = new ThriftCodecManager(); ThriftServiceProcessor processor = new ThriftServiceProcessor(codecManager, ImmutableList.<ThriftEventHandler>of(), new GenericService()); try (ThriftServer server = new ThriftServer(processor, new ThriftServerConfig()).start(); ThriftClientManager clientManager = new ThriftClientManager(codecManager)) { ThriftClient<GenericInterface.Client> clientOpener = new ThriftClient<>(clientManager, GenericInterface.Client.class); try (GenericInterface.Client client = clientOpener.open(new FramedClientConnector(HostAndPort.fromParts("localhost", server.getPort()))).get()) { GenericStruct<String> original = new GenericStruct<>(); original.genericField = "original.genericField"; GenericStruct<String> copy = client.echo(original); assertEquals(original, copy); } } } }
protected <T> ListenableFuture<T> createHttpClient(Class<T> clientClass, int serverPort) throws TTransportException, InterruptedException, ExecutionException { ThriftClientConfig config = new ThriftClientConfig().setConnectTimeout(new Duration(1, TimeUnit.SECONDS)) .setReceiveTimeout(new Duration(10, TimeUnit.SECONDS)) .setReadTimeout(new Duration(1, TimeUnit.SECONDS)) .setWriteTimeout(new Duration(1, TimeUnit.SECONDS)); HttpClientConnector connector = new HttpClientConnector(URI.create("http://localhost:" + serverPort + "/thrift/")); return new ThriftClient<>(clientManager, clientClass, config, "asyncTestClient").open(connector); }
protected <T> ListenableFuture<T> createClient(Class<T> clientClass, int serverPort, final Duration connectDelay) throws TTransportException, InterruptedException, ExecutionException { HostAndPort address = HostAndPort.fromParts("localhost", serverPort); ThriftClientConfig config = new ThriftClientConfig().setConnectTimeout(new Duration(1, TimeUnit.SECONDS)) .setReceiveTimeout(new Duration(10, TimeUnit.SECONDS)) .setReadTimeout(new Duration(1, TimeUnit.SECONDS)) .setWriteTimeout(new Duration(1, TimeUnit.SECONDS)); FramedClientConnector connector = new FramedClientConnector(address) { @Override public FramedClientChannel newThriftClientChannel( Channel nettyChannel, NettyClientConfig nettyClientConfig) { if (connectDelay.toMillis() > 0) { // Introduce an artificial delay to the client creation process, to test // cases where the client future is not set immediately when making async // connections Uninterruptibles.sleepUninterruptibly(connectDelay.toMillis(), TimeUnit.MILLISECONDS); } return super.newThriftClientChannel(nettyChannel, nettyClientConfig); } }; return new ThriftClient<>(clientManager, clientClass, config, "asyncTestClient").open(connector); }