org.apache.thrift.transport.TTransport Java Examples

The following examples show how to use org.apache.thrift.transport.TTransport. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AbstractMultiRequestTest.java    From disruptor_thrift_server with Apache License 2.0 6 votes vote down vote up
@Test
public void multiRequestTest() throws Exception
{
    TTransport transport = getNewTransport();

    try
    {
        TestService.Client client = getNewClient(transport);

        for (int i = 0; i < REQUESTS; i += 4)
            invokeRequests(client, i, getRandomArgument(), getRandomArgument());
    }
    finally
    {
        transport.close();
    }
}
 
Example #2
Source File: ThriftBinaryCodec.java    From attic-aurora with Apache License 2.0 6 votes vote down vote up
/**
 * Encodes a thrift object into a DEFLATE-compressed binary array.
 *
 * @param tBase Object to encode.
 * @return Deflated, encoded object.
 * @throws CodingException If the object could not be encoded.
 */
public static byte[] deflateNonNull(TBase<?, ?> tBase) throws CodingException {
  requireNonNull(tBase);

  // NOTE: Buffering is needed here for performance.
  // There are actually 2 buffers in play here - the BufferedOutputStream prevents thrift from
  // causing a call to deflate() on every encoded primitive. The DeflaterOutputStream buffer
  // allows the underlying Deflater to operate on a larger chunk at a time without stopping to
  // copy the intermediate compressed output to outBytes.
  // See http://bugs.java.com/bugdatabase/view_bug.do?bug_id=4986239
  ByteArrayOutputStream outBytes = new ByteArrayOutputStream();
  TTransport transport = new TIOStreamTransport(
      new BufferedOutputStream(
          new DeflaterOutputStream(outBytes, new Deflater(DEFLATE_LEVEL), DEFLATER_BUFFER_SIZE),
          DEFLATER_BUFFER_SIZE));
  try {
    TProtocol protocol = PROTOCOL_FACTORY.getProtocol(transport);
    tBase.write(protocol);
    transport.close(); // calls finish() on the underlying stream, completing the compression
    return outBytes.toByteArray();
  } catch (TException e) {
    throw new CodingException("Failed to serialize: " + tBase, e);
  } finally {
    transport.close();
  }
}
 
Example #3
Source File: FailoverChecker.java    From ThriftJ with Apache License 2.0 6 votes vote down vote up
@Override
public void run() {
	for (ThriftServer thriftServer : getAvailableServers(true)) {
		TTransport tt = null;
		boolean valid = false;
		try {
			tt = poolProvider.getConnection(thriftServer);
			valid = connectionValidator.isValid(tt);
		} catch (Exception e) {
			valid = false;
			logger.warn(e.getMessage(), e);
		} finally {
			if (tt != null) {
				if (valid) {
					poolProvider.returnConnection(thriftServer, tt);
				} else {
					failoverStrategy.fail(thriftServer);
					poolProvider.returnBrokenConnection(thriftServer, tt);
				}
			} else {
				failoverStrategy.fail(thriftServer);
			}
		}
	}
}
 
Example #4
Source File: ThriftConnectionFactory.java    From ThriftJ with Apache License 2.0 6 votes vote down vote up
@Override
public boolean validateObject(ThriftServer thriftServer, PooledObject<TTransport> pooledObject) {
	boolean isValidate;
	try {
		if (failoverChecker == null) {
			isValidate = pooledObject.getObject().isOpen();
		} else {
			ConnectionValidator validator = failoverChecker.getConnectionValidator();
			isValidate = pooledObject.getObject().isOpen() && (validator == null || validator.isValid(pooledObject.getObject()));
		}
	} catch (Throwable e) {
		logger.warn("Fail to validate tsocket: {}:{}", new Object[]{thriftServer.getHost(), thriftServer.getPort(), e});
		isValidate = false;
	}
	if (failoverChecker != null && !isValidate) {
		failoverChecker.getFailoverStrategy().fail(thriftServer);
	}
	logger.info("ValidateObject isValidate:{}", isValidate);
	
	return isValidate;
}
 
