org.apache.thrift.transport.TNonblockingTransport Java Examples

The following examples show how to use org.apache.thrift.transport.TNonblockingTransport. 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: TDisruptorServer.java    From disruptor_thrift_server with Apache License 2.0 6 votes vote down vote up
@Override
protected void processKey(SelectionKey key) throws IOException
{
    if (!key.isAcceptable())
        return;

    try
    {
        // accept the connection
        SelectorThread selector = selectorLoadBalancer.nextSelector();
        selector.subscribe((TNonblockingTransport) serverTransport.accept());
        selector.wakeupSelector();
    }
    catch (TTransportException tte)
    {
        // accept() shouldn't be NULL if fine because are are raising for a socket
        logger.debug("Non-fatal exception trying to accept!", tte);
    }
}
 
Example #2
Source File: TThreadedSelectorServerWithFix.java    From phoenix-tephra with Apache License 2.0 6 votes vote down vote up
/**
 * Accept a new connection.
 */
private void handleAccept() {
  final TNonblockingTransport client = doAccept();
  if (client != null) {
    // Pass this connection to a selector thread
    final SelectorThread targetThread = threadChooser.nextThread();

    if (args.acceptPolicy == Args.AcceptPolicy.FAST_ACCEPT || invoker == null) {
      doAddAccept(targetThread, client);
    } else {
      // FAIR_ACCEPT
      try {
        invoker.submit(new Runnable() {
          public void run() {
            doAddAccept(targetThread, client);
          }
        });
      } catch (RejectedExecutionException rx) {
        LOGGER.warn("ExecutorService rejected accept registration!", rx);
        // close immediately
        client.close();
      }
    }
  }
}
 
Example #3
Source File: TThreadedSelectorServerWithFix.java    From phoenix-tephra with Apache License 2.0 6 votes vote down vote up
/**
 * Hands off an accepted connection to be handled by this thread. This
 * method will block if the queue for new connections is at capacity.
 *
 * @param accepted
 *          The connection that has been accepted.
 * @return true if the connection has been successfully added.
 */
public boolean addAcceptedConnection(TNonblockingTransport accepted) {
  try {
    while (!acceptedQueue.offer(accepted, 200, TimeUnit.MILLISECONDS)) {
      // If server is stopped, then return false.
      if (stopped_) {
        return false;
      }
    }
  } catch (InterruptedException e) {
    LOGGER.warn("Interrupted while adding accepted connection!", e);
    return false;
  }
  selector.wakeup();
  return true;
}
 
Example #4
Source File: ThriftTest.java    From java-specialagent with Apache License 2.0 5 votes vote down vote up
@Test
public void async() throws Exception {
  TestUtil.setGlobalTracer(tracer);

  startAsyncServer();

  final Factory protocolFactory = new Factory();
  final TNonblockingTransport transport = new TNonblockingSocket("localhost", port);
  final TAsyncClientManager clientManager = new TAsyncClientManager();
  final AsyncClient asyncClient = new AsyncClient(protocolFactory, clientManager, transport);
  final AtomicInteger counter = new AtomicInteger();
  asyncClient.say("Async", "World", new AsyncMethodCallback<String>() {
    @Override
    public void onComplete(final String response) {
      assertEquals("Say Async World", response);
      assertNotNull(GlobalTracer.get().activeSpan());
      counter.incrementAndGet();
    }

    @Override
    public void onError(final Exception exception) {
      exception.printStackTrace();
    }
  });

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

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

  assertNull(tracer.activeSpan());
  verify(tracer, times(2)).buildSpan(anyString());
  verify(tracer, times(1)).inject(any(SpanContext.class), any(Format.class), any());
}
 
Example #5
Source File: Buffer.java    From disruptor_thrift_server with Apache License 2.0 5 votes vote down vote up
public int writeTo(TNonblockingTransport transport, int start, int count) throws IOException
{
    ByteBuffer dup = buffer.duplicate();
    dup.position(start).limit(start + count);

    return transport.write(dup);
}
 
Example #6
Source File: TDisruptorServer.java    From disruptor_thrift_server with Apache License 2.0 5 votes vote down vote up
@Override
protected void selectorIterationComplete() throws IOException
{
    TNonblockingTransport newClient;

    while ((newClient = newConnections.poll()) != null)
    {
        SelectionKey clientKey = newClient.registerSelector(selector, SelectionKey.OP_READ);
        clientKey.attach(new Message(newClient, clientKey, thriftFactories, useHeapBasedAllocation, alwaysReallocateBuffers));
    }
}
 
