io.netty.util.concurrent.DefaultPromise Java Examples

The following examples show how to use io.netty.util.concurrent.DefaultPromise. 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: Http2MultiplexedChannelPoolTest.java    From aws-sdk-java-v2 with Apache License 2.0 6 votes vote down vote up
@Test
public void acquire_shouldAcquireAgainIfExistingNotReusable() throws Exception {
    Channel channel = new EmbeddedChannel();

    try {
        ChannelPool connectionPool = Mockito.mock(ChannelPool.class);

        loopGroup.register(channel).awaitUninterruptibly();
        Promise<Channel> channelPromise = new DefaultPromise<>(loopGroup.next());
        channelPromise.setSuccess(channel);

        Mockito.when(connectionPool.acquire()).thenReturn(channelPromise);

        Http2MultiplexedChannelPool h2Pool = new Http2MultiplexedChannelPool(connectionPool, loopGroup,
                                                                             Collections.emptySet(), null);

        h2Pool.acquire().awaitUninterruptibly();
        h2Pool.acquire().awaitUninterruptibly();

        Mockito.verify(connectionPool, Mockito.times(2)).acquire();
    } finally {
        channel.close();
    }
}
 
Example #2
Source File: UnpooledNode.java    From xio with Apache License 2.0 6 votes vote down vote up
public Future<Void> send(Object message) {
  DefaultPromise<Void> promise = new DefaultPromise<>(eventLoopGroup().next());

  log.debug("Acquiring Node: " + this);
  if (channelResult == null) {
    channelResult = bootstrap.clone().connect();
  }

  if (channelResult.isSuccess()) {
    writeAndFlush(message, promise);
  } else {
    channelResult.addListener(
        (ChannelFutureListener)
            channelFuture -> {
              if (channelFuture.isSuccess()) {
                log.debug("connection achieved " + message);
                writeAndFlush(message, promise);
              } else {
                log.error("connection error: ", channelFuture.cause());
                promise.setFailure(channelFuture.cause());
              }
            });
  }

  return promise;
}
 
Example #3
Source File: UnpooledNode.java    From xio with Apache License 2.0 6 votes vote down vote up
private void writeAndFlush(Object message, DefaultPromise<Void> promise) {
  Channel channel = channelResult.channel();
  channel
      .writeAndFlush(message)
      .addListener(
          (ChannelFutureListener)
              channelFuture -> {
                if (channelFuture.isSuccess()) {
                  log.debug("write finished for " + message);
                  promise.setSuccess(null);
                } else {
                  log.error("Write error: ", channelFuture.cause());
                  promise.setFailure(channelFuture.cause());
                }
              });
}
 
Example #4
Source File: XioConnectionPool.java    From xio with Apache License 2.0 6 votes vote down vote up
private void acquireWithRetry(AsyncRetryLoop retry, DefaultPromise<Channel> result) {
  Future<Channel> poolResult = simpleChannelPool.acquire();
  poolResult.addListener(
      new FutureListener<Channel>() {
        public void operationComplete(Future<Channel> f) {
          if (f.isSuccess()) {
            result.setSuccess(f.getNow());
          } else {
            // deal with connection failure here.
            if (retry.canRetry()) {
              retry.attempt(() -> acquireWithRetry(retry, result));
            } else {
              result.setFailure(f.cause());
            }
          }
        }
      });
}
 
Example #5
Source File: SmtpSessionTest.java    From NioSmtpClient with Apache License 2.0 6 votes vote down vote up
@Test
public void itReturnsTheStartTlsResponseIfTheTlsHandshakeSucceeds() throws Exception {
  CompletableFuture<SmtpClientResponse> f = session.startTls();
  responseFuture.complete(Lists.newArrayList(OK_RESPONSE));

  // respond to the ehlo sent after starttls
  secondResponseFuture.complete(Lists.newArrayList(new DefaultSmtpResponse(250,
      "smtp.example.com Hello client.example.com",
      "AUTH PLAIN LOGIN",
      "PIPELINING")));

  // the handshake succeeds
  SslHandler sslHandler = getSslHandler();
  ((DefaultPromise<Channel>) sslHandler.handshakeFuture()).setSuccess(channel);

  assertThat(f.isDone()).isTrue();
  assertThat(f.get().getResponses().get(0).code()).isEqualTo(OK_RESPONSE.code());

  // check EHLO is parsed again
  assertThat(session.getEhloResponse().isSupported(Extension.PIPELINING)).isTrue();
  assertThat(session.getEhloResponse().isSupported(Extension.STARTTLS)).isFalse();
}
 