Example #5
Source File: MultiServiceClient.java    From ThriftBook with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws TException {
    TTransport trans = new TFramedTransport(new TSocket("localhost", 9090));
    TProtocol proto = new TJSONProtocol(trans);

    TMultiplexedProtocol proto_msg = new TMultiplexedProtocol(proto, "Message");
    Message.Client client_msg = new Message.Client(proto_msg);
    TMultiplexedProtocol proto_time = new TMultiplexedProtocol(proto, "ServerTime");
    ServerTime.Client client_time = new ServerTime.Client(proto_time);

    trans.open();
    String line;
    do {
        System.out.println("Message from server: " + client_msg.motd());
        System.out.println("Time at server: " + client_time.time_at_server((short)-1));
        System.out.println("Enter to continue, 'q' to quit: ");
        line = System.console().readLine();
    } while (0 != line.compareToIgnoreCase("q"));   
}
 
Example #6
Source File: GfxdThriftServerSelector.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
protected ClientProcessData(GfxdTSocket socket, int connectionNumber,
    TProcessor proc, TTransport in, TTransport out, TProtocol inp,
    TProtocol outp, TServerEventHandler eventHandler) {
  this.clientSocket = socket;
  this.connectionNumber = connectionNumber;
  this.processor = proc;
  this.inputTransport = in;
  this.outputTransport = out;
  this.inputProtocol = inp;
  this.outputProtocol = outp;
  this.eventHandler = eventHandler;
  if (eventHandler != null) {
    this.connectionContext = eventHandler.createContext(inp, outp);
  }
  else {
    this.connectionContext = null;
  }
  this.idle = true;
}
 
Example #7
Source File: ServerTest.java    From ikasoa with MIT License 6 votes vote down vote up
@Test
public void testDefaultNettyServerImpl() {
	int serverPort = ServerUtil.getNewPort();
	NettyServer defaultNettyServer = (NettyServer) factory.getThriftServer(serverName, serverPort,
			new ThriftSimpleService.Processor<Iface>(new ThriftSimpleServiceImpl()));
	assertEquals(defaultNettyServer.getServerName(), serverName);
	assertEquals(defaultNettyServer.getServerPort(), serverPort);
	defaultNettyServer.run();
	waiting();
	try (ThriftClient client = factory.getThriftClient(TestConstants.LOCAL_HOST, serverPort);
			TTransport transport = client.getTransport()) {
		transport.open();
		assertEquals(TestConstants.TEST_STRING,
				new ThriftSimpleService.Client(client.getProtocol(transport)).get(TestConstants.TEST_STRING));
	} catch (Exception e) {
		fail();
	} finally {
		defaultNettyServer.stop();
	}
}
 
Example #8
Source File: AppClient.java    From rpcx-benchmark with Apache License 2.0 6 votes vote down vote up
public static Greeter.Client[] createClients(String host, int n, BenchmarkMessage msg) throws TException {
    TTransport[] transport = new TTransport[n];
    Greeter.Client[] clients = new Greeter.Client[n];

    //warmup
    for (int i = 0; i < n; i++) {
        transport[i] = new TFramedTransport(new TSocket(host, 8972));
        transport[i].open();

        TProtocol protocol = new TBinaryProtocol(transport[i]);
        clients[i] =  new Greeter.Client(protocol);
        clients[i].say(msg);
    }

    return clients;
}
 
Example #9
Source File: ThriftTest.java    From java-specialagent with Apache License 2.0 6 votes vote down vote up
@Test
public void test(final MockTracer tracer) throws Exception {
  startNewThreadPoolServer();

  final TTransport transport = new TSocket("localhost", port);
  transport.open();

  final TProtocol protocol = new TBinaryProtocol(transport);

  final CustomService.Client client = new CustomService.Client(protocol);
  assertEquals("Say Good bye World", client.say("Good bye", "World"));

  await().atMost(5, TimeUnit.SECONDS).until(TestUtil.reportedSpansSize(tracer), equalTo(2));

  final List<MockSpan> mockSpans = tracer.finishedSpans();
  assertEquals(2, mockSpans.size());

  assertTrue(mockSpans.get(0).parentId() != 0 || mockSpans.get(1).parentId() != 0);
  assertNull(tracer.activeSpan());
}
 
