Java Code Examples for org.apache.thrift.protocol.TBinaryProtocol#Factory

The following examples show how to use org.apache.thrift.protocol.TBinaryProtocol#Factory . 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: ThriftServer.java    From stratio-cassandra with Apache License 2.0 6 votes vote down vote up
public ThriftServerThread(InetAddress listenAddr,
                          int listenPort,
                          int listenBacklog,
                          TProcessor processor,
                          TTransportFactory transportFactory)
{
    // now we start listening for clients
    logger.info(String.format("Binding thrift service to %s:%s", listenAddr, listenPort));

    TServerFactory.Args args = new TServerFactory.Args();
    args.tProtocolFactory = new TBinaryProtocol.Factory(true, true);
    args.addr = new InetSocketAddress(listenAddr, listenPort);
    args.listenBacklog = listenBacklog;
    args.processor = processor;
    args.keepAlive = DatabaseDescriptor.getRpcKeepAlive();
    args.sendBufferSize = DatabaseDescriptor.getRpcSendBufferSize();
    args.recvBufferSize = DatabaseDescriptor.getRpcRecvBufferSize();
    args.inTransportFactory = transportFactory;
    args.outTransportFactory = transportFactory;
    serverEngine = new TServerCustomFactory(DatabaseDescriptor.getRpcServerType()).buildTServer(args);
}
 
Example 2
Source File: rpcServer.java    From leaf-snowflake with Apache License 2.0 6 votes vote down vote up
public static void startRPCServer(leafServer leafserver , String ip , int port) throws Exception
{
	ServerSocket serverSocket = new ServerSocket(port,10000, InetAddress.getByName(ip));

	TServerSocket serverTransport = new TServerSocket(serverSocket);

	//设置协议工厂为TBinaryProtocolFactory
	Factory proFactory = new TBinaryProtocol.Factory();
	//关联处理器leafrpc的实现
	TProcessor processor = new leafrpc.Processor<leafrpc.Iface>(new RPCService(leafserver));
	TThreadPoolServer.Args args2 = new TThreadPoolServer.Args(serverTransport);
	args2.processor(processor);
	args2.protocolFactory(proFactory);
	TServer server = new TThreadPoolServer(args2);
	LOG.info("leaf RPCServer(type:TThreadPoolServer) start at ip:port : "+ ip +":" + port );
	server.serve();
}
 
Example 3
Source File: ThriftUtil.java    From buck with Apache License 2.0 6 votes vote down vote up
public static TProtocolFactory getProtocolFactory(ThriftProtocol protocol) {
  // TODO(ruibm): Check whether the Factories are thread safe so we can static initialize
  // them just once.
  switch (protocol) {
    case JSON:
      return new TJSONProtocol.Factory();

    case COMPACT:
      return new TCompactProtocol.Factory();

    case BINARY:
      return new TBinaryProtocol.Factory();

    default:
      throw new IllegalArgumentException(
          String.format("Unknown ThriftProtocol [%s].", protocol.toString()));
  }
}
 
Example 4
Source File: ThriftITest.java    From java-specialagent with Apache License 2.0 6 votes vote down vote up
private static TServer startNewThreadPoolServer(final TServerTransport transport) {
  final TProtocolFactory protocolFactory = new TBinaryProtocol.Factory();

  final CustomHandler customHandler = new CustomHandler();
  final CustomService.Processor<CustomService.Iface> customProcessor = new CustomService.Processor<CustomService.Iface>(customHandler);

  final TThreadPoolServer.Args args = new TThreadPoolServer
    .Args(transport)
    .processorFactory(new TProcessorFactory(customProcessor))
    .protocolFactory(protocolFactory)
    .minWorkerThreads(5)
    .maxWorkerThreads(10);

  final TServer server = new TThreadPoolServer(args);
  new Thread(new Runnable() {
    @Override
    public void run() {
      server.serve();
    }
  }).start();

  return server;
}
 