Example #7
Source File: Message.java    From disruptor_thrift_server with Apache License 2.0 5 votes vote down vote up
public Message(TNonblockingTransport trans,
               SelectionKey key,
               ThriftFactories factories,
               boolean heapBasedAllocation,
               boolean reallocateBuffers)
{
    frameSizeBuffer = Buffer.allocate(4, heapBasedAllocation);
    transport = trans;
    selectionKey = key;
    thriftFactories = factories;
    useHeapBasedAllocation = heapBasedAllocation;
    alwaysReallocateBuffers = reallocateBuffers;
}
 
Example #8
Source File: FrameBufferTransportInjectInterceptor.java    From pinpoint with Apache License 2.0 5 votes vote down vote up
protected final Socket getRootSocket(Object target) {
    if (target instanceof TNonblockingTransportFieldGetter) {
        TNonblockingTransport inTrans = ((TNonblockingTransportFieldGetter) target)._$PINPOINT$_getTNonblockingTransport();
        if (inTrans != null) {
            if (inTrans instanceof SocketFieldAccessor) {
                return ((SocketFieldAccessor) inTrans)._$PINPOINT$_getSocket();
            } else {
                if (isDebug) {
                    logger.debug("Invalid target object. Need field accessor({}).", SocketFieldAccessor.class.getName());
                }
            }
        }
    }
    return null;
}
 
Example #9
Source File: TAsyncMethodCallConstructInterceptor.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 (isDebug) {
        logger.afterInterceptor(target, args, result, throwable);
    }
    if (validate(target)) {
        TNonblockingTransport transport = ((TNonblockingTransportFieldGetter)target)._$PINPOINT$_getTNonblockingTransport();
        if (validateTransport(transport)) {
            SocketAddress socketAddress = ((SocketAddressFieldAccessor)transport)._$PINPOINT$_getSocketAddress();
            ((SocketAddressFieldAccessor)target)._$PINPOINT$_setSocketAddress(socketAddress);
        }
    }
}
 
Example #10
Source File: GeneralFactory.java    From ikasoa with MIT License 5 votes vote down vote up
/**
 * 获取客户端AsyncService对象
 */
@Override
public AsyncService getAsyncService(TNonblockingTransport transport, String serviceName) throws IkasoaException {
	if (ObjectUtil.isNull(transport))
		throw new IllegalArgumentException("'transport' is null !");
	try {
		return StringUtil.isEmpty(serviceName)
				? new AsyncServiceClientImpl((TProtocolFactory) new TCompactProtocol.Factory(), transport)
				: new AsyncServiceClientImpl(new AsyncMultiplexedProtocolFactory(serviceName), transport);
	} catch (IOException e) {
		throw new IkasoaException(e);
	}
}
 
Example #11
Source File: TThreadedSelectorServerWithFix.java    From phoenix-tephra with Apache License 2.0 5 votes vote down vote up
private void registerAccepted(TNonblockingTransport accepted) {
  SelectionKey clientKey = null;
  try {
    clientKey = accepted.registerSelector(selector, SelectionKey.OP_READ);

    FrameBuffer frameBuffer = new FrameBuffer(accepted, clientKey, SelectorThread.this);
    clientKey.attach(frameBuffer);
  } catch (IOException e) {
    LOGGER.warn("Failed to register accepted connection to selector!", e);
    if (clientKey != null) {
      cleanupSelectionKey(clientKey);
    }
    accepted.close();
  }
}
 
Example #12
Source File: TThreadedSelectorServerWithFix.java    From phoenix-tephra with Apache License 2.0 5 votes vote down vote up
private void processAcceptedConnections() {
  // Register accepted connections
  while (!stopped_) {
    TNonblockingTransport accepted = acceptedQueue.poll();
    if (accepted == null) {
      break;
    }
    registerAccepted(accepted);
  }
}
 