Example #6
Source File: ThreadPerChannelEventLoopGroupTest.java    From netty4.0.27Learn with Apache License 2.0 6 votes vote down vote up
@Test
public void testTerminationFutureSuccessReflectively() throws Exception {
    Field terminationFutureField =
            ThreadPerChannelEventLoopGroup.class.getDeclaredField("terminationFuture");
    terminationFutureField.setAccessible(true);
    final Exception[] exceptionHolder = new Exception[1];
    for (int i = 0; i < 2; i++) {
        ThreadPerChannelEventLoopGroup loopGroup = new ThreadPerChannelEventLoopGroup(64);
        Promise<?> promise = new DefaultPromise<Void>(GlobalEventExecutor.INSTANCE) {
            @Override
            public Promise<Void> setSuccess(Void result) {
                try {
                    return super.setSuccess(result);
                } catch (IllegalStateException e) {
                    exceptionHolder[0] = e;
                    throw e;
                }
            }
        };
        terminationFutureField.set(loopGroup, promise);
        runTest(loopGroup);
    }
    // The global event executor will not terminate, but this will give the test a chance to fail.
    GlobalEventExecutor.INSTANCE.awaitTermination(100, TimeUnit.MILLISECONDS);
    assertNull(exceptionHolder[0]);
}
 
Example #7
Source File: FiniteStateMachineTest.java    From bgpcep with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Establish PCEPS TLS connection with peer.
 */
@Test
public void testEstablishTLS() {
    final DefaultPCEPSessionNegotiator negotiator =
        new DefaultPCEPSessionNegotiator(new DefaultPromise<>(GlobalEventExecutor.INSTANCE),
            this.channel, this.listener, (short) 1, 20, new OpenBuilder().setKeepalive(Uint8.ONE).build(),
            SslContextFactoryTest.createTlsConfig());
    negotiator.channelActive(null);
    assertEquals(1, this.msgsSend.size());
    assertTrue(this.msgsSend.get(0) instanceof Starttls);
    assertEquals(DefaultPCEPSessionNegotiator.State.START_TLS_WAIT, negotiator.getState());
    negotiator.handleMessage(this.startTlsMsg);
    assertEquals(DefaultPCEPSessionNegotiator.State.OPEN_WAIT, negotiator.getState());
    assertEquals(2, this.msgsSend.size());
    assertTrue(this.msgsSend.get(1) instanceof Open);
    negotiator.handleMessage(this.openMsg);
    assertEquals(DefaultPCEPSessionNegotiator.State.KEEP_WAIT, negotiator.getState());
}
 
Example #8
Source File: ThreadPerChannelEventLoopGroupTest.java    From netty-4.1.22 with Apache License 2.0 6 votes vote down vote up
@Test
public void testTerminationFutureSuccessReflectively() throws Exception {
    Field terminationFutureField =
            ThreadPerChannelEventLoopGroup.class.getDeclaredField("terminationFuture");
    terminationFutureField.setAccessible(true);
    final Exception[] exceptionHolder = new Exception[1];
    for (int i = 0; i < 2; i++) {
        ThreadPerChannelEventLoopGroup loopGroup = new ThreadPerChannelEventLoopGroup(64);
        Promise<?> promise = new DefaultPromise<Void>(GlobalEventExecutor.INSTANCE) {
            @Override
            public Promise<Void> setSuccess(Void result) {
                try {
                    return super.setSuccess(result);
                } catch (IllegalStateException e) {
                    exceptionHolder[0] = e;
                    throw e;
                }
            }
        };
        terminationFutureField.set(loopGroup, promise);
        runTest(loopGroup);
    }
    // The global event executor will not terminate, but this will give the test a chance to fail.
    GlobalEventExecutor.INSTANCE.awaitTermination(100, TimeUnit.MILLISECONDS);
    assertNull(exceptionHolder[0]);
}
 