Example 5
Source File: ThriftTest.java    From java-specialagent with Apache License 2.0 6 votes vote down vote up
private void startNewThreadPoolServer() throws Exception {
  final TServerTransport transport = new TServerSocket(port);
  final TProtocolFactory protocolFactory = new TBinaryProtocol.Factory();
  // TTransportFactory transportFactory = new TFramedTransport.Factory();

  final CustomHandler customHandler = new CustomHandler();
  final TProcessor customProcessor = new CustomService.Processor<CustomService.Iface>(customHandler);

  final TThreadPoolServer.Args args = new TThreadPoolServer.Args(transport)
    .processorFactory(new TProcessorFactory(customProcessor))
    .protocolFactory(protocolFactory)
    // .transportFactory(transportFactory)
    .minWorkerThreads(5)
    // .executorService(Executors.newCachedThreadPool())
    .maxWorkerThreads(10);

  server = new TThreadPoolServer(args);
  new Thread(new Runnable() {
    @Override
    public void run() {
      server.serve();
    }
  }).start();
}
 
Example 6
Source File: CoronaTaskTracker.java    From RDFS with Apache License 2.0 6 votes vote down vote up
private synchronized void initializeClusterManagerCallbackServer()
    throws IOException {
  // Create thrift RPC to serve ClusterManager
  int soTimeout = fConf.getInt(
      CORONA_TASK_TRACKER_SERVER_CLIENTTIMEOUT_KEY, 30 * 1000);
  ServerSocket serverSocket = new ServerSocket();
  serverSocket.setReuseAddress(true);
  serverSocket.bind(new InetSocketAddress(0));
  TServerSocket tSocket = new TServerSocket(serverSocket, soTimeout);
  CoronaTaskTrackerService.Processor proc =
      new CoronaTaskTrackerService.Processor(this);
  TBinaryProtocol.Factory protocolFactory =
      new TBinaryProtocol.Factory(true, true);
  TThreadPoolServer.Args args = new TThreadPoolServer.Args(tSocket);
  args.processor(proc);
  args.protocolFactory(protocolFactory);
  clusterManagerCallbackServer = new TThreadPoolServer(args);
  clusterManagerCallbackServerThread =
      new TServerThread(clusterManagerCallbackServer);
  clusterManagerCallbackServerThread.start();
  clusterManagerCallbackServerAddr = new InetAddress(
      getLocalHostname(), serverSocket.getLocalPort());
  LOG.info("SessionServer up at " + serverSocket.getLocalSocketAddress());
}
 
Example 7
Source File: TestHMSPathsFullDump.java    From incubator-sentry with Apache License 2.0 6 votes vote down vote up
@Test
public void testThrftSerialization() throws TException {
  HMSPathsDumper serDe = genHMSPathsDumper();
  long t1 = System.currentTimeMillis();
  TPathsDump pathsDump = serDe.createPathsDump();
  
  TProtocolFactory protoFactory = useCompact ? new TCompactProtocol.Factory(
      ServiceConstants.ClientConfig.SENTRY_HDFS_THRIFT_MAX_MESSAGE_SIZE_DEFAULT,
      ServiceConstants.ClientConfig.SENTRY_HDFS_THRIFT_MAX_MESSAGE_SIZE_DEFAULT)
      : new TBinaryProtocol.Factory(true, true,
      ServiceConstants.ClientConfig.SENTRY_HDFS_THRIFT_MAX_MESSAGE_SIZE_DEFAULT,
      ServiceConstants.ClientConfig.SENTRY_HDFS_THRIFT_MAX_MESSAGE_SIZE_DEFAULT);
  byte[] ser = new TSerializer(protoFactory).serialize(pathsDump);
  long serTime = System.currentTimeMillis() - t1;
  System.out.println("Serialization Time: " + serTime + ", " + ser.length);

  t1 = System.currentTimeMillis();
  TPathsDump tPathsDump = new TPathsDump();
  new TDeserializer(protoFactory).deserialize(tPathsDump, ser);
  HMSPaths fromDump = serDe.initializeFromDump(tPathsDump);
  System.out.println("Deserialization Time: " + (System.currentTimeMillis() - t1));
  Assert.assertEquals(new HashSet<String>(Arrays.asList("db9.tbl999")), fromDump.findAuthzObject(new String[]{"user", "hive", "warehouse", "db9", "tbl999"}, false));
  Assert.assertEquals(new HashSet<String>(Arrays.asList("db9.tbl999")), fromDump.findAuthzObject(new String[]{"user", "hive", "warehouse", "db9", "tbl999", "part99"}, false));
}
 