Example #10
Source File: ServerTest.java    From ikasoa with MIT License 6 votes vote down vote up
@Test
public void testDESCompactThriftServerImpl() {
	int serverPort = ServerUtil.getNewPort();
	String key = TestConstants.TEST_KEY8;
	ThriftServerConfiguration serverConfiguration = new ThriftServerConfiguration();
	serverConfiguration.setProtocolFactory(new DESCompactProtocolFactory(key));
	ThriftClientConfiguration clientConfiguration = new ThriftClientConfiguration();
	clientConfiguration.setProtocolFactory(new DESCompactProtocolFactory(key));
	Factory factory = new GeneralFactory(serverConfiguration, clientConfiguration);
	ThriftServer thriftServer = factory.getThriftServer(serverName, serverPort, processor);
	thriftServer.run();
	waiting();
	try (ThriftClient thriftClient = factory.getThriftClient(TestConstants.LOCAL_HOST, serverPort);
			TTransport transport = thriftClient.getTransport()) {
		transport.open();
		assertEquals(TestConstants.TEST_STRING,
				new ThriftSimpleService.Client(thriftClient.getProtocol(transport)).get(TestConstants.TEST_STRING));
	} catch (Exception e) {
		fail();
	} finally {
		thriftServer.stop();
	}
}
 
Example #11
Source File: TProtocolReadMessageEndInterceptor.java    From pinpoint with Apache License 2.0 6 votes vote down vote up
private void recordConnection(SpanRecorder recorder, TTransport transport) {
    // retrieve connection information
    String localIpPort = ThriftConstants.UNKNOWN_ADDRESS;
    String remoteAddress = ThriftConstants.UNKNOWN_ADDRESS;
    Socket socket = ((SocketFieldAccessor) transport)._$PINPOINT$_getSocket();
    if (socket != null) {
        localIpPort = ThriftUtils.getIpPort(socket.getLocalSocketAddress());
        remoteAddress = ThriftUtils.getIp(socket.getRemoteSocketAddress());
    }
    if (localIpPort != ThriftConstants.UNKNOWN_ADDRESS) {
        recorder.recordEndPoint(localIpPort);
    }
    if (remoteAddress != ThriftConstants.UNKNOWN_ADDRESS) {
        recorder.recordRemoteAddress(remoteAddress);
    }
}
 
Example #12
Source File: HealthCheck.java    From suro with Apache License 2.0 5 votes vote down vote up
private SuroServer.Client getClient(String host, int port, int timeout) throws SocketException, TTransportException {
    TSocket socket = new TSocket(host, port, timeout);
    socket.getSocket().setTcpNoDelay(true);
    socket.getSocket().setKeepAlive(true);
    socket.getSocket().setSoLinger(true, 0);
    TTransport transport = new TFramedTransport(socket);
    transport.open();

    TProtocol protocol = new TBinaryProtocol(transport);

    return new SuroServer.Client(protocol);
}
 
Example #13
Source File: FactoryClient.java    From ThriftBook with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws TException {
    TTransport trans = new TFramedTransport(new TSocket("localhost", 9090));
    TProtocol proto = new TJSONProtocol(trans);
    Message.Iface client = new Message.Client(proto);
    trans.open();
    String line;
    do {
        System.out.println("Message from server: " + client.motd());
        System.out.println("Enter to continue, 'q' to quit: ");
        line = System.console().readLine();
    } while (0 != line.compareToIgnoreCase("q"));
}
 
