org.apache.thrift.transport.TFramedTransport Java Examples

The following examples show how to use org.apache.thrift.transport.TFramedTransport. 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: CoronaTaskTracker.java    From RDFS with Apache License 2.0 7 votes vote down vote up
private synchronized void initializeClusterManagerClient()
    throws IOException {
  // Connect to cluster manager thrift service
  String target = CoronaConf.getClusterManagerAddress(fConf);
  LOG.info("Connecting to Cluster Manager at " + target);
  InetSocketAddress address = NetUtils.createSocketAddr(target);
  transport = new TFramedTransport(
    new TSocket(address.getHostName(), address.getPort()));
  TProtocol protocol = new TBinaryProtocol(transport);
  client = new ClusterManagerService.Client(protocol);
  try {
    transport.open();
  } catch (TTransportException e) {
    throw new IOException(e);
  }
}
 
Example #2
Source File: TestApacheThriftMethodInvoker.java    From drift with Apache License 2.0 6 votes vote down vote up
private static int logThrift(HostAndPort address, List<LogEntry> messages)
{
    try {
        TSocket socket = new TSocket(address.getHost(), address.getPort());
        socket.open();
        try {
            TBinaryProtocol tp = new TBinaryProtocol(new TFramedTransport(socket));
            assertEquals(new scribe.Client(tp).Log(messages), ResultCode.OK);
        }
        finally {
            socket.close();
        }
    }
    catch (TException e) {
        throw new RuntimeException(e);
    }
    return 1;
}
 
Example #3
Source File: ConnectionPool.java    From suro with Apache License 2.0 6 votes vote down vote up
public void connect() throws Exception {
    TSocket socket = new TSocket(server.getHost(), server.getPort(), config.getConnectionTimeout());
    socket.getSocket().setTcpNoDelay(true);
    socket.getSocket().setKeepAlive(true);
    socket.getSocket().setSoLinger(true, 0);
    transport = new TFramedTransport(socket);
    transport.open();

    TProtocol protocol = new TBinaryProtocol(transport);

    client = new SuroServer.Client(protocol);
    ServiceStatus status = client.getStatus();
    if (status != ServiceStatus.ALIVE) {
        transport.close();
        throw new RuntimeException(server + " IS NOT ALIVE!!!");
    }
}
 
Example #4
Source File: AbstractTransportPool.java    From jigsaw-payment with Apache License 2.0 6 votes vote down vote up
/**
 * 根据rc的设置来确定创建什么类型的transport;
 *
 * @param instance
 * @return
 */
protected TTransport createNativeTransport(
        ServiceInstance<RpcPayload> instance) {
    TSocket socket = new TSocket(instance.getAddress(), instance.getPort());
    socket.setTimeout(socketTimeout);

    RpcPayload server = instance.getPayload();
    if ((server == null) || (server.getTransport() == null)
            || (server.getTransport().equals("socket"))) {
        return socket;
    } else if ("framed-transport".equals(server.getTransport())) {
        return new TFramedTransport(socket);
    }

    // for default, use TSocket;
    return socket;
}
 
Example #5
Source File: TestDriftNettyMethodInvoker.java    From drift with Apache License 2.0 6 votes vote down vote up
private static List<DriftLogEntry> testMethodInvoker(ServerMethodInvoker methodInvoker)
{
    int invocationCount = testMethodInvoker(methodInvoker, ImmutableList.of(
            address -> logThrift(address, MESSAGES, new TTransportFactory(), new TBinaryProtocol.Factory()),
            address -> logThrift(address, MESSAGES, new TTransportFactory(), new TCompactProtocol.Factory()),
            address -> logThrift(address, MESSAGES, new TFramedTransport.Factory(), new TBinaryProtocol.Factory()),
            address -> logThrift(address, MESSAGES, new TFramedTransport.Factory(), new TCompactProtocol.Factory()),
            address -> logThriftAsync(address, MESSAGES),
            address -> logNiftyInvocationHandler(address, DRIFT_MESSAGES, Transport.UNFRAMED, BINARY),
            address -> logNiftyInvocationHandler(address, DRIFT_MESSAGES, Transport.UNFRAMED, Protocol.COMPACT),
            address -> logNiftyInvocationHandler(address, DRIFT_MESSAGES, Transport.UNFRAMED, Protocol.FB_COMPACT),
            address -> logNiftyInvocationHandler(address, DRIFT_MESSAGES, FRAMED, BINARY),
            address -> logNiftyInvocationHandler(address, DRIFT_MESSAGES, FRAMED, Protocol.COMPACT),
            address -> logNiftyInvocationHandler(address, DRIFT_MESSAGES, FRAMED, Protocol.FB_COMPACT),
            address -> logNiftyInvocationHandler(address, DRIFT_MESSAGES, Transport.HEADER, BINARY),
            address -> logNiftyInvocationHandler(address, DRIFT_MESSAGES, Transport.HEADER, Protocol.FB_COMPACT)));

    return newArrayList(concat(nCopies(invocationCount, DRIFT_MESSAGES)));
}
 