Example #9
Source File: Http2FrameCodecTest.java    From netty-4.1.22 with Apache License 2.0 6 votes vote down vote up
@Test(timeout = 5000)
public void newOutboundStream() {
    final Http2FrameStream stream = frameCodec.newStream();

    assertNotNull(stream);
    assertFalse(isStreamIdValid(stream.id()));

    final Promise<Void> listenerExecuted = new DefaultPromise<Void>(GlobalEventExecutor.INSTANCE);

    channel.writeAndFlush(new DefaultHttp2HeadersFrame(new DefaultHttp2Headers(), false).stream(stream))
           .addListener(new ChannelFutureListener() {
                @Override
                public void operationComplete(ChannelFuture future) throws Exception {
                    assertTrue(future.isSuccess());
                    assertTrue(isStreamIdValid(stream.id()));
                    listenerExecuted.setSuccess(null);
                }
            }
    );
    ByteBuf data = Unpooled.buffer().writeZero(100);
    ChannelFuture f = channel.writeAndFlush(new DefaultHttp2DataFrame(data).stream(stream));
    assertTrue(f.isSuccess());

    listenerExecuted.syncUninterruptibly();
    assertTrue(listenerExecuted.isSuccess());
}
 
Example #10
Source File: MqttClientImpl.java    From smartacus-mqtt-broker with Apache License 2.0 6 votes vote down vote up
/**
 * Publish a message to the given payload, using the given qos and optional retain
 *
 * @param topic   The topic to publish to
 * @param payload The payload to send
 * @param qos     The qos to use while publishing
 * @param retain  true if you want to retain the message on the server, false otherwise
 * @return A future which will be completed when the message is delivered to the server
 */
@Override
public Future<Void> publish(String topic, ByteBuf payload, MqttQoS qos, boolean retain) {
    Promise<Void> future = new DefaultPromise<>(this.eventLoop.next());
    MqttFixedHeader fixedHeader = new MqttFixedHeader(MqttMessageType.PUBLISH, false, qos, retain, 0);
    MqttPublishVariableHeader variableHeader = new MqttPublishVariableHeader(topic, getNewMessageId().messageId());
    MqttPublishMessage message = new MqttPublishMessage(fixedHeader, variableHeader, payload);
    MqttPendingPublish pendingPublish = new MqttPendingPublish(variableHeader.packetId(), future, payload.retain(), message, qos);
    ChannelFuture channelFuture = this.sendAndFlushPacket(message);

    if (channelFuture != null) {
        pendingPublish.setSent(true);
        if (channelFuture.cause() != null) {
            future.setFailure(channelFuture.cause());
            return future;
        }
    }
    if (pendingPublish.isSent() && pendingPublish.getQos() == MqttQoS.AT_MOST_ONCE) {
        pendingPublish.getFuture().setSuccess(null); //We don't get an ACK for QOS 0
    } else if (pendingPublish.isSent()) {
        this.pendingPublishes.put(pendingPublish.getMessageId(), pendingPublish);
        pendingPublish.startPublishRetransmissionTimer(this.eventLoop.next(), this::sendAndFlushPacket);
    }
    return future;
}
 
Example #11
Source File: MultiplexedChannelRecordTest.java    From ambry with Apache License 2.0 6 votes vote down vote up
/**
 * IOException is expected if acquire stream from closed channel.
 */
@Test
public void acquireClaimedConnectionOnClosedChannelShouldThrowIOException() {
  loopGroup.register(channel).awaitUninterruptibly();
  Promise<Channel> channelPromise = new DefaultPromise<>(loopGroup.next());

  MultiplexedChannelRecord record = new MultiplexedChannelRecord(channel, 1, 10000L, streamChannelInitializer);

  record.closeChildChannels();

  record.acquireClaimedStream(channelPromise);

  try {
    channelPromise.get();
  } catch (InterruptedException | ExecutionException e) {
    assertTrue(e.getCause() instanceof IOException);
  }
}
 