Example #13
Source File: AsyncMockServerRuleTest.java    From thrift-mock with Apache License 2.0 5 votes vote down vote up
@Test
public void testAsyncServerRule() throws Exception {
  String responseMsg = "asyncHello";
  Response expectHelloResponse = new Response(200, responseMsg);
  server.setExpectReturn("sayHello", expectHelloResponse);

  TNonblockingTransport transport = new TNonblockingSocket("127.0.0.1",
                                                           9999,
                                                           3000);
  HelloService.AsyncIface client = new HelloService.AsyncClient(new TBinaryProtocol.Factory(),
                                                                new TAsyncClientManager(),
                                                                transport);
  Request request = new Request();
  request.setMsg("hello async!");
  Response result = new Response();
  Stopwatch stopwatch = Stopwatch.createStarted();
  client.sayHello(request, new AsyncMethodCallback() {
    @Override
    public void onComplete(Object response) {
      System.out.println("cost:" + stopwatch.elapsed(TimeUnit.MILLISECONDS));
      result.setCode(((Response)response).getCode());
      result.setResponseMsg(((Response)response).getResponseMsg());
    }

    @Override
    public void onError(Exception exception) {
      System.out.println(exception.getMessage());
    }
  });
  Thread.sleep(50);
  System.out.println("final result:" + result);
  Assert.assertTrue(result.getCode() == 200);
  Assert.assertTrue(responseMsg.equals(result.getResponseMsg()));
}
 