Example 8
Source File: ThriftMessageParserTest.java    From secor with Apache License 2.0 5 votes vote down vote up
private Message buildMessage(long timestamp, int timestampTwo, long timestampThree) throws Exception {
    UnitTestMessage thriftMessage = new UnitTestMessage(timestamp, "notimportant", timestampTwo, timestampThree);
    TSerializer serializer = new TSerializer(new TBinaryProtocol.Factory());
    byte[] data = serializer.serialize(thriftMessage);

    return new Message("test", 0, 0, null, data, timestamp, null);
}
 
Example 9
Source File: PinLaterClient.java    From pinlater with Apache License 2.0 5 votes vote down vote up
public PinLaterClient(String host, int port, int concurrency) {
  this.service = ClientBuilder.safeBuild(
      ClientBuilder.get()
          .hosts(new InetSocketAddress(host, port))
          .codec(ThriftClientFramedCodec.apply(Option.apply(new ClientId("pinlaterclient"))))
          .hostConnectionLimit(concurrency)
          .tcpConnectTimeout(Duration.apply(2, TimeUnit.SECONDS))
          .requestTimeout(Duration.apply(10, TimeUnit.SECONDS))
          .retries(1));
  this.iface = new PinLater.ServiceToClient(service, new TBinaryProtocol.Factory());
}
 
Example 10
Source File: ThriftServer.java    From hbase with Apache License 2.0 5 votes vote down vote up
protected TProtocolFactory getProtocolFactory() {
  TProtocolFactory protocolFactory;

  if (conf.getBoolean(COMPACT_CONF_KEY, COMPACT_CONF_DEFAULT)) {
    LOG.debug("Using compact protocol");
    protocolFactory = new TCompactProtocol.Factory();
  } else {
    LOG.debug("Using binary protocol");
    protocolFactory = new TBinaryProtocol.Factory();
  }

  return protocolFactory;
}
 
Example 11
Source File: DasServer.java    From das with Apache License 2.0 5 votes vote down vote up
public static void startServer(int port) throws TTransportException, UnknownHostException, SQLException {
    final long start = System.currentTimeMillis();
    
    String address = InetAddress.getLocalHost().getHostAddress();
    
    DasServerContext serverContext = new DasServerContext(address, port);

    DasServer server = new DasServer(address, serverContext.getWorkerId());

    TBinaryProtocol.Factory protocolFactory = new TBinaryProtocol.Factory();

    DasService.Processor<DasService.Iface> processor = new DasService.Processor<>(
            server);

    int selector = Integer.parseInt(serverContext.getServerConfigure().get(SELECTING_NUMBER));
    int worker   = Integer.parseInt(serverContext.getServerConfigure().get(WORKING_NUMBER));
    TThreadedSelectorServer ttServer = new TThreadedSelectorServer(
            new TThreadedSelectorServer.Args(new TNonblockingServerSocket(port))
                    .selectorThreads(selector)
                    .processor(processor)
                    .workerThreads(worker)
                    .protocolFactory(protocolFactory));

    startupHSServer();
    logger.info("Start server duration on port [" + port + "]: [" +
            TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis() - start) + "] seconds.");
    ttServer.serve();
}
 