Example #12
Source File: MySQLClient.java    From shardingsphere with Apache License 2.0 6 votes vote down vote up
/**
 * Connect to MySQL.
 */
public synchronized void connect() {
    responseCallback = new DefaultPromise<>(eventLoopGroup.next());
    channel = new Bootstrap()
            .group(eventLoopGroup)
            .channel(NioSocketChannel.class)
            .handler(new ChannelInitializer<SocketChannel>() {
                @Override
                protected void initChannel(final SocketChannel socketChannel) {
                    socketChannel.pipeline().addLast(new PacketCodec(new MySQLPacketCodecEngine()));
                    socketChannel.pipeline().addLast(new MySQLCommandPacketDecoder());
                    socketChannel.pipeline().addLast(new MySQLNegotiateHandler(username, password, responseCallback));
                    socketChannel.pipeline().addLast(new MySQLCommandResponseHandler());
                }
            })
            .option(ChannelOption.AUTO_READ, true)
            .connect(host, port).channel();
    serverInfo = waitExpectedResponse(ServerInfo.class);
}
 
Example #13
Source File: PCEPDispatcherImpl.java    From bgpcep with Eclipse Public License 1.0 5 votes vote down vote up
synchronized ServerBootstrap createServerBootstrap(final ChannelPipelineInitializer initializer) {
    final ServerBootstrap b = new ServerBootstrap();
    b.childHandler(new ChannelInitializer<SocketChannel>() {
        @Override
        protected void initChannel(final SocketChannel ch) {
            initializer.initializeChannel(ch, new DefaultPromise<>(PCEPDispatcherImpl.this.executor));
        }
    });
    b.option(ChannelOption.SO_BACKLOG, SOCKET_BACKLOG_SIZE);

    b.childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT);

    if (Epoll.isAvailable()) {
        b.channel(EpollServerSocketChannel.class);
        b.childOption(EpollChannelOption.EPOLL_MODE, EpollMode.LEVEL_TRIGGERED);
    } else {
        b.channel(NioServerSocketChannel.class);
    }
    if (!this.keys.isEmpty()) {
        if (Epoll.isAvailable()) {
            b.option(EpollChannelOption.TCP_MD5SIG, this.keys);
        } else {
            throw new UnsupportedOperationException(Epoll.unavailabilityCause().getCause());
        }
    }

    // Make sure we are doing round-robin processing
    b.childOption(ChannelOption.RCVBUF_ALLOCATOR, new FixedRecvByteBufAllocator(1));

    if (b.config().group() == null) {
        b.group(this.bossGroup, this.workerGroup);
    }

    return b;
}
 
