Package org.apache.thrift.transport

Examples of org.apache.thrift.transport.TNonblockingSocket


        ThriftSessionManager.instance.setCurrentSocket(socket.getSocketChannel().socket().getRemoteSocketAddress());
    }

    public void beforeClose(Message buffer)
    {
        TNonblockingSocket socket = (TNonblockingSocket) buffer.transport;
        ThriftSessionManager.instance.connectionComplete(socket.getSocketChannel().socket().getRemoteSocketAddress());
    }
View Full Code Here


    // set up async client manager
    TAsyncClientManager acm = new TAsyncClientManager();

    // connect an async client
    TNonblockingSocket clientSock = new TNonblockingSocket(
      ServerTestBase.HOST, ServerTestBase.PORT);
    Srv.AsyncClient client = new Srv.AsyncClient(new TBinaryProtocol.Factory(), acm, clientSock);

    // make a standard method call
    standardCallTest(client);

    // make a standard method call that succeeds within timeout
    assertFalse(s.isStopped());
    client.setTimeout(5000);
    standardCallTest(client);

    // make a void method call
    assertFalse(s.isStopped());
    final CountDownLatch voidLatch = new CountDownLatch(1);
    final AtomicBoolean voidMethodReturned = new AtomicBoolean(false);
    client.voidMethod(new FailureLessCallback<Srv.AsyncClient.voidMethod_call>() {
      @Override
      public void onComplete(voidMethod_call response) {
        try {
          response.getResult();
          voidMethodReturned.set(true);
        } catch (TException e) {
          fail(e);
        } finally {
          voidLatch.countDown();
        }
      }
    });
    voidLatch.await(1, TimeUnit.SECONDS);
    assertTrue(voidMethodReturned.get());

    // make a oneway method call
    assertFalse(s.isStopped());
    final CountDownLatch onewayLatch = new CountDownLatch(1);
    final AtomicBoolean onewayReturned = new AtomicBoolean(false);
    client.onewayMethod(new FailureLessCallback<onewayMethod_call>() {
      @Override
      public void onComplete(onewayMethod_call response) {
        try {
          response.getResult();
          onewayReturned.set(true);
        } catch (TException e) {
          fail(e);
        } finally {
          onewayLatch.countDown();
        }
      }
    });
    onewayLatch.await(1, TimeUnit.SECONDS);
    assertTrue(onewayReturned.get());

    // make another standard method call
    assertFalse(s.isStopped());
    final CountDownLatch voidAfterOnewayLatch = new CountDownLatch(1);
    final AtomicBoolean voidAfterOnewayReturned = new AtomicBoolean(false);
    client.voidMethod(new FailureLessCallback<voidMethod_call>() {
      @Override
      public void onComplete(voidMethod_call response) {
        try {
          response.getResult();
          voidAfterOnewayReturned.set(true);
        } catch (TException e) {
          fail(e);
        } finally {
          voidAfterOnewayLatch.countDown();
        }
      }
    });
    voidAfterOnewayLatch.await(1, TimeUnit.SECONDS);
    assertTrue(voidAfterOnewayReturned.get());

    // make multiple calls with deserialization in the selector thread (repro Eric's issue)
    assertFalse(s.isStopped());
    int numThreads = 50;
    int numCallsPerThread = 100;
    List<JankyRunnable> runnables = new ArrayList<JankyRunnable>();
    List<Thread> threads = new ArrayList<Thread>();
    for (int i = 0; i < numThreads; i++) {
      JankyRunnable runnable = new JankyRunnable(acm, numCallsPerThread);
      Thread thread = new Thread(runnable);
      thread.start();
      threads.add(thread);
      runnables.add(runnable);
    }
    for (Thread thread : threads) {
      thread.join();
    }
    int numSuccesses = 0;
    for (JankyRunnable runnable : runnables) {
      numSuccesses += runnable.getNumSuccesses();
    }
    assertEquals(numThreads * numCallsPerThread, numSuccesses);

    // check that timeouts work
    assertFalse(s.isStopped());
    assertTrue(clientSock.isOpen());
    final CountDownLatch timeoutLatch = new CountDownLatch(1);
    client.setTimeout(100);
    client.primitiveMethod(new AsyncMethodCallback<primitiveMethod_call>() {

      @Override
      public void onError(Throwable throwable) {
        try {
          if (!(throwable instanceof TimeoutException)) {
            StringWriter sink = new StringWriter();
            throwable.printStackTrace(new PrintWriter(sink, true));
            fail("expected TimeoutException but got " + sink.toString());
          }
        } finally {
          timeoutLatch.countDown();
        }
      }

      @Override
      public void onComplete(primitiveMethod_call response) {
        try {
          fail("should not have finished timed out call.");
        } finally {
          timeoutLatch.countDown();
        }
      }

    });
    timeoutLatch.await(2, TimeUnit.SECONDS);
    assertTrue(client.hasError());
    assertTrue(client.getError() instanceof TimeoutException);

    // error closes socket and make sure isOpen reflects that
    assertFalse(clientSock.isOpen());
  }
View Full Code Here

    private TNonblockingSocket clientSocket_;

    public JankyRunnable(TAsyncClientManager acm, int numCalls) throws Exception {
      this.acm_ = acm;
      this.numCalls_ = numCalls;
      this.clientSocket_ = new TNonblockingSocket(ServerTestBase.HOST, ServerTestBase.PORT);
      this.client_ = new Srv.AsyncClient(new TBinaryProtocol.Factory(), acm_, clientSocket_);
      this.client_.setTimeout(20000);
    }
View Full Code Here

            SocketChannel socketChannel = serverSocketChannel.accept();
            if (socketChannel == null) {
                return null;
            }

            TNonblockingSocket tsocket = new TNonblockingSocket(socketChannel);
            tsocket.setTimeout(0); // disabling client timeout
            tsocket.getSocketChannel().socket().setKeepAlive(true);
            tsocket.getSocketChannel().socket().setSendBufferSize(config.getSocketSendBufferBytes());
            tsocket.getSocketChannel().socket().setReceiveBufferSize(config.getSocketRecvBufferBytes());
            return tsocket;
        } catch (IOException iox) {
            throw new TTransportException(iox);
        }
    }
View Full Code Here

    }

    @Override
    protected void beforeInvoke(Message buffer)
    {
        TNonblockingSocket socket = (TNonblockingSocket) buffer.transport;
        ThriftSessionManager.instance.setCurrentSocket(socket.getSocketChannel().socket().getRemoteSocketAddress());
    }
View Full Code Here

TOP

Related Classes of org.apache.thrift.transport.TNonblockingSocket

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.