项目作者: dharmeshjogadia

项目描述 :
Thrift service client pooling using stormpot
高级语言: Java
项目地址: git://github.com/dharmeshjogadia/thrift-client-pooling.git
创建时间: 2019-12-06T09:38:42Z
项目社区:https://github.com/dharmeshjogadia/thrift-client-pooling

开源协议:

下载


thrift-client-pooling

Thrift service client pooling using stormpot

Example

Configuration

I am using YAML configuraiton to load thrift service client pooling

  1. !!com.github.thrift.client.ThriftClientConfiguration
  2. baseUrl: "http://localhost:8080"
  3. services:
  4. TestService:
  5. poolSize: 5
  6. connectionExpireInMills: 1000
  7. connectionFetchTimeoutInMills: 1000
  8. FakeService1:
  9. .....

Thrift service client pooling sample code

  1. public static final int POOL_SIZE = 5;
  2. private static final String SERVICE_NAME = "TestService";
  3. private ThriftClientConfiguration thriftClientConfigurationYaml() {
  4. Yaml yaml = new Yaml();
  5. InputStream inputStream = this.getClass()
  6. .getClassLoader()
  7. .getResourceAsStream("thriftClientConfig.yaml");
  8. return yaml.load(inputStream);
  9. }
  10. public void testGetConnection() throws Exception {
  11. // Initlize Thrift Connection Factory for TestService
  12. configuration = thriftClientConfigurationYaml();
  13. thriftClientFactory = new ThriftClientFactory(protocolFactory, configuration.getBaseUrl());
  14. ThriftClientConfiguration.ServiceProperties serviceProperties = configuration.getServices().get(SERVICE_NAME);
  15. Config<ThriftConnection<TTestService.Client>> config = new Config<>().setAllocator(
  16. new ThriftConnectionAllocator<>(thriftClientFactory, SERVICE_NAME, TTestService.Client.class));
  17. config.setSize(serviceProperties.getPoolSize());
  18. config.setExpiration(new TimeExpiration<>(serviceProperties.getConnectionExpireInMills(),
  19. TimeUnit.MILLISECONDS));
  20. thriftConnectionFactory = new ThriftConnectionFactory<>(config,
  21. new Timeout(serviceProperties.getConnectionFetchTimeoutInMills(), TimeUnit.MILLISECONDS));
  22. // Run the test
  23. ThriftConnection<TTestService.Client> result = thriftConnectionFactory.getConnection();
  24. // Verify the results
  25. Assert.assertNotNull(result);
  26. Assert.assertNotNull(result.getClient());
  27. Assert.assertTrue(thriftConnectionFactory.getPool().getAllocationCount() > 0);
  28. Assert.assertEquals(POOL_SIZE, thriftConnectionFactory.getPool().getTargetSize());
  29. // close
  30. result.release();
  31. result.close();
  32. }