Example 12
Source File: TestDriftNettyMethodInvoker.java    From drift with Apache License 2.0 5 votes vote down vote up
private static int testProcessor(TProcessor processor, List<ToIntFunction<HostAndPort>> clients)
        throws Exception
{
    try (TServerSocket serverTransport = new TServerSocket(0)) {
        TProtocolFactory protocolFactory = new TBinaryProtocol.Factory();
        TTransportFactory transportFactory = new TFramedTransport.Factory();
        TServer server = new TSimpleServer(new Args(serverTransport)
                .protocolFactory(protocolFactory)
                .transportFactory(transportFactory)
                .processor(processor));

        Thread serverThread = new Thread(server::serve);
        try {
            serverThread.start();

            int localPort = serverTransport.getServerSocket().getLocalPort();
            HostAndPort address = HostAndPort.fromParts("localhost", localPort);

            int sum = 0;
            for (ToIntFunction<HostAndPort> client : clients) {
                sum += client.applyAsInt(address);
            }
            return sum;
        }
        finally {
            server.stop();
            serverThread.interrupt();
        }
    }
}
 
Example 13
Source File: ProxyClient.java    From distributedlog with Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public ProxyClient build(SocketAddress address) {
    Service<ThriftClientRequest, byte[]> client =
        ClientBuilder.safeBuildFactory(
                clientBuilder
                        .hosts((InetSocketAddress) address)
                        .reportTo(clientStats.getFinagleStatsReceiver(address))
        ).toService();
    DistributedLogService.ServiceIface service =
            new DistributedLogService.ServiceToClient(client, new TBinaryProtocol.Factory());
    return new ProxyClient(address, client, service);
}
 
Example 14
Source File: ZipkinSpanReceiver.java    From incubator-retired-htrace with Apache License 2.0 4 votes vote down vote up
public ZipkinSpanReceiver(HTraceConfiguration conf) {
  this.transport = createTransport(conf);
  this.queue = new ArrayBlockingQueue<Span>(1000);
  this.protocolFactory = new TBinaryProtocol.Factory();
  configure(conf);
}
 
Example 15
Source File: BasicAbstractTest.java    From Thrift-Connection-Pool with Apache License 2.0 4 votes vote down vote up
protected ThriftServerInfo startServer() throws Throwable {
	// 获取一个监听端口
	final int port = choseListenPort();
	ThriftServerInfo serverInfo = new ThriftServerInfo(LOACLHOST, port);
	final AtomicReference<Throwable> ex = new AtomicReference<Throwable>();

	Thread runner = new Thread("thrift-server-starter") {
		@Override
		public void run() {
			try {
				TServerTransport serverTransport = new TServerSocket(port);
				Factory proFactory = new TBinaryProtocol.Factory();
				Processor<Iface> processor = new Example.Processor<Example.Iface>(new Example.Iface() {

					@Override
					public void pong() throws TException {
						logger.info("pong");
					}

					@Override
					public void ping() throws TException {
						logger.info("ping");
					}
				});
				Args thriftArgs = new Args(serverTransport);
				thriftArgs.processor(processor);
				thriftArgs.protocolFactory(proFactory);
				TServer tserver = new TThreadPoolServer(thriftArgs);
				servers.add(tserver);
				logger.info("启动测试服务监听:" + port);
				tserver.serve();
			} catch (TTransportException e) {
				logger.error("thrift服务器启动失败", e);
				ex.set(e);
			}
		}
	};

	runner.start();

	Throwable throwable = ex.get();
	if (throwable != null) {
		throw throwable;
	}
	// 等待服务器启动
	Thread.sleep(1000);
	return serverInfo;
}
 