Example #14
Source File: ThriftTest.java    From java-specialagent with Apache License 2.0 5 votes vote down vote up
@Test
public void withError() throws Exception {
  TestUtil.setGlobalTracer(tracer);

  startNewThreadPoolServer();

  final TTransport transport = new TSocket("localhost", port);
  transport.open();

  final TProtocol protocol = new TBinaryProtocol(transport);
  CustomService.Client client = new CustomService.Client(protocol);

  try {
    assertEquals("Say Good bye", client.withError());
    fail();
  }
  catch (final Exception ignore) {
  }

  await().atMost(15, TimeUnit.SECONDS).until(TestUtil.reportedSpansSize(tracer), equalTo(2));

  final List<MockSpan> mockSpans = tracer.finishedSpans();
  assertEquals(2, mockSpans.size());

  assertTrue(mockSpans.get(0).parentId() != 0 || mockSpans.get(1).parentId() != 0);

  assertNull(tracer.activeSpan());

  verify(tracer, times(2)).buildSpan(anyString());
  verify(tracer, times(1)).inject(any(SpanContext.class), any(Format.class), any());
}
 
Example #15
Source File: EmbeddedCassandraServiceTest.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
/**
 * Gets a connection to the localhost client
 *
 * @return
 * @throws TTransportException
 */
private Cassandra.Client getClient() throws TTransportException
{
    TTransport tr = new TFramedTransport(new TSocket("localhost", DatabaseDescriptor.getRpcPort()));
    TProtocol proto = new TBinaryProtocol(tr);
    Cassandra.Client client = new Cassandra.Client(proto);
    tr.open();
    return client;
}
 
Example #16
Source File: FrameBufferTransportInjectInterceptor.java    From pinpoint with Apache License 2.0 5 votes vote down vote up
protected final void injectSocket(TTransport inTrans, Socket rootSocket) {
    if (!(inTrans instanceof SocketFieldAccessor)) {
        if (isDebug) {
            logger.debug("Invalid target object. Need field accessor({}).", SocketFieldAccessor.class.getName());
        }
        return;
    }
    ((SocketFieldAccessor)inTrans)._$PINPOINT$_setSocket(rootSocket);
}
 
Example #17
Source File: FrameBufferTransportInjectInterceptor.java    From pinpoint with Apache License 2.0 5 votes vote down vote up
@Override
public void after(Object target, Object[] args, Object result, Throwable throwable) {
    if (validate0(target, args, result)) {
        if (isDebug) {
            logger.afterInterceptor(target, args, result, throwable);
        }
        Socket rootSocket = getRootSocket(target);
        if (rootSocket != null) {
            TTransport injectionTarget = getInjectionTarget(target, args, result);
            injectSocket(injectionTarget, rootSocket);
        }
    }
}
 
Example #18
Source File: SimpleClient.java    From ThriftBook with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws TException {
    TTransport trans = new TSocket("localhost", 9090);
    TProtocol proto = new TBinaryProtocol(trans);
    SocialLookup.Iface client = new SocialLookup.Client(proto);

    trans.open();
    System.out.println("Number 1 site: " + client.GetSiteByRank(1));
    System.out.println("Twitter rank : " + client.GetSiteRankByName("Twitter"));
    trans.close();
}
 
Example #19
Source File: TBoundedThreadPoolServer.java    From hbase with Apache License 2.0 5 votes vote down vote up
/**
 * Loops on processing a client forever
 */
@Override
public void run() {
  TProcessor processor = null;
  TTransport inputTransport = null;
  TTransport outputTransport = null;
  TProtocol inputProtocol = null;
  TProtocol outputProtocol = null;
  try {
    processor = processorFactory_.getProcessor(client);
    inputTransport = inputTransportFactory_.getTransport(client);
    outputTransport = outputTransportFactory_.getTransport(client);
    inputProtocol = inputProtocolFactory_.getProtocol(inputTransport);
    outputProtocol = outputProtocolFactory_.getProtocol(outputTransport);
    // we check stopped_ first to make sure we're not supposed to be shutting
    // down. this is necessary for graceful shutdown.
    while (true) {
      if (stopped) {
        break;
      }
      processor.process(inputProtocol, outputProtocol);
    }
  } catch (TTransportException ttx) {
    // Assume the client died and continue silently
  } catch (TException tx) {
    LOG.error("Thrift error occurred during processing of message.", tx);
  } catch (Exception x) {
    LOG.error("Error occurred during processing of message.", x);
  }

  if (inputTransport != null) {
    inputTransport.close();
  }

  if (outputTransport != null) {
    outputTransport.close();
  }
}
 
