Example usage for io.netty.channel DefaultChannelPromise DefaultChannelPromise

List of usage examples for io.netty.channel DefaultChannelPromise DefaultChannelPromise

Introduction

In this page you can find the example usage for io.netty.channel DefaultChannelPromise DefaultChannelPromise.

Prototype

public DefaultChannelPromise(Channel channel, EventExecutor executor) 

Source Link

Document

Creates a new instance.

Usage

From source file:com.lambdaworks.redis.protocol.CommandHandlerTest.java

License:Apache License

@Test
public void shouldCancelCommandOnQueueBatchFailure() throws Exception {

    Command<String, String, String> commandMock = mock(Command.class);

    RuntimeException exception = new RuntimeException();
    when(commandMock.getOutput()).thenThrow(exception);

    ChannelPromise channelPromise = new DefaultChannelPromise(null, ImmediateEventExecutor.INSTANCE);
    try {//from   ww w .java 2  s  . com
        sut.write(context, Arrays.asList(commandMock), channelPromise);
        fail("Missing RuntimeException");
    } catch (RuntimeException e) {
        assertThat(e).isSameAs(exception);
    }

    assertThat((Collection) ReflectionTestUtils.getField(sut, "queue")).isEmpty();
    verify(commandMock).completeExceptionally(exception);
}

From source file:com.linecorp.armeria.common.RequestContextTest.java

License:Apache License

@Test
@SuppressWarnings("deprecation")
public void makeContextAwareChannelFutureListener() {
    RequestContext context = createContext();
    ChannelPromise promise = new DefaultChannelPromise(channel, ImmediateEventExecutor.INSTANCE);
    promise.addListener(context.makeContextAware((ChannelFutureListener) f -> {
        assertEquals(context, RequestContext.current());
        assertTrue(entered.get());// w  w  w . j  ava 2s .  c om
    }));
    promise.setSuccess(null);
}

From source file:com.test.AbstractBootstrap.java

License:Apache License

final ChannelFuture initAndRegister() {
    final Channel channel = channelFactory().newChannel();
    try {/*from w ww  .  ja v  a2  s. c o m*/
        init(channel);
    } catch (Throwable t) {
        channel.unsafe().closeForcibly();
        // as the Channel is not registered yet we need to force the usage of the GlobalEventExecutor
        return new DefaultChannelPromise(channel, GlobalEventExecutor.INSTANCE).setFailure(t);
    }

    ChannelFuture regFuture = group().register(channel);
    if (regFuture.cause() != null) {
        if (channel.isRegistered()) {
            channel.close();
        } else {
            channel.unsafe().closeForcibly();
        }
    }

    // If we are here and the promise is not failed, it's one of the following cases:
    // 1) If we attempted registration from the event loop, the registration has been completed at this point.
    //    i.e. It's safe to attempt bind() or connect() now because the channel has been registered.
    // 2) If we attempted registration from the other thread, the registration request has been successfully
    //    added to the event loop's task queue for later execution.
    //    i.e. It's safe to attempt bind() or connect() now:
    //         because bind() or connect() will be executed *after* the scheduled registration task is executed
    //         because register(), bind(), and connect() are all bound to the same thread.

    return regFuture;
}

From source file:com.yea.remote.netty.balancing.RemoteClient.java

License:Apache License

@SuppressWarnings({ "rawtypes", "unchecked" })
public <T> NettyChannelPromise<T> send(CallAct act, List<GenericFutureListener> listeners,
        RemoteConstants.MessageType messageType, byte[] sessionID, Object[] messages) throws Exception {
    if (!this.channel.isWritable()) {
        throw new WriteRejectException();
    }/*from   www  .j a  v a2s .  c  om*/
    AwaitPromise observer = new AwaitPromise(new DefaultChannelPromise(channel, GlobalEventExecutor.INSTANCE));
    if (listeners != null && listeners.size() > 0) {
        for (GenericFutureListener listener : listeners) {
            observer.addListener(listener);
        }
    }
    Observable handle = null;
    Iterator<Map.Entry<String, ChannelHandler>> itHandle = channel.pipeline().iterator();
    while (itHandle.hasNext()) {
        Map.Entry<String, ChannelHandler> entry = itHandle.next();
        if (entry.getValue() instanceof Observable) {
            handle = (Observable) entry.getValue();
            handle.addObserver(sessionID, observer);
        }
    }

    Message nettyMessage = new Message();
    Header header = new Header();
    header.setType(messageType.value());
    header.setSessionID(sessionID);
    if (act != null) {
        header.addAttachment(NettyConstants.MessageHeaderAttachment.CALL_ACT.value(), act.getActName());
        if (!StringUtils.isEmpty(act.getCallbackName())) {
            header.addAttachment(NettyConstants.MessageHeaderAttachment.CALLBACK_ACT.value(),
                    act.getCallbackName());
        }
        if (act instanceof CallReflect) {
            header.addAttachment(NettyConstants.MessageHeaderAttachment.CALL_REFLECT.value(), act);
        }
    } else {
        if (messageType.value() == RemoteConstants.MessageType.SERVICE_REQ.value()) {
            throw new Exception("?????Act??");
        }
    }
    header.addAttachment(NettyConstants.MessageHeaderAttachment.HEADER_DATE.value(), new Date());
    nettyMessage.setHeader(header);
    nettyMessage.setBody(messages);

    try {
        getNettySend().send(nettyMessage, observer);
    } catch (Exception ex) {
        Thread.sleep(200);
        throw ex;
    }

    return observer;
}