Example 16
Source File: ApiModule.java    From attic-aurora with Apache License 2.0 4 votes vote down vote up
@Provides
@Singleton
TContentAwareServlet provideApiThriftServlet(AnnotatedAuroraAdmin schedulerThriftInterface) {
  /*
   * For backwards compatibility the servlet is configured to assume `application/x-thrift` and
   * `application/json` have TJSON bodies.
   *
   * Requests that have the registered MIME type for apache thrift are mapped to their respective
   * protocols. See
   * http://www.iana.org/assignments/media-types/application/vnd.apache.thrift.binary and
   * http://www.iana.org/assignments/media-types/application/vnd.apache.thrift.json for details.
   *
   * Responses have the registered MIME type so the client can decode appropriately.
   *
   * The Accept header is used to determine the response type. By default JSON is sent for any
   * value except for the binary thrift header.
   */

  ContentFactoryPair jsonFactory = new ContentFactoryPair(
      new TJSONProtocol.Factory(),
      THRIFT_JSON);
  ContentFactoryPair binFactory = new ContentFactoryPair(
      new TBinaryProtocol.Factory(),
      THRIFT_BINARY);

  // Which factory to use based on the Content-Type header of the request for reading the request.
  InputConfig inputConfig = new InputConfig(GENERIC_THRIFT, ImmutableMap.of(
      GENERIC_JSON, jsonFactory,
      GENERIC_THRIFT, jsonFactory,
      THRIFT_JSON, jsonFactory,
      THRIFT_JSON_UTF_8, jsonFactory,
      THRIFT_BINARY, binFactory
  ));

  // Which factory to use based on the Accept header of the request for the response.
  OutputConfig outputConfig = new OutputConfig(GENERIC_JSON, ImmutableMap.of(
      GENERIC_JSON, jsonFactory,
      GENERIC_THRIFT, jsonFactory,
      THRIFT_JSON, jsonFactory,
      THRIFT_JSON_UTF_8, jsonFactory,
      THRIFT_BINARY, binFactory
      ));

  // A request without a Content-Type (like from curl) should be treated as GENERIC_THRIFT
  return new TContentAwareServlet(
      new AuroraAdmin.Processor<>(schedulerThriftInterface),
      inputConfig,
      outputConfig);
}
 
Example 17
Source File: DemoClientTraditionalTEST.java    From nettythrift with Apache License 2.0 4 votes vote down vote up
@Test
public void test_AsyncClient() throws Throwable {
	Random rnd = new Random(System.nanoTime());

	TProtocolFactory[] protfacs = new TProtocolFactory[] { new TCompactProtocol.Factory(),
			new TBinaryProtocol.Factory(), new TJSONProtocol.Factory(),
			new TSimpleJSONProtocol.Factory(TCalculator.Iface.class, false) };

	TProtocolFactory protocolFactory = protfacs[rnd.nextInt(protfacs.length)];

	System.out.println("protocolFactory: " + protocolFactory);

	TAsyncClientManager clientManager = new TAsyncClientManager();
	TNonblockingTransport transport = new TNonblockingSocket(HOST, PORT);
	TCalculator.AsyncClient client = new TCalculator.AsyncClient(protocolFactory, clientManager, transport);
	final int num1 = rnd.nextInt(Integer.MAX_VALUE / 2 - 1);
	final int num2 = rnd.nextInt(Integer.MAX_VALUE / 2 - 1);

	final CountDownLatch latch = new CountDownLatch(1);
	final Throwable[] exceptions = new Throwable[1];
	AsyncMethodCallback<TCalculator.AsyncClient.add_call> resultHandler = new AsyncMethodCallback<TCalculator.AsyncClient.add_call>() {
		@Override
		public void onComplete(TCalculator.AsyncClient.add_call response) {
			System.out.println("onComplete!");
			try {
				int result = response.getResult();
				Assert.assertEquals(num1 + num2, result);
			} catch (Throwable e) {
				exceptions[0] = e;
			} finally {
				latch.countDown();
			}
		}

		@Override
		public void onError(Exception exception) {
			System.err.println("onError!");
			exception.printStackTrace();
			latch.countDown();
		}

	};
	client.add(num1, num2, resultHandler);
	latch.await();
	transport.close();
	if (exceptions[0] != null) {
		throw exceptions[0];
	}
}
 