Example #20
Source File: ClientConnection.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
private void setTimeout(int milliseconds) throws SQLException {
  // input and output protocol are identical in our usage
  TTransport socket = this.clientService.getInputProtocol().getTransport();
  if (socket instanceof SocketTimeout) {
    try {
      ((SocketTimeout)socket).setSoTimeout(milliseconds);
    } catch (SocketException se) {
      throw ThriftExceptionUtil.newSQLException(SQLState.SOCKET_EXCEPTION,
          se, se.getMessage());
    }
  }
}
 
Example #21
Source File: AbstractColumnFamilyInputFormat.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
public static Cassandra.Client createAuthenticatedClient(String location, int port, Configuration conf) throws Exception
{
    logger.debug("Creating authenticated client for CF input format");
    TTransport transport;
    try
    {
        transport = ConfigHelper.getClientTransportFactory(conf).openTransport(location, port);
    }
    catch (Exception e)
    {
        throw new TTransportException("Failed to open a transport to " + location + ":" + port + ".", e);
    }
    TProtocol binaryProtocol = new TBinaryProtocol(transport, true, true);
    Cassandra.Client client = new Cassandra.Client(binaryProtocol);

    // log in
    client.set_keyspace(ConfigHelper.getInputKeyspace(conf));
    if ((ConfigHelper.getInputKeyspaceUserName(conf) != null) && (ConfigHelper.getInputKeyspacePassword(conf) != null))
    {
        Map<String, String> creds = new HashMap<String, String>();
        creds.put(IAuthenticator.USERNAME_KEY, ConfigHelper.getInputKeyspaceUserName(conf));
        creds.put(IAuthenticator.PASSWORD_KEY, ConfigHelper.getInputKeyspacePassword(conf));
        AuthenticationRequest authRequest = new AuthenticationRequest(creds);
        client.login(authRequest);
    }
    logger.debug("Authenticated client for CF input format created successfully");
    return client;
}
 
Example #22
Source File: AbstractThriftOverHttpTest.java    From armeria with Apache License 2.0 5 votes vote down vote up
@Test
public void testHttpsInvocation() throws Exception {
    try (TTransport transport = newTransport("https", "/hello")) {
        final HelloService.Client client =
                new HelloService.Client.Factory().getClient(
                        ThriftProtocolFactories.BINARY.getProtocol(transport));

        assertThat(client.hello("Trustin")).isEqualTo("Hello, Trustin!");
    }
}
 
Example #23
Source File: TFactoryBasedThreadPoolServer.java    From RDFS with Apache License 2.0 5 votes vote down vote up
/**
 * Loops on processing a client forever
 */
public void run() {
  TProcessor processor = null;
  TTransport inputTransport = null;
  TTransport outputTransport = null;
  TProtocol inputProtocol = null;
  TProtocol outputProtocol = null;
  try {
    processor = processorFactory_.getProcessor(client_);
    inputTransport = inputTransportFactory_.getTransport(client_);
    outputTransport = outputTransportFactory_.getTransport(client_);
    inputProtocol = inputProtocolFactory_.getProtocol(inputTransport);
    outputProtocol = outputProtocolFactory_.getProtocol(outputTransport);
    // we check stopped_ first to make sure we're not supposed to be shutting
    // down. this is necessary for graceful shutdown.
    while (!stopped_ && processor.process(inputProtocol, outputProtocol)) {}
  } catch (TTransportException ttx) {
    // Assume the client died and continue silently
  } catch (TException tx) {
    LOG.error("Thrift error occurred during processing of message.", tx);
  } catch (Exception x) {
    LOG.error("Error occurred during processing of message.", x);
  }

  if (inputTransport != null) {
    inputTransport.close();
  }

  if (outputTransport != null) {
    outputTransport.close();
  }
}
 