From source file:com.yea.remote.netty.balancing.RemoteClient.java

License:Apache License

@Override
public void ping(int timeout) throws Throwable {
    if (this.isDown()) {
        throw new RemoteException("Node[" + this.channel + "]?");
    }//from w  w  w  .jav  a  2 s  . co  m
    if (!this.channel.isWritable()) {
        throw new WriteRejectException();
    }
    byte[] sessionID = UUIDGenerator.generate();
    AwaitPromise<Boolean> promise = new AwaitPromise<Boolean>(
            new DefaultChannelPromise(channel, GlobalEventExecutor.INSTANCE));
    Observable handle = null;
    Iterator<Map.Entry<String, ChannelHandler>> itHandle = channel.pipeline().iterator();
    while (itHandle.hasNext()) {
        Map.Entry<String, ChannelHandler> entry = itHandle.next();
        if (entry.getValue() instanceof Observable) {
            handle = (Observable) entry.getValue();
            handle.addObserver(sessionID, promise);
        }
    }
    Message nettyMessage = new Message();
    Header header = new Header();
    header.setType(RemoteConstants.MessageType.PING_REQ.value());
    header.setSessionID(sessionID);
    nettyMessage.setHeader(header);
    channel.writeAndFlush(nettyMessage, promise);
    promise.awaitObject(timeout);
}

From source file:cz.znj.kvr.sw.exp.java.netty.netty.MyEmbeddedEventLoop.java

License:Apache License

@Override
public ChannelFuture register(Channel channel) {
    return register(channel, new DefaultChannelPromise(channel, this));
}

From source file:io.lettuce.core.protocol.CommandHandlerTest.java

License:Apache License

@Test
public void shouldCancelCommandOnQueueSingleFailure() throws Exception {

    Command<String, String, String> commandMock = mock(Command.class);

    RuntimeException exception = new RuntimeException();
    when(commandMock.getOutput()).thenThrow(exception);

    ChannelPromise channelPromise = new DefaultChannelPromise(channel, ImmediateEventExecutor.INSTANCE);
    try {//from w ww  .j a va 2 s.co  m
        sut.write(context, commandMock, channelPromise);
        fail("Missing RuntimeException");
    } catch (RuntimeException e) {
        assertThat(e).isSameAs(exception);
    }

    assertThat(stack).isEmpty();
    verify(commandMock).completeExceptionally(exception);
}

From source file:io.lettuce.core.protocol.CommandHandlerTest.java

License:Apache License

@Test
public void shouldCancelCommandOnQueueBatchFailure() throws Exception {

    Command<String, String, String> commandMock = mock(Command.class);

    RuntimeException exception = new RuntimeException();
    when(commandMock.getOutput()).thenThrow(exception);

    ChannelPromise channelPromise = new DefaultChannelPromise(channel, ImmediateEventExecutor.INSTANCE);
    try {/*from w w  w.  j  a v  a 2s  .c om*/
        sut.write(context, Arrays.asList(commandMock), channelPromise);
        fail("Missing RuntimeException");
    } catch (RuntimeException e) {
        assertThat(e).isSameAs(exception);
    }

    assertThat(stack).isEmpty();
    verify(commandMock).completeExceptionally(exception);
}

From source file:io.lettuce.core.protocol.CommandHandlerTest.java

License:Apache License

@Test
public void shouldFailOnDuplicateCommands() throws Exception {

    Command<String, String, String> commandMock = mock(Command.class);

    ChannelPromise channelPromise = new DefaultChannelPromise(channel, ImmediateEventExecutor.INSTANCE);
    sut.write(context, Arrays.asList(commandMock, commandMock), channelPromise);

    assertThat(stack).isEmpty();/*from   ww  w  . j  a  va 2s  .  c om*/
    verify(commandMock).completeExceptionally(any(RedisException.class));
}

From source file:io.lettuce.core.protocol.CommandHandlerTest.java

License:Apache License

@Test
public void shouldRecordCorrectFirstResponseLatency() throws Exception {

    ChannelPromise channelPromise = new DefaultChannelPromise(channel, ImmediateEventExecutor.INSTANCE);
    channelPromise.setSuccess();/* w w  w . j  a v a2s .  co  m*/

    sut.channelRegistered(context);
    sut.channelActive(context);

    LatencyMeteredCommand<String, String, String> wrapped = new LatencyMeteredCommand<>(command);

    sut.write(context, wrapped, channelPromise);
    Thread.sleep(10);

    sut.channelRead(context, Unpooled.wrappedBuffer("*1\r\n+OK\r\n".getBytes()));

    verify(latencyCollector).recordCommandLatency(any(), any(), eq(CommandType.APPEND), gt(0L), gt(0L));

    sut.channelUnregistered(context);
}