Example #6
Source File: TestRingCache.java    From stratio-cassandra with Apache License 2.0 6 votes vote down vote up
private void setup(String server, int port) throws Exception
{
    /* Establish a thrift connection to the cassandra instance */
    TSocket socket = new TSocket(server, port);
    System.out.println(" connected to " + server + ":" + port + ".");
    TBinaryProtocol binaryProtocol = new TBinaryProtocol(new TFramedTransport(socket));
    Cassandra.Client cassandraClient = new Cassandra.Client(binaryProtocol);
    socket.open();
    thriftClient = cassandraClient;
    String seed = DatabaseDescriptor.getSeeds().iterator().next().getHostAddress();
    conf = new Configuration();
    ConfigHelper.setOutputPartitioner(conf, DatabaseDescriptor.getPartitioner().getClass().getName());
    ConfigHelper.setOutputInitialAddress(conf, seed);
    ConfigHelper.setOutputRpcPort(conf, Integer.toString(DatabaseDescriptor.getRpcPort()));

}
 
Example #7
Source File: ThriftReader.java    From singer with Apache License 2.0 6 votes vote down vote up
public ThriftReader(
    String path,
    TBaseFactory<T> baseFactory,
    TProtocolFactory protocolFactory,
    int readBufferSize,
    int maxMessageSize) throws IOException {
  Preconditions.checkArgument(!Strings.isNullOrEmpty(path));
  Preconditions.checkNotNull(protocolFactory);

  this.byteOffsetInputStream = new ByteOffsetInputStream(
      new RandomAccessFile(path, "r"), readBufferSize);
  this.framedTransport = new TFramedTransport(new TIOStreamTransport(this
      .byteOffsetInputStream), maxMessageSize);
  this.baseFactory = Preconditions.checkNotNull(baseFactory);
  this.protocol = protocolFactory.get(this.framedTransport);
}
 
Example #8
Source File: DasServerManager.java    From das with Apache License 2.0 6 votes vote down vote up
private boolean isAvailable(int port){
    try (TSocket transport = new TSocket("localhost", port, 5 * 1000)){
        TFramedTransport ft = new TFramedTransport(transport);
        TBinaryProtocol protocol = new TBinaryProtocol(ft);
        transport.open();

        DasCheckRequest request = new DasCheckRequest()
                .setAppId("server manager")
                .setClientAddress("")
                .setDasClientVersion("")
                .setPpdaiClientVersion("");

        DasServerStatus serverStatus =  new DasService.Client(protocol).check(request);
        return serverStatus.isOnline();
    } catch (Exception e) {
        dalLogger.error("isAvailable", e);
        logger.error(e.getMessage(), e);
        return false;
    }
}
 
Example #9
Source File: ThriftServer.java    From plow with Apache License 2.0 6 votes vote down vote up
@PostConstruct
public void start() {
    logger.info("Starting thift server " + name + " on port " + port);

    try {
        transport = new TNonblockingServerSocket(port);
        server = new TThreadedSelectorServer(
                new TThreadedSelectorServer.Args(transport)
            .processor(processor)
            .workerThreads(threads)
            .selectorThreads((threads / 16) + 1)
            .protocolFactory(protocolFactory)
            .transportFactory(new TFramedTransport.Factory()));
        thread.start();

    } catch (TTransportException e) {
        throw new RuntimeException("Unable to start thrift server " + e, e);
    }
}
 