Example #24
Source File: TSetIpAddressProcessorFactoryTest.java    From waggle-dance with Apache License 2.0 5 votes vote down vote up
@Test
public void connectionIsMonitored() throws Exception {
  factory.getProcessor(transport);

  ArgumentCaptor<TTransport> transportCaptor = ArgumentCaptor.forClass(TTransport.class);
  ArgumentCaptor<Closeable> handlerCaptor = ArgumentCaptor.forClass(Closeable.class);
  verify(transportMonitor).monitor(transportCaptor.capture(), handlerCaptor.capture());
  assertThat(transportCaptor.getValue(), is(transport));
  assertThat(handlerCaptor.getValue(), is(instanceOf(FederatedHMSHandler.class)));
}
 
Example #25
Source File: ServiceClientImpl.java    From ikasoa with MIT License 5 votes vote down vote up
@Override
public String get(String arg) throws IkasoaException {
	TTransport transport = oprot_.getTransport();
	try {
		if (!transport.isOpen())
			transport.open();
		log.debug("Transport is open .");
		sendGet(arg);
		return recvGet();
	} catch (TException e) {
		transport.close();
		log.debug("Transport is close .");
		throw new IkasoaException("Execute failed !", e);
	}
}
 
Example #26
Source File: TProtocolReadMessageEndInterceptor.java    From pinpoint with Apache License 2.0 5 votes vote down vote up
private void recordRootSpan(final Trace trace, final ThriftRequestProperty parentTraceInfo, Object target) {
    // begin root span
    SpanRecorder recorder = trace.getSpanRecorder();
    recorder.recordServiceType(ThriftConstants.THRIFT_SERVER);
    recorder.recordApi(this.thriftServerEntryMethodDescriptor);
    if (!trace.isRoot()) {
        recordParentInfo(recorder, parentTraceInfo);
    }
    // record connection information here as the socket may be closed by the time the Span is popped in
    // TBaseAsyncProcessorProcessInterceptor's after section.
    TTransport transport = ((TProtocol)target).getTransport();
    recordConnection(recorder, transport);
}
 
Example #27
Source File: ClientConnection.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
private int getTimeout() throws SQLException {
  // input and output protocol are identical in our usage
  TTransport socket = this.clientService.getInputProtocol().getTransport();
  if (socket instanceof SocketTimeout) {
    try {
      return ((SocketTimeout)socket).getSoTimeout();
    } catch (SocketException se) {
      throw ThriftExceptionUtil.newSQLException(SQLState.SOCKET_EXCEPTION,
          se, se.getMessage());
    }
  }
  else {
    return 0;
  }
}
 
Example #28
Source File: SocketPoolTest.java    From ikasoa with MIT License 5 votes vote down vote up
@Test
public void testCustomNoSocketPool() {
	int serverPort = ServerUtil.getNewPort();
	ThriftClientConfiguration configuration = new ThriftClientConfiguration();
	configuration.setSocketPool(new NoSocketPoolImpl());
	configuration.setTransportFactory(new TTransportFactory());
	configuration.setSocketPool(new TestSocketPoolImpl(serverPort));
	try (ThriftClient thriftClient = new GeneralFactory(configuration).getThriftClient(TestConstants.LOCAL_HOST,
			serverPort); TTransport transport = thriftClient.getTransport()) {
		assertNull(transport);
	} catch (Exception e) {
		fail();
	}
}
 
Example #29
Source File: ColumnFamilyRecordReader.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
public void close()
{
    if (client != null)
    {
        TTransport transport = client.getOutputProtocol().getTransport();
        if (transport.isOpen())
            transport.close();
    }
}
 
Example #30
Source File: SocketPoolTest.java    From ikasoa with MIT License 5 votes vote down vote up
@Test
public void testCustomCommonsSocketPool() {
	int serverPort = ServerUtil.getNewPort();
	ThriftClientConfiguration configuration = new ThriftClientConfiguration();
	configuration.setSocketPool(new CommonsPoolImpl());
	configuration.setTransportFactory(new TTransportFactory());
	configuration.setSocketPool(new TestSocketPoolImpl(serverPort));
	try (ThriftClient thriftClient = new GeneralFactory(configuration).getThriftClient(TestConstants.LOCAL_HOST,
			serverPort); TTransport transport = thriftClient.getTransport()) {
		assertNull(transport);
	} catch (Exception e) {
		fail();
	}
}