Example 18
Source File: ServiceMethodNotFoundTest.java    From dubbo-2.6.5 with Apache License 2.0 2 votes vote down vote up
protected void init() throws Exception {

        TServerTransport serverTransport = new TServerSocket(PORT);

        DubboDemoImpl impl = new DubboDemoImpl();

        $__DemoStub.Processor processor = new $__DemoStub.Processor(impl);

        // for test
        Field field = processor.getClass().getSuperclass().getDeclaredField("processMap");

        field.setAccessible(true);

        Object obj = field.get(processor);

        if (obj instanceof Map) {
            ((Map) obj).remove("echoString");
        }
        // ~

        TBinaryProtocol.Factory bFactory = new TBinaryProtocol.Factory();

        MultiServiceProcessor wrapper = new MultiServiceProcessor();
        wrapper.addProcessor(Demo.class, processor);

        server = new TThreadPoolServer(
                new TThreadPoolServer.Args(serverTransport)
                        .inputProtocolFactory(bFactory)
                        .outputProtocolFactory(bFactory)
                        .inputTransportFactory(getTransportFactory())
                        .outputTransportFactory(getTransportFactory())
                        .processor(wrapper));

        Thread startTread = new Thread() {

            @Override
            public void run() {

                server.serve();
            }

        };

        startTread.start();

        while (!server.isServing()) {
            Thread.sleep(100);
        }

    }
 
Example 19
Source File: ServiceMethodNotFoundTest.java    From dubbox with Apache License 2.0 2 votes vote down vote up
protected void init() throws Exception {

        TServerTransport serverTransport = new TServerSocket( PORT );

        DubboDemoImpl impl = new DubboDemoImpl();

        $__DemoStub.Processor processor = new $__DemoStub.Processor( impl );

        // for test
        Field field = processor.getClass().getSuperclass().getDeclaredField( "processMap" );

        field.setAccessible( true );

        Object obj = field.get( processor );

        if ( obj instanceof Map ) {
            ( ( Map ) obj ).remove( "echoString" );
        }
        // ~

        TBinaryProtocol.Factory bFactory = new TBinaryProtocol.Factory();

        MultiServiceProcessor wrapper = new MultiServiceProcessor();
        wrapper.addProcessor( Demo.class, processor );

        server = new TThreadPoolServer(
                new TThreadPoolServer.Args( serverTransport )
                        .inputProtocolFactory( bFactory )
                        .outputProtocolFactory( bFactory )
                        .inputTransportFactory( getTransportFactory() )
                        .outputTransportFactory( getTransportFactory() )
                        .processor( wrapper ) );

        Thread startTread = new Thread() {

            @Override
            public void run() {

                server.serve();
            }

        };

        startTread.start();

        while ( !server.isServing() ) {
            Thread.sleep( 100 );
        }

    }
 
Example 20
Source File: ServiceMethodNotFoundTest.java    From dubbox-hystrix with Apache License 2.0 2 votes vote down vote up
protected void init() throws Exception {

        TServerTransport serverTransport = new TServerSocket( PORT );

        DubboDemoImpl impl = new DubboDemoImpl();

        $__DemoStub.Processor processor = new $__DemoStub.Processor( impl );

        // for test
        Field field = processor.getClass().getSuperclass().getDeclaredField( "processMap" );

        field.setAccessible( true );

        Object obj = field.get( processor );

        if ( obj instanceof Map ) {
            ( ( Map ) obj ).remove( "echoString" );
        }
        // ~

        TBinaryProtocol.Factory bFactory = new TBinaryProtocol.Factory();

        MultiServiceProcessor wrapper = new MultiServiceProcessor();
        wrapper.addProcessor( Demo.class, processor );

        server = new TThreadPoolServer(
                new TThreadPoolServer.Args( serverTransport )
                        .inputProtocolFactory( bFactory )
                        .outputProtocolFactory( bFactory )
                        .inputTransportFactory( getTransportFactory() )
                        .outputTransportFactory( getTransportFactory() )
                        .processor( wrapper ) );

        Thread startTread = new Thread() {

            @Override
            public void run() {

                server.serve();
            }

        };

        startTread.start();

        while ( !server.isServing() ) {
            Thread.sleep( 100 );
        }

    }