public static ApacheThriftMethodInvokerFactory<?> createStaticApacheThriftMethodInvokerFactory( ApacheThriftClientConfig clientConfig, ApacheThriftConnectionFactoryConfig factoryConfig) { requireNonNull(clientConfig, "clientConfig is null"); return new ApacheThriftMethodInvokerFactory<>(factoryConfig, clientIdentity -> clientConfig); }
@Override public ListenableFuture<Object> invoke(InvokeRequest request) { try { return executorService.submit(() -> invokeSynchronous(request)); } catch (Exception e) { return immediateFailedFuture(toDriftException(e)); } }
@Provides @Singleton private static MethodInvokerFactory<Annotation> getMethodInvokerFactory(ApacheThriftConnectionFactoryConfig factoryConfig, Injector injector) { return new ApacheThriftMethodInvokerFactory<>(factoryConfig, annotation -> injector.getInstance(Key.get(ApacheThriftClientConfig.class, annotation))); }
private static int logApacheThriftDriftClient( HostAndPort address, String headerValue, List<DriftLogEntry> entries, List<MethodInvocationFilter> filters, Transport transport, Protocol protocol, boolean secure) { if (!isValidConfiguration(transport, protocol)) { return 0; } AddressSelector<?> addressSelector = context -> Optional.of(() -> address); ApacheThriftClientConfig config = new ApacheThriftClientConfig() .setTransport(toApacheThriftTransport(transport)) .setProtocol(toApacheThriftProtocol(protocol)) .setTrustCertificate(ClientTestUtils.getCertificateChainFile()) .setSslEnabled(secure); ApacheThriftConnectionFactoryConfig factoryConfig = new ApacheThriftConnectionFactoryConfig(); try (ApacheThriftMethodInvokerFactory<String> methodInvokerFactory = new ApacheThriftMethodInvokerFactory<>(factoryConfig, clientIdentity -> config)) { DriftClientFactoryManager<String> clientFactoryManager = new DriftClientFactoryManager<>(CODEC_MANAGER, methodInvokerFactory); DriftClientFactory proxyFactory = clientFactoryManager.createDriftClientFactory("clientIdentity", addressSelector, NORMAL_RESULT); DriftScribe scribe = proxyFactory.createDriftClient(DriftScribe.class, Optional.empty(), filters, new DriftClientConfig()).get(); assertEquals(scribe.log(headerValue, entries), DRIFT_OK); } catch (Exception e) { throw new RuntimeException(e); } return 1; }
@Test public void testDefaults() { assertRecordedDefaults(recordDefaults(ApacheThriftClientConfig.class) .setTransport(FRAMED) .setProtocol(BINARY) .setConnectTimeout(new Duration(500, MILLISECONDS)) .setRequestTimeout(new Duration(1, MINUTES)) .setSocksProxy(null) .setMaxFrameSize(new DataSize(16, MEGABYTE)) .setSslEnabled(false) .setTrustCertificate(null) .setKey(null) .setKeyPassword(null)); }
if (config.getSocksProxy() == null) { config.setSocksProxy(defaultSocksProxy); switch (config.getTransport()) { case UNFRAMED: transportFactory = new TTransportFactory(); break; case FRAMED: transportFactory = new TFramedTransport.Factory(toIntExact(config.getMaxFrameSize().toBytes())); break; default: throw new IllegalArgumentException("Unknown transport: " + config.getTransport()); switch (config.getProtocol()) { case BINARY: protocolFactory = new TBinaryProtocol.Factory(); break; default: throw new IllegalArgumentException("Unknown protocol: " + config.getProtocol()); if (config.isSslEnabled()) { sslContext = Optional.of(createSslContext(config)); return new ApacheThriftMethodInvoker( executorService, delayService, transportFactory, protocolFactory,
private static int logApacheThriftInvocationHandler(HostAndPort address, List<DriftLogEntry> entries) { ApacheThriftClientConfig config = new ApacheThriftClientConfig(); ApacheThriftConnectionFactoryConfig factoryConfig = new ApacheThriftConnectionFactoryConfig(); try (ApacheThriftMethodInvokerFactory<Void> methodInvokerFactory = new ApacheThriftMethodInvokerFactory<>(factoryConfig, clientIdentity -> config)) { MethodInvoker methodInvoker = methodInvokerFactory.createMethodInvoker(null); ParameterMetadata parameter = new ParameterMetadata( (short) 1, "messages", (ThriftCodec<Object>) codecManager.getCodec(list(codecManager.getCodec(DriftLogEntry.class).getType()))); MethodMetadata methodMetadata = new MethodMetadata( "Log", ImmutableList.of(parameter), (ThriftCodec<Object>) (Object) codecManager.getCodec(DriftResultCode.class), ImmutableMap.of(), false, true); ListenableFuture<Object> future = methodInvoker.invoke(new InvokeRequest(methodMetadata, () -> address, ImmutableMap.of(), ImmutableList.of(entries))); assertEquals(future.get(), DRIFT_OK); return 1; } catch (Exception e) { throw new RuntimeException(e); } }
@Test public void testExplicitPropertyMappings() { Map<String, String> properties = new ImmutableMap.Builder<String, String>() .put("thrift.client.thread-count", "99") .put("thrift.client.socks-proxy", "example.com:9876") .build(); ApacheThriftConnectionFactoryConfig expected = new ApacheThriftConnectionFactoryConfig() .setThreadCount(99) .setSocksProxy(HostAndPort.fromParts("example.com", 9876)); assertFullMapping(properties, expected); } }
@Inject public ApacheThriftMethodInvokerFactory(ApacheThriftConnectionFactoryConfig factoryConfig, Function<I, ApacheThriftClientConfig> clientConfigurationProvider) { requireNonNull(factoryConfig, "factoryConfig is null"); ThreadFactory threadFactory = daemonThreadsNamed("drift-client-%s"); if (factoryConfig.getThreadCount() == null) { executorService = listeningDecorator(newCachedThreadPool(threadFactory)); delayService = listeningDecorator(newSingleThreadScheduledExecutor(daemonThreadsNamed("drift-client-delay-%s"))); } else { delayService = listeningDecorator(newScheduledThreadPool(factoryConfig.getThreadCount(), threadFactory)); executorService = delayService; } this.clientConfigurationProvider = requireNonNull(clientConfigurationProvider, "clientConfigurationProvider is null"); this.defaultSocksProxy = factoryConfig.getSocksProxy(); }
@Test public void testDefaults() { assertRecordedDefaults(recordDefaults(ApacheThriftConnectionFactoryConfig.class) .setThreadCount(null) .setSocksProxy(null)); }
@Override public ListenableFuture<?> delay(Duration duration) { try { return delayService.schedule(() -> null, duration.toMillis(), MILLISECONDS); } catch (Exception e) { return immediateFailedFuture(toDriftException(e)); } }
private static int logApacheThriftClientBinder( HostAndPort address, String headerValue, List<DriftLogEntry> entries, List<MethodInvocationFilter> filters, Transport transport, Protocol protocol, boolean secure) { if (!isValidConfiguration(transport, protocol)) { return 0; } return logDriftClientBinder(address, headerValue, entries, new ApacheThriftClientModule(), filters, transport, protocol, secure); }
private static int logApacheThriftDriftClientAsync( HostAndPort address, String headerValue, List<DriftLogEntry> entries, List<MethodInvocationFilter> filters, Transport transport, Protocol protocol, boolean secure) { if (!isValidConfiguration(transport, protocol)) { return 0; } AddressSelector<?> addressSelector = context -> Optional.of(() -> address); ApacheThriftClientConfig config = new ApacheThriftClientConfig() .setTransport(toApacheThriftTransport(transport)) .setProtocol(toApacheThriftProtocol(protocol)) .setTrustCertificate(ClientTestUtils.getCertificateChainFile()) .setSslEnabled(secure); ApacheThriftConnectionFactoryConfig factoryConfig = new ApacheThriftConnectionFactoryConfig(); try (ApacheThriftMethodInvokerFactory<String> methodInvokerFactory = new ApacheThriftMethodInvokerFactory<>(factoryConfig, clientIdentity -> config)) { DriftClientFactoryManager<String> proxyFactoryManager = new DriftClientFactoryManager<>(CODEC_MANAGER, methodInvokerFactory); DriftClientFactory proxyFactory = proxyFactoryManager.createDriftClientFactory("myFactory", addressSelector, NORMAL_RESULT); DriftAsyncScribe scribe = proxyFactory.createDriftClient(DriftAsyncScribe.class, Optional.empty(), filters, new DriftClientConfig()).get(); assertEquals(scribe.log(headerValue, entries).get(), DRIFT_OK); } catch (Exception e) { throw new RuntimeException(e); } return 1; }
if (config.getSocksProxy() == null) { config.setSocksProxy(defaultSocksProxy); switch (config.getTransport()) { case UNFRAMED: transportFactory = new TTransportFactory(); break; case FRAMED: transportFactory = new TFramedTransport.Factory(toIntExact(config.getMaxFrameSize().toBytes())); break; default: throw new IllegalArgumentException("Unknown transport: " + config.getTransport()); switch (config.getProtocol()) { case BINARY: protocolFactory = new TBinaryProtocol.Factory(); break; default: throw new IllegalArgumentException("Unknown protocol: " + config.getProtocol()); if (config.isSslEnabled()) { sslContext = Optional.of(createSslContext(config)); return new ApacheThriftMethodInvoker( executorService, delayService, transportFactory, protocolFactory,
private static int logApacheThriftInvocationHandlerOptional(HostAndPort address, List<DriftLogEntry> entries) ApacheThriftClientConfig config = new ApacheThriftClientConfig(); ApacheThriftConnectionFactoryConfig factoryConfig = new ApacheThriftConnectionFactoryConfig(); try (ApacheThriftMethodInvokerFactory<Void> methodInvokerFactory = new ApacheThriftMethodInvokerFactory<>(factoryConfig, clientIdentity -> config)) { MethodInvoker methodInvoker = methodInvokerFactory.createMethodInvoker(null);
public static ApacheThriftMethodInvokerFactory<?> createStaticApacheThriftMethodInvokerFactory( ApacheThriftClientConfig clientConfig, ApacheThriftConnectionFactoryConfig factoryConfig) { requireNonNull(clientConfig, "clientConfig is null"); return new ApacheThriftMethodInvokerFactory<>(factoryConfig, clientIdentity -> clientConfig); }
@Provides @Singleton private static MethodInvokerFactory<Annotation> getMethodInvokerFactory(ApacheThriftConnectionFactoryConfig factoryConfig, Injector injector) { return new ApacheThriftMethodInvokerFactory<>(factoryConfig, annotation -> injector.getInstance(Key.get(ApacheThriftClientConfig.class, annotation))); }
@Override public ListenableFuture<Object> invoke(InvokeRequest request) { try { return executorService.submit(() -> invokeSynchronous(request)); } catch (Exception e) { return immediateFailedFuture(toDriftException(e)); } }
@Inject public ApacheThriftMethodInvokerFactory(ApacheThriftConnectionFactoryConfig factoryConfig, Function<I, ApacheThriftClientConfig> clientConfigurationProvider) { requireNonNull(factoryConfig, "factoryConfig is null"); ThreadFactory threadFactory = daemonThreadsNamed("drift-client-%s"); if (factoryConfig.getThreadCount() == null) { executorService = listeningDecorator(newCachedThreadPool(threadFactory)); delayService = listeningDecorator(newSingleThreadScheduledExecutor(daemonThreadsNamed("drift-client-delay-%s"))); } else { delayService = listeningDecorator(newScheduledThreadPool(factoryConfig.getThreadCount(), threadFactory)); executorService = delayService; } this.clientConfigurationProvider = requireNonNull(clientConfigurationProvider, "clientConfigurationProvider is null"); this.defaultSocksProxy = factoryConfig.getSocksProxy(); }
@Override public ListenableFuture<?> delay(Duration duration) { try { return delayService.schedule(() -> null, duration.toMillis(), MILLISECONDS); } catch (Exception e) { return immediateFailedFuture(toDriftException(e)); } }