Example #10
Source File: ThriftServer.java    From luxun with Apache License 2.0 6 votes vote down vote up
public ThriftServer(QueueService.Iface queueService, ServerConfig serverConfig, ThriftServerStats stats) throws TTransportException {
		this.queueService = queueService;
		this.serverConfig = serverConfig;
        this.stats = stats;
        
        // assemble thrift server
        TProcessor tprocessor = new QueueService.Processor(this.queueService);
        TNonblockingServerSocket tnbSocketTransport = new TNonblockingServerSocket(serverConfig.getPort());
        TNonblockingServer.Args tnbArgs = new TNonblockingServer.Args(tnbSocketTransport);
        tnbArgs.processor(tprocessor);
        // Nonblocking server mode must use TFramedTransport
        tnbArgs.transportFactory(new TFramedTransport.Factory());
        tnbArgs.protocolFactory(new TBinaryProtocol.Factory());
        
        this.server = new TNonblockingServer(tnbArgs);
        
//        THsHaServer.Args thhArgs = new THsHaServer.Args(tnbSocketTransport);
//        thhArgs.processor(tprocessor);
//        // Nonblocking server mode must use TFramedTransport
//        thhArgs.transportFactory(new TFramedTransport.Factory());
//        thhArgs.protocolFactory(new TBinaryProtocol.Factory());
//
//        this.server = new THsHaServer(thhArgs);
    
        this.serverThread = new ServerThread(this.server);
	}
 
Example #11
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 #12
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 #13
Source File: ThriftServerTest2.java    From ThriftJ with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args){
	ExecutorService es = Executors.newFixedThreadPool(2);
	for(int i=0; i<ports.length; i++){
		final int index = i;
		es.execute(new Runnable() {
			@Override
			public void run() {
				try{
					TNonblockingServerSocket socket = new TNonblockingServerSocket(ports[index]);
					TestThriftJ.Processor processor = new TestThriftJ.Processor(new QueryImp());
					TNonblockingServer.Args arg = new TNonblockingServer.Args(socket);
					arg.protocolFactory(new TBinaryProtocol.Factory());
					arg.transportFactory(new TFramedTransport.Factory());
					arg.processorFactory(new TProcessorFactory(processor));
					TServer server = new TNonblockingServer (arg);
					
					logger.info("127.0.0.1:" + ports[index] + " start");
					server.serve();
				}catch(Exception e){
					logger.error("127.0.0.1:" + ports[index] + " error");
				}
			}
		});
	}
}
 
Example #14
Source File: ThriftServerTest.java    From ThriftJ with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args){
	ExecutorService es = Executors.newFixedThreadPool(2);
	for(int i=0; i<ports.length; i++){
		final int index = i;
		es.execute(new Runnable() {
			@Override
			public void run() {
				try{
					TNonblockingServerSocket socket = new TNonblockingServerSocket(ports[index]);
					TestThriftJ.Processor processor = new TestThriftJ.Processor(new QueryImp());
					TNonblockingServer.Args arg = new TNonblockingServer.Args(socket);
					arg.protocolFactory(new TBinaryProtocol.Factory());
					arg.transportFactory(new TFramedTransport.Factory());
					arg.processorFactory(new TProcessorFactory(processor));
					TServer server = new TNonblockingServer(arg);
					
					logger.info("127.0.0.1:" + ports[index] + " start");
					server.serve();
				}catch(Exception e){
					logger.error("127.0.0.1:" + ports[index] + " error");
				}
			}
		});
	}
}
 
Example #15
Source File: rpcClient.java    From leaf-snowflake with Apache License 2.0 6 votes vote down vote up
public static void startClient2(String ip ,int port ,int timeout) throws Exception
{
	TTransport transport = new TFramedTransport(new TSocket(ip,port,timeout));
	TProtocol protocol = new TBinaryProtocol(transport);
	leafrpc.Client client = new leafrpc.Client(protocol);
	transport.open();

	for(int i = 0; i< 1000000; i++)
	{
		client.getID("");
		if (i % 100000 == 0)
		{
			System.out.println(Thread.currentThread().getName() + " " + client.getID(""));
		}
		//ai.incrementAndGet();
	}
	transport.close();
}
 
Example #16
Source File: CoronaAdmin.java    From RDFS with Apache License 2.0 6 votes vote down vote up
/**
 * Turns on the Safe Mode if safeMode is true. Turns off the Safe Mode if
 * safeMode is false.
 * @param safeMode Is true if we want the Safe Mode to be on. false
 *                 otherwise.
 * @return 0 if successful.
 * @throws IOException
 */
private int setSafeMode(boolean safeMode) throws IOException {
  // Get the current configuration
  CoronaConf conf = new CoronaConf(getConf());

  InetSocketAddress address = NetUtils.createSocketAddr(conf
    .getClusterManagerAddress());
  TFramedTransport transport = new TFramedTransport(
    new TSocket(address.getHostName(), address.getPort()));
  ClusterManagerService.Client client = new ClusterManagerService.Client(
    new TBinaryProtocol(transport));

  try {
    transport.open();
    if (client.setSafeMode(safeMode)) {
      System.out.println("The safeMode is: " +
                          (safeMode ? "ON" : "OFF"));
    } else {
      System.err.println("Could not set the safeMode flag");
    }
  } catch (TException e) {
    throw new IOException(e);
  }

  return 0;
}
 