Example #14
Source File: HandlerSubscriberWhiteboxVerificationTest.java    From netty-reactive-streams with Apache License 2.0 5 votes vote down vote up
@Override
public Subscriber<Long> createSubscriber(WhiteboxSubscriberProbe<Long> probe) {


    final ClosedLoopChannel channel = new ClosedLoopChannel();
    channel.config().setAutoRead(false);
    ChannelFuture registered = eventLoop.register(channel);

    final HandlerSubscriber<Long> subscriber = new HandlerSubscriber<>(registered.channel().eventLoop(), 2, 4);
    final ProbeHandler<Long> probeHandler = new ProbeHandler<>(probe, Long.class);
    final Promise<Void> handlersInPlace = new DefaultPromise<>(eventLoop.next());

    registered.addListener(new ChannelFutureListener() {
        @Override
        public void operationComplete(ChannelFuture future) throws Exception {
            channel.pipeline().addLast("probe", probeHandler);
            channel.pipeline().addLast("subscriber", subscriber);
            handlersInPlace.setSuccess(null);
            // Channel needs to be active before the subscriber starts responding to demand
            channel.pipeline().fireChannelActive();
        }
    });

    if (workAroundIssue277) {
        try {
            // Wait for the pipeline to be setup, so we're ready to receive elements even if they aren't requested,
            // because https://github.com/reactive-streams/reactive-streams-jvm/issues/277
            handlersInPlace.await();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }

    return probeHandler.wrap(subscriber);
}
 
Example #15
Source File: TestEventLoopGroupProvider.java    From spinach with Apache License 2.0 5 votes vote down vote up
@Override
public Promise<Boolean> release(EventExecutorGroup eventLoopGroup, long quietPeriod, long timeout, TimeUnit unit) {
    DefaultPromise<Boolean> result = new DefaultPromise<Boolean>(ImmediateEventExecutor.INSTANCE);
    result.setSuccess(true);

    return result;
}
 
Example #16
Source File: MySQLClient.java    From shardingsphere with Apache License 2.0 5 votes vote down vote up
private void registerSlave() {
    responseCallback = new DefaultPromise<>(eventLoopGroup.next());
    InetSocketAddress localAddress = (InetSocketAddress) channel.localAddress();
    MySQLComRegisterSlaveCommandPacket registerSlaveCommandPacket = new MySQLComRegisterSlaveCommandPacket(serverId, localAddress.getHostName(), username, password, localAddress.getPort());
    channel.writeAndFlush(registerSlaveCommandPacket);
    waitExpectedResponse(MySQLOKPacket.class);
}
 
Example #17
Source File: MySQLClient.java    From shardingsphere with Apache License 2.0 5 votes vote down vote up
/**
 * Execute query.
 *
 * @param queryString query string
 * @return result set
 */
public synchronized InternalResultSet executeQuery(final String queryString) {
    responseCallback = new DefaultPromise<>(eventLoopGroup.next());
    MySQLComQueryPacket comQueryPacket = new MySQLComQueryPacket(queryString);
    channel.writeAndFlush(comQueryPacket);
    return waitExpectedResponse(InternalResultSet.class);
}
 
Example #18
Source File: Node.java    From xio with Apache License 2.0 5 votes vote down vote up
public Future<Void> send(Object message) {
  DefaultPromise<Void> promise = new DefaultPromise<>(eventLoopGroup.next());

  log.debug("Acquiring Node: " + this);
  Future<Channel> channelResult = connectionPool.acquire();
  channelResult.addListener(
      new FutureListener<Channel>() {
        public void operationComplete(Future<Channel> future) {
          if (future.isSuccess()) {
            Channel channel = future.getNow();
            channel
                .writeAndFlush(message)
                .addListener(
                    new ChannelFutureListener() {
                      public void operationComplete(ChannelFuture channelFuture) {
                        if (channelFuture.isSuccess()) {
                          log.debug("write finished for " + message);
                          promise.setSuccess(null);
                        } else {
                          log.error("Write error: ", channelFuture.cause());
                          promise.setFailure(channelFuture.cause());
                        }
                      }
                    });
          } else {
            log.error("Could not connect to client for write: " + future.cause());
            promise.setFailure(future.cause());
          }
        }
      });

  return promise;
}
 
Example #19
Source File: MySQLClient.java    From shardingsphere with Apache License 2.0 5 votes vote down vote up
/**
 * Execute command.
 *
 * @param queryString query string
 * @return true if execute successfully, otherwise false
 */
public synchronized boolean execute(final String queryString) {
    responseCallback = new DefaultPromise<>(eventLoopGroup.next());
    MySQLComQueryPacket comQueryPacket = new MySQLComQueryPacket(queryString);
    channel.writeAndFlush(comQueryPacket);
    return null != waitExpectedResponse(MySQLOKPacket.class);
}
 
Example #20
Source File: FiniteStateMachineTest.java    From bgpcep with Eclipse Public License 1.0 5 votes vote down vote up
@Before
public void setup() {
    final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.open.object.Open
        localPrefs = new OpenBuilder().setKeepalive(Uint8.ONE).build();
    this.serverSession = new DefaultPCEPSessionNegotiator(new DefaultPromise<>(GlobalEventExecutor.INSTANCE),
            this.channel, this.listener, (short) 1, 20, localPrefs);
    this.tlsSessionNegotiator = new DefaultPCEPSessionNegotiator(new DefaultPromise<>(GlobalEventExecutor.INSTANCE),
            this.channel, this.listener, (short) 1, 20, localPrefs, new TlsBuilder().build());
}
 
Example #21
Source File: BGPDispatcherImpl.java    From bgpcep with Eclipse Public License 1.0 5 votes vote down vote up
static <S extends BGPSession> ChannelHandler createServerChannelHandler(
        final ChannelPipelineInitializer<S> initializer) {
    return new ChannelInitializer<SocketChannel>() {
        @Override
        protected void initChannel(final SocketChannel channel) {
            initializer.initializeChannel(channel, new DefaultPromise<>(GlobalEventExecutor.INSTANCE));
        }
    };
}
 
Example #22
Source File: FSMTest.java    From bgpcep with Eclipse Public License 1.0 5 votes vote down vote up
@Test
public void testDenyPeer() {
    this.clientSession = new BGPClientSessionNegotiator(new DefaultPromise<>(GlobalEventExecutor.INSTANCE),
            this.speakerListener, new StrictBGPPeerRegistry());
    this.clientSession.channelActive(null);
    assertEquals(1, this.receivedMsgs.size());
    assertTrue(this.receivedMsgs.get(0) instanceof Notify);
}
 
Example #23
Source File: MultiplexedChannelRecordTest.java    From ambry with Apache License 2.0 5 votes vote down vote up
/**
 * False from acquireStream() is expected if no stream available.
 */
@Test
public void availableStream0ShouldBeFalse() {
  loopGroup.register(channel).awaitUninterruptibly();
  Promise<Channel> channelPromise = new DefaultPromise<>(loopGroup.next());
  channelPromise.setSuccess(channel);

  MultiplexedChannelRecord record = new MultiplexedChannelRecord(channel, 0, null, streamChannelInitializer);

  assertFalse(record.acquireStream(null));
}
 
Example #24
Source File: Http2MultiplexedChannelPoolTest.java    From ambry with Apache License 2.0 5 votes vote down vote up
/**
 * Channel acquire and release test.
 */
@Test
public void streamChannelAcquireReleaseTest() throws Exception {
  Channel channel = newHttp2Channel();

  try {
    ChannelPool connectionPool = Mockito.mock(ChannelPool.class);

    loopGroup.register(channel).awaitUninterruptibly();
    Promise<Channel> channelPromise = new DefaultPromise<>(loopGroup.next());
    channelPromise.setSuccess(channel);

    Mockito.when(connectionPool.acquire()).thenReturn(channelPromise);

    Http2MultiplexedChannelPool h2Pool =
        new Http2MultiplexedChannelPool(connectionPool, loopGroup, new HashSet<>(), null,
            http2ClientConfigForOneConnection, new Http2ClientMetrics(new MetricRegistry()),
            streamChannelInitializer);

    Channel streamChannel1 = h2Pool.acquire().awaitUninterruptibly().getNow();
    assertTrue(streamChannel1 instanceof Http2StreamChannel);
    Mockito.verify(connectionPool, Mockito.times(1)).acquire();

    Channel streamChannel2 = h2Pool.acquire().awaitUninterruptibly().getNow();
    assertTrue(streamChannel2 instanceof Http2StreamChannel);
    Mockito.verify(connectionPool, Mockito.times(1)).acquire();

    // Verify number of numOfAvailableStreams
    MultiplexedChannelRecord multiplexedChannelRecord =
        streamChannel2.parent().attr(Http2MultiplexedChannelPool.MULTIPLEXED_CHANNEL).get();
    assertEquals(maxConcurrentStreamsPerConnection - 2, multiplexedChannelRecord.getNumOfAvailableStreams().get());
    h2Pool.release(streamChannel1).getNow();
    h2Pool.release(streamChannel2).getNow();
    assertEquals(maxConcurrentStreamsPerConnection, multiplexedChannelRecord.getNumOfAvailableStreams().get());
  } finally {
    channel.close();
  }
}
 
Example #25
Source File: Http2MultiplexedChannelPoolTest.java    From ambry with Apache License 2.0 5 votes vote down vote up
@Test(timeout = 5_000)
public void interruptDuringClosePreservesFlag() throws InterruptedException {
  SocketChannel channel = new NioSocketChannel();
  try {
    loopGroup.register(channel).awaitUninterruptibly();
    Promise<Channel> channelPromise = new DefaultPromise<>(loopGroup.next());
    channelPromise.setSuccess(channel);

    ChannelPool connectionPool = mock(ChannelPool.class);
    Promise<Void> releasePromise = Mockito.spy(new DefaultPromise<>(loopGroup.next()));

    when(connectionPool.release(eq(channel))).thenReturn(releasePromise);

    MultiplexedChannelRecord record = new MultiplexedChannelRecord(channel, 8, null, streamChannelInitializer);
    Http2MultiplexedChannelPool h2Pool =
        new Http2MultiplexedChannelPool(connectionPool, loopGroup, Collections.singleton(record), null,
            http2ClientConfigForOneConnection, new Http2ClientMetrics(new MetricRegistry()),
            streamChannelInitializer);

    CompletableFuture<Boolean> interrupteFlagPreserved = new CompletableFuture<>();

    Thread t = new Thread(() -> {
      try {
        h2Pool.close();
      } catch (Exception e) {
        if (e.getCause() instanceof InterruptedException && Thread.currentThread().isInterrupted()) {
          interrupteFlagPreserved.complete(true);
        }
      }
    });

    t.start();
    t.interrupt();
    t.join();
    assertTrue(interrupteFlagPreserved.join());
  } finally {
    channel.close().awaitUninterruptibly();
  }
}
 
Example #26
Source File: NettyClient2.java    From wind-im with Apache License 2.0 5 votes vote down vote up
public Future<IRedisCommandResponse> sendRedisCommand(final RedisCommand redisCommand) {
	final Future<IRedisCommandResponse> responseFuture;
	// logger.info("send push message {} {} {}", channelPromise,
	// channelPromise.isSuccess(),
	// channelPromise.channel().isActive());
	if (channelPromise != null) {
		final ChannelPromise readyPromise = this.channelPromise;

		final DefaultPromise<IRedisCommandResponse> responsePromise = new DefaultPromise<IRedisCommandResponse>(
				readyPromise.channel().eventLoop());
		// 提交一个事件
		readyPromise.channel().eventLoop().submit(new Runnable() {
			@Override
			public void run() {
				// 将这个结果赋值给responsePromise
				NettyClient2.this.responsePromise = responsePromise;
			}
		});

		readyPromise.channel().writeAndFlush(redisCommand).addListener(new GenericFutureListener<ChannelFuture>() {
			@Override
			public void operationComplete(final ChannelFuture future) throws Exception {
				if (!future.isSuccess()) {
					// 如果失败了,直接将promise返回
					responsePromise.tryFailure(future.cause());
					logger.error("send push message error: {},cause={}", redisCommand, future.cause());
				} else {
					// logger.info("write data to platform success");
				}
			}
		});
		responseFuture = responsePromise;
	} else {
		logger.error("send push error because client is not connected: {}", redisCommand.toString());
		responseFuture = new FailedFuture<IRedisCommandResponse>(GlobalEventExecutor.INSTANCE, CONNECT_EXCEPTION);
	}
	return responseFuture;
}
 
Example #27
Source File: PlatformSSLClient.java    From wind-im with Apache License 2.0 5 votes vote down vote up
public Future<IRedisCommandResponse> sendRedisCommand(final RedisCommand redisCommand) {
	final Future<IRedisCommandResponse> responseFuture;
	if (channelPromise != null) {
		final ChannelPromise readyPromise = this.channelPromise;

		final DefaultPromise<IRedisCommandResponse> responsePromise = new DefaultPromise<IRedisCommandResponse>(
				readyPromise.channel().eventLoop());
		// 提交一个事件
		readyPromise.channel().eventLoop().submit(new Runnable() {
			@Override
			public void run() {
				// 将这个结果赋值给responsePromise
				PlatformSSLClient.this.responsePromise = responsePromise;
			}
		});

		readyPromise.channel().writeAndFlush(redisCommand).addListener(new GenericFutureListener<ChannelFuture>() {
			@Override
			public void operationComplete(final ChannelFuture future) throws Exception {
				if (!future.isSuccess()) {
					// 如果失败了,直接将promise返回
					responsePromise.tryFailure(future.cause());
					logger.error("send push message error: {},cause={}", redisCommand, future.cause());
				} else {
					// logger.info("write data to platform success");
				}
			}
		});
		responseFuture = responsePromise;
	} else {
		logger.error("send push error because client is not connected: {}", redisCommand.toString());
		responseFuture = new FailedFuture<IRedisCommandResponse>(GlobalEventExecutor.INSTANCE, CONNECT_EXCEPTION);
	}
	return responseFuture;
}
 
Example #28
Source File: MqttClientImpl.java    From smartacus-mqtt-broker with Apache License 2.0 5 votes vote down vote up
/**
 * Remove the subscription for the given topic and handler
 * If you want to unsubscribe from all handlers known for this topic, use {@link #off(String)}
 *
 * @param topic   The topic to unsubscribe for
 * @param handler The handler to unsubscribe
 * @return A future which will be completed when the server acknowledges our unsubscribe request
 */
@Override
public Future<Void> off(String topic, MqttHandler handler) {
    Promise<Void> future = new DefaultPromise<>(this.eventLoop.next());
    for (MqttSubscription subscription : this.handlerToSubscribtion.get(handler)) {
        this.subscriptions.remove(topic, subscription);
    }
    this.handlerToSubscribtion.removeAll(handler);
    this.checkSubscribtions(topic, future);
    return future;
}
 
Example #29
Source File: MqttClientImpl.java    From smartacus-mqtt-broker with Apache License 2.0 5 votes vote down vote up
/**
 * Remove all subscriptions for the given topic.
 * If you want to specify which handler to unsubscribe, use {@link #off(String, MqttHandler)}
 *
 * @param topic The topic to unsubscribe for
 * @return A future which will be completed when the server acknowledges our unsubscribe request
 */
@Override
public Future<Void> off(String topic) {
    Promise<Void> future = new DefaultPromise<>(this.eventLoop.next());
    ImmutableSet<MqttSubscription> subscriptions = ImmutableSet.copyOf(this.subscriptions.get(topic));
    for (MqttSubscription subscription : subscriptions) {
        for (MqttSubscription handSub : this.handlerToSubscribtion.get(subscription.getHandler())) {
            this.subscriptions.remove(topic, handSub);
        }
        this.handlerToSubscribtion.remove(subscription.getHandler(), subscription);
    }
    this.checkSubscribtions(topic, future);
    return future;
}
 
Example #30
Source File: NettyClient2.java    From openzaly with Apache License 2.0 5 votes vote down vote up
public Future<IRedisCommandResponse> sendRedisCommand(final RedisCommand redisCommand) {
	final Future<IRedisCommandResponse> responseFuture;
	// logger.info("send push message {} {} {}", channelPromise,
	// channelPromise.isSuccess(),
	// channelPromise.channel().isActive());
	if (channelPromise != null) {
		final ChannelPromise readyPromise = this.channelPromise;

		final DefaultPromise<IRedisCommandResponse> responsePromise = new DefaultPromise<IRedisCommandResponse>(
				readyPromise.channel().eventLoop());
		// 提交一个事件
		readyPromise.channel().eventLoop().submit(new Runnable() {
			@Override
			public void run() {
				// 将这个结果赋值给responsePromise
				NettyClient2.this.responsePromise = responsePromise;
			}
		});

		readyPromise.channel().writeAndFlush(redisCommand).addListener(new GenericFutureListener<ChannelFuture>() {
			@Override
			public void operationComplete(final ChannelFuture future) throws Exception {
				if (!future.isSuccess()) {
					// 如果失败了,直接将promise返回
					responsePromise.tryFailure(future.cause());
					logger.error("send push message error: {},cause={}", redisCommand, future.cause());
				} else {
					// logger.info("write data to platform success");
				}
			}
		});
		responseFuture = responsePromise;
	} else {
		logger.error("send push error because client is not connected: {}", redisCommand.toString());
		responseFuture = new FailedFuture<IRedisCommandResponse>(GlobalEventExecutor.INSTANCE, CONNECT_EXCEPTION);
	}
	return responseFuture;
}