Example #14
Source File: ThriftTest.java    From java-specialagent with Apache License 2.0 5 votes vote down vote up
@Test
public void asyncWithoutArgs() throws Exception {
  TestUtil.setGlobalTracer(tracer);

  startAsyncServer();

  final Factory protocolFactory = new Factory();

  final TNonblockingTransport transport = new TNonblockingSocket("localhost", port);
  final TAsyncClientManager clientManager = new TAsyncClientManager();
  final AsyncClient asyncClient = new AsyncClient(protocolFactory, clientManager, transport);
  final AtomicInteger counter = new AtomicInteger();
  asyncClient.withoutArgs(new AsyncMethodCallback<String>() {
    @Override
    public void onComplete(final String response) {
      assertEquals("no args", response);
      assertNotNull(GlobalTracer.get().activeSpan());
      counter.incrementAndGet();
    }

    @Override
    public void onError(final Exception exception) {
      exception.printStackTrace();
    }
  });

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

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

  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: ThriftTest.java    From java-specialagent with Apache License 2.0 5 votes vote down vote up
@Test
public void asyncMany() throws Exception {
  TestUtil.setGlobalTracer(tracer);

  startAsyncServer();

  final AtomicInteger counter = new AtomicInteger();
  for (int i = 0; i < 4; ++i) {
    final Factory protocolFactory = new Factory();
    final TNonblockingTransport transport = new TNonblockingSocket("localhost", port);
    final TAsyncClientManager clientManager = new TAsyncClientManager();
    final AsyncClient asyncClient = new AsyncClient(protocolFactory, clientManager, transport);
    asyncClient.withDelay(1, new AsyncMethodCallback<String>() {
      @Override
      public void onComplete(final String response) {
        assertEquals("delay 1", response);
        assertNotNull(GlobalTracer.get().activeSpan());
        counter.incrementAndGet();
      }

      @Override
      public void onError(final Exception exception) {
        exception.printStackTrace();
      }
    });
  }

  await().atMost(15, TimeUnit.SECONDS).until(TestUtil.reportedSpansSize(tracer), equalTo(8));
  assertEquals(4, counter.get());

  final List<MockSpan> spans = tracer.finishedSpans();
  assertEquals(8, spans.size());

  assertNull(tracer.activeSpan());
  verify(tracer, times(8)).buildSpan(anyString());
  verify(tracer, times(4)).inject(any(SpanContext.class), any(Format.class), any());
}
 
Example #16
Source File: ThriftTest.java    From java-specialagent with Apache License 2.0 5 votes vote down vote up
@Test
public void oneWayAsync() throws Exception {
  TestUtil.setGlobalTracer(tracer);

  startAsyncServer();

  final Factory protocolFactory = new Factory();
  final TNonblockingTransport transport = new TNonblockingSocket("localhost", port);
  final TAsyncClientManager clientManager = new TAsyncClientManager();
  final AsyncClient asyncClient = new AsyncClient(protocolFactory, clientManager, transport);
  final AtomicInteger counter = new AtomicInteger();
  asyncClient.oneWay(new AsyncMethodCallback<Void>() {
    @Override
    public void onComplete(final Void response) {
      assertNotNull(GlobalTracer.get().activeSpan());
      counter.incrementAndGet();
    }

    @Override
    public void onError(final Exception exception) {
      exception.printStackTrace();
    }
  });

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

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

  assertNull(tracer.activeSpan());
  verify(tracer, times(2)).buildSpan(anyString());
  verify(tracer, times(1)).inject(any(SpanContext.class), any(Format.class), any());
}
 
Example #17
Source File: TThreadedSelectorServerWithFix.java    From phoenix-tephra with Apache License 2.0 5 votes vote down vote up
private static BlockingQueue<TNonblockingTransport> createDefaultAcceptQueue(int queueSize) {
  if (queueSize == 0) {
    // Unbounded queue
    return new LinkedBlockingQueue<TNonblockingTransport>();
  }
  return new ArrayBlockingQueue<TNonblockingTransport>(queueSize);
}
 
Example #18
Source File: TThreadedSelectorServerWithFix.java    From phoenix-tephra with Apache License 2.0 5 votes vote down vote up
private TNonblockingTransport doAccept() {
  try {
    return (TNonblockingTransport) serverTransport.accept();
  } catch (TTransportException tte) {
    // something went wrong accepting.
    LOGGER.warn("Exception trying to accept!", tte);
    return null;
  }
}
 
Example #19
Source File: CustomTNonBlockingServer.java    From stratio-cassandra with Apache License 2.0 4 votes vote down vote up
public TNonblockingTransport getTransport() {
    return this.trans_;
}
 
Example #20
Source File: Buffer.java    From disruptor_thrift_server with Apache License 2.0 4 votes vote down vote up
public int readFrom(TNonblockingTransport transport) throws IOException
{
    return transport.read(buffer);
}
 
Example #21
Source File: TDisruptorServer.java    From disruptor_thrift_server with Apache License 2.0 4 votes vote down vote up
public void subscribe(TNonblockingTransport newClient)
{
    newConnections.add(newClient); // always returns true as queue is unbounded
}
 
Example #22
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 #23
Source File: CustomTNonBlockingServer.java    From stratio-cassandra with Apache License 2.0 4 votes vote down vote up
public CustomFrameBuffer(final TNonblockingTransport trans,
       final SelectionKey selectionKey,
       final AbstractSelectThread selectThread) {
super(trans, selectionKey, selectThread);
     }
 
Example #24
Source File: GeneralFactory.java    From ikasoa with MIT License 4 votes vote down vote up
/**
 * 获取客户端AsyncService对象
 */
@Override
public AsyncService getAsyncService(TNonblockingTransport transport) throws IkasoaException {
	return getAsyncService(transport, null);
}
 
Example #25
Source File: CallBack.java    From ikasoa with MIT License 4 votes vote down vote up
public CallBack(String arg, AsyncMethodCallback<String> resultHandler, TAsyncClient client,
		TProtocolFactory protocolFactory, TNonblockingTransport transport) throws TException {
	super(client, protocolFactory, transport, resultHandler, false);
	this.arg = arg;
}
 
Example #26
Source File: AsyncServiceClientImpl.java    From ikasoa with MIT License 4 votes vote down vote up
public AsyncServiceClientImpl(TProtocolFactory protocolFactory, TNonblockingTransport transport)
		throws IOException {
	super(protocolFactory, new TAsyncClientManager(), transport);
}
 
Example #27
Source File: AsyncServiceClientImpl.java    From ikasoa with MIT License 4 votes vote down vote up
public AsyncServiceClientImpl(TProtocolFactory protocolFactory, TAsyncClientManager manager,
		TNonblockingTransport transport) {
	super(protocolFactory, manager, transport);
}
 
Example #28
Source File: TThreadedSelectorServerWithFix.java    From phoenix-tephra with Apache License 2.0 4 votes vote down vote up
private void doAddAccept(SelectorThread thread, TNonblockingTransport client) {
  if (!thread.addAcceptedConnection(client)) {
    client.close();
  }
}
 
Example #29
Source File: FastMemoryOutputTransport.java    From disruptor_thrift_server with Apache License 2.0 3 votes vote down vote up
/**
 * Start writing accumulated content to the given transport
 * or continue from the previous stop point (this method is stateful).
 *
 * @param transport The transport to write contents into.
 *
 * @return The number of bytes actually written to the transport.
 *
 * @throws IOException on any I/O related error, such as socket disconnect.
 */
public int streamTo(TNonblockingTransport transport) throws IOException
{
    int bytesWritten = buf.writeTo(transport, streamPos, count - streamPos);
    streamPos += bytesWritten;
    return bytesWritten;
}
 
Example #30
Source File: TThreadedSelectorServerWithFix.java    From phoenix-tephra with Apache License 2.0 2 votes vote down vote up
/**
 * Set up the SelectorThread with a specified queue for connections.
 *
 * @param acceptedQueue
 *          The BlockingQueue implementation for holding incoming accepted
 *          connections.
 * @throws IOException
 *           if a selector cannot be created.
 */
public SelectorThread(BlockingQueue<TNonblockingTransport> acceptedQueue) throws IOException {
  this.acceptedQueue = acceptedQueue;
}