Example #17
Source File: CassandraConnection.java    From learning-hadoop with Apache License 2.0 6 votes vote down vote up
/**
 * Construct an CassandaraConnection with optional authentication.
 * 
 * @param host the host to connect to
 * @param port the port to use
 * @param username the username to authenticate with (may be null
 * for no authentication)
 * @param password the password to authenticate with (may be null
 * for no authentication)
 * @throws Exception if the connection fails
 */
public CassandraConnection(String host, int port,
    String username, String password, int timeout) throws Exception {
  TSocket socket = new TSocket(host, port);
  if (timeout > 0) {
    socket.setTimeout(timeout);
  }
  
  m_transport = new TFramedTransport(socket);
  TProtocol protocol = new TBinaryProtocol(m_transport);
  m_client = new Cassandra.Client(protocol);      
  m_transport.open();
  
  if (!Const.isEmpty(username) && !Const.isEmpty(password)) {
    Map<String, String> creds = new HashMap<String, String>();
    creds.put("username", username);
    creds.put("password", password);
    m_client.login(new AuthenticationRequest(creds));
  }
}
 
Example #18
Source File: TestThriftServiceStarter.java    From thrift-client-pool-java with Artistic License 2.0 6 votes vote down vote up
public static void main(String[] args) {
    int port = 9090;

    try {
        TServerTransport serverTransport = new TServerSocket(port);

        Args processor = new TThreadPoolServer.Args(serverTransport)
                .inputTransportFactory(new TFramedTransport.Factory())
                .outputTransportFactory(new TFramedTransport.Factory())
                .processor(new Processor<>(new TestThriftServiceHandler()));
        //            processor.maxWorkerThreads = 20;
        TThreadPoolServer server = new TThreadPoolServer(processor);

        System.out.println("Starting the server...");
        server.serve();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example #19
Source File: TestThriftClientPool.java    From thrift-client-pool-java with Artistic License 2.0 6 votes vote down vote up
@BeforeClass
public static void setUp() {
    int port = 9090;

    try {
        TServerTransport serverTransport = new TServerSocket(port);

        Args processor = new TThreadPoolServer.Args(serverTransport)
                .inputTransportFactory(new TFramedTransport.Factory())
                .outputTransportFactory(new TFramedTransport.Factory())
                .processor(new Processor<>(new TestThriftServiceHandler()));
        //            processor.maxWorkerThreads = 20;
        TThreadPoolServer server = new TThreadPoolServer(processor);

        logger.info("Starting test server...");
        new Thread(server::serve).start();
        Thread.sleep(1000); // waiting server init
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example #20
Source File: TestThriftClientPool.java    From thrift-client-pool-java with Artistic License 2.0 6 votes vote down vote up
@Test
public void testReusingIFace() throws TException {
    List<ServiceInfo> serverList = Collections
            .singletonList(new ServiceInfo("127.0.0.1", 9090));
    PoolConfig config = new PoolConfig();
    config.setFailover(true);
    config.setTimeout(1000);
    ThriftClientPool<TestThriftService.Client> pool = new ThriftClientPool<>(serverList,
            transport -> new Client(new TBinaryProtocol(new TFramedTransport(transport))),
            config);

    Iface iface = pool.iface();
    iface.echo("Hello!");
    boolean exceptionThrow = false;
    try {
        iface.echo("Hello again!");
    } catch (IllegalStateException e) {
        logger.info("exception thrown expected!", e);
        exceptionThrow = true;
    }
    Assert.assertTrue("exception must thrown", exceptionThrow);
}
 
Example #21
Source File: ScribeSource.java    From mt-flume with Apache License 2.0 6 votes vote down vote up
public void run() {
  try {
    Scribe.Processor processor = new Scribe.Processor(new Receiver());
    TNonblockingServerTransport transport = new TNonblockingServerSocket(port);
    THsHaServer.Args args = new THsHaServer.Args(transport);
    
    args.workerThreads(workers);
    args.processor(processor);
    args.transportFactory(new TFramedTransport.Factory());
    args.protocolFactory(new TBinaryProtocol.Factory(false, false));

    server = new THsHaServer(args);

    LOG.info("Starting Scribe Source on port " + port);

    server.serve();
  } catch (Exception e) {
    LOG.warn("Scribe failed", e);
  }
}
 
Example #22
Source File: ThriftClient.java    From deep-spark with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a new client for the specified host, setting the specified keyspace.
 *
 * @param host     the Cassandra host address.
 * @param port     the Cassandra host RPC port.
 * @param keyspace the name of the Cassandra keyspace to set.
 * @return a new client for the specified host, setting the specified keyspace.
 * @throws TException if there is any problem with the {@code set_keyspace} call.
 */
public static ThriftClient build(String host, int port, String keyspace) throws TException {
    TTransport transport = new TFramedTransport(new TSocket(host, port));
    TProtocol protocol = new TBinaryProtocol(transport);
    ThriftClient client = new ThriftClient(protocol);
    transport.open();
    if (keyspace != null) {
        client.set_keyspace(keyspace);
    }
    return client;
}
 
Example #23
Source File: TWritelogTransportFactory.java    From ThriftBook with Apache License 2.0 5 votes vote down vote up
@Override
public TTransport getTransport(TTransport trans) {
  TSimpleFileTransport log;
  try {
    log = new TSimpleFileTransport("svr_log_" + ++clientID, false, true);
    log.open();
  } catch (TTransportException ex) {
    log = null;
  }
  TFramedTransport frame = new TFramedTransport(trans);
  return new TTeeTransport(frame, log);
}
 
Example #24
Source File: AbstractCassandraOutputAdapter.java    From OpenRate with Apache License 2.0 5 votes vote down vote up
/**
 * Tries to connect to Cassandra.
 *
 * @throws OpenRate.exception.ProcessingException
 */
public void openCassandraConnection() throws ProcessingException {
  tr = new TFramedTransport(new TSocket(cassandraIPAddr, 9160));
  TProtocol proto = new TBinaryProtocol(tr);
  client = new Cassandra.Client(proto);

  try {
    tr.open();
  } catch (TTransportException ex) {
    message = "Transport exception opening Cassandra transport";
    throw new ProcessingException(message, ex, getSymbolicName());
  }
}
 
Example #25
Source File: FactoryServer.java    From ThriftBook with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws TTransportException {
  TServerSocket trans_svr = new TServerSocket(9090);
  TProcessor proc = new Message.Processor<>(new MessageHandler());
  
  TServer server = new TThreadPoolServer(
          new TThreadPoolServer.Args(trans_svr)
            .processor(proc)
            .protocolFactory(new TJSONProtocol.Factory())
            .inputTransportFactory(new TFramedTransport.Factory())
            .outputTransportFactory(new TWritelogTransportFactory(100)));
  server.serve();
}
 
Example #26
Source File: AbstractCassandraOutputAdapter.java    From OpenRate with Apache License 2.0 5 votes vote down vote up
/**
 * Tries to connect to Cassandra.
 *
 * @throws OpenRate.exception.InitializationException
 */
public void initCassandraConnection() throws InitializationException {
  tr = new TFramedTransport(new TSocket(cassandraIPAddr, cassandraPort));
  TProtocol proto = new TBinaryProtocol(tr);
  client = new Cassandra.Client(proto);

  try {
    tr.open();
  } catch (TTransportException ex) {
    message = "Transport exception opening Cassandra transport";
    throw new InitializationException(message, ex, getSymbolicName());
  }
}
 
Example #27
Source File: SSLTransportFactory.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
@Override
public TTransport openTransport(String host, int port) throws Exception
{
    TSSLTransportFactory.TSSLTransportParameters params = new TSSLTransportFactory.TSSLTransportParameters(protocol, cipherSuites);
    params.setTrustStore(truststore, truststorePassword);
    if (null != keystore)
        params.setKeyStore(keystore, keystorePassword);
    TTransport trans = TSSLTransportFactory.getClientSocket(host, port, SOCKET_TIMEOUT, params);
    int frameSize = 15 * 1024 * 1024; // 15 MiB
    return new TFramedTransport(trans, frameSize);
}
 
Example #28
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 #29
Source File: PigTestBase.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
protected static Cassandra.Client getClient() throws TTransportException
{
    TTransport tr = new TFramedTransport(new TSocket("localhost", 9170));
    TProtocol proto = new TBinaryProtocol(tr);
    Cassandra.Client client = new Cassandra.Client(proto);
    tr.open();
    return client;
}
 
Example #30
Source File: TFramedTransportFactory.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
public TTransport openTransport(String host, int port) throws TTransportException
{
    TSocket socket = new TSocket(host, port);
    TTransport transport = new TFramedTransport(socket, thriftFramedTransportSizeMb * 1024 * 1024);
    transport.open();
    return transport;
}