Java Code Examples for io.netty.util.Timer#newTimeout()

The following examples show how to use io.netty.util.Timer#newTimeout() . 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: ServerTest.java    From simulacron with Apache License 2.0 6 votes vote down vote up
@Test
public void testTryWithResourcesShouldCloseAllClustersButNotTimerIfProvided() throws Exception {
  EventLoopGroup eventLoop;
  Timer timer = new HashedWheelTimer();

  try (Server server = Server.builder().withTimer(timer).build()) {
    // Do nothing here, since this is a unit test, we don't want to create any inet sockets
    // which is what Server does by default.
    eventLoop = server.eventLoopGroup;
  }

  // event loop should have been closed since a custom one was not provided.
  assertThat(eventLoop.isShutdown()).isTrue();
  // timer should not have been closed since a custom one was provided.
  timer.newTimeout(
      timeout -> {
        // noop
      },
      1,
      TimeUnit.SECONDS);
  timer.stop();
}
 
Example 2
Source File: ServerTest.java    From simulacron with Apache License 2.0 5 votes vote down vote up
@Test
public void testTryWithResourcesShouldCloseAllResources() throws Exception {
  EventLoopGroup eventLoop;
  Timer timer;

  try (Server server = Server.builder().build()) {
    // Do nothing here, since this is a unit test, we don't want to create any inet sockets
    // which is what Server does by default.
    eventLoop = server.eventLoopGroup;
    timer = server.timer;
  }

  // event loop should have been closed since a custom one was not provided.
  assertThat(eventLoop.isShutdown()).isTrue();
  // timer should have since a custom one was not provided.
  try {
    timer.newTimeout(
        timeout -> {
          // noop
        },
        1,
        TimeUnit.SECONDS);
    fail("Expected IllegalStateException");
  } catch (IllegalStateException ise) {
    // expected
  }
}
 
Example 3
Source File: CommunicationClient.java    From brpc-java with Apache License 2.0 4 votes vote down vote up
public void execute(Request request, Response response) throws RpcException {
    request.setCommunicationClient(this);
    Channel channel = selectChannel();
    request.setChannel(channel);
    ChannelInfo channelInfo = ChannelInfo.getClientChannelInfo(channel);
    RpcFuture rpcFuture = RpcFuture.createRpcFuture(request);
    if (request.getCallback() != null) {
        rpcFuture.setInterceptors(interceptors);
    }
    channelInfo.setCorrelationId(rpcFuture.getCorrelationId());
    rpcFuture.setChannelInfo(channelInfo);
    rpcFuture.setChannelType(communicationOptions.getChannelType());
    request.setRpcFuture(rpcFuture);
    request.setCorrelationId(rpcFuture.getCorrelationId());

    try {
        request.setSendBuf(communicationOptions.getProtocol().encodeRequest(request));
    } catch (Throwable t) {
        throw new RpcException(RpcException.SERIALIZATION_EXCEPTION, t.getMessage(), t);
    }

    // register timeout timer
    Timer timeoutTimer = TimerInstance.getInstance();
    RpcTimeoutTimer timeoutTask = new RpcTimeoutTimer(
            channelInfo, request.getCorrelationId(), communicationOptions.getProtocol());
    Timeout timeout = timeoutTimer.newTimeout(timeoutTask, request.getReadTimeoutMillis(), TimeUnit.MILLISECONDS);
    request.getRpcFuture().setTimeout(timeout);
    try {
        // netty will release the send buffer after sent.
        // we retain here, so it can be used when rpc retry.
        request.retain();
        ChannelFuture sendFuture = request.getChannel().writeAndFlush(request.getSendBuf());
        sendFuture.awaitUninterruptibly(request.getWriteTimeoutMillis());
        if (!sendFuture.isSuccess()) {
            if (!(sendFuture.cause() instanceof ClosedChannelException)) {
                log.warn("send request failed, channelActive={}, ex=",
                        request.getChannel().isActive(), sendFuture.cause());
            }
            String errMsg = String.format("send request failed, channelActive=%b",
                    request.getChannel().isActive());
            throw new RpcException(RpcException.NETWORK_EXCEPTION, errMsg);
        }
    } catch (Exception ex) {
        channelInfo.handleRequestFail(communicationOptions.getChannelType(), request.getCorrelationId());
        timeout.cancel();
        log.debug("send request failed:", ex);
        if (ex instanceof RpcException) {
            throw (RpcException) ex;
        } else {
            throw new RpcException(RpcException.NETWORK_EXCEPTION, "send request failed", ex);
        }
    }

    // return channel
    channelInfo.handleRequestSuccess(communicationOptions.getChannelType());

    // receive
    if (rpcFuture.isAsync()) {
        response.setRpcFuture(rpcFuture);
    } else {
        response.setResult(rpcFuture.get(request.getReadTimeoutMillis(), TimeUnit.MILLISECONDS));
        response.setCorrelationId(rpcFuture.getCorrelationId());
    }
}