Example usage for io.netty.channel ChannelPromise setFailure

List of usage examples for io.netty.channel ChannelPromise setFailure

Introduction

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

Prototype

@Override
    ChannelPromise setFailure(Throwable cause);

Source Link

Usage

From source file:org.opendaylight.tcpmd5.netty.MD5NioSocketChannel.java

License:Open Source License

@Override
public ChannelFuture shutdownOutput(final ChannelPromise future) {
    EventLoop loop = eventLoop();/*from   w  ww .  j  a v a2 s. co  m*/
    if (loop.inEventLoop()) {
        try {
            javaChannel().socket().shutdownOutput();
            future.setSuccess();
        } catch (Exception e) {
            future.setFailure(e);
        }
    } else {
        loop.execute(new Runnable() {
            @Override
            public void run() {
                shutdownOutput(future);
            }
        });
    }
    return future;
}

From source file:org.restcomm.media.network.netty.channel.AsyncNettyNetworkChannelTest.java

License:Open Source License

@SuppressWarnings("unchecked")
@Test//from  ww  w.j a  v a2 s  .c  o  m
public void testBindFailure() {
    // given
    final SocketAddress localAddress = new InetSocketAddress("127.0.0.1", 2427);

    final ChannelFuture channelBindFuture = mock(ChannelFuture.class);
    final ChannelFuture channelCloseFuture = mock(ChannelFuture.class);
    final Channel channel = mock(Channel.class);
    final ChannelHandler channelHandler = mock(ChannelHandler.class);

    this.eventGroup = new NioEventLoopGroup();
    final Bootstrap bootstrap = new Bootstrap().group(eventGroup).handler(channelHandler)
            .channel(NioDatagramChannel.class);
    final NettyNetworkManager networkManager = new NettyNetworkManager(bootstrap);
    final NettyNetworkManager networkManagerSpy = spy(networkManager);
    final AsyncNettyNetworkChannel<Object> networkChannel = new AsyncNettyNetworkChannel<>(networkManagerSpy);

    final FutureCallback<Void> openCallback = mock(FutureCallback.class);
    final FutureCallback<Void> bindCallback = mock(FutureCallback.class);
    final Exception exception = new RuntimeException("Testing purposes!");

    when(channel.bind(localAddress)).thenReturn(channelBindFuture);
    doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            final FutureCallback<Channel> callback = invocation.getArgumentAt(0, FutureCallback.class);
            callback.onSuccess(channel);
            return null;
        }

    }).when(networkManagerSpy).openChannel(any(FutureCallback.class));
    doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            final ChannelFutureListener listener = invocation.getArgumentAt(0, ChannelFutureListener.class);
            final ChannelPromise promise = new DefaultChannelProgressivePromise(channel,
                    mock(EventExecutor.class));
            promise.setFailure(exception);
            listener.operationComplete(promise);
            return null;
        }

    }).when(channelBindFuture).addListener(any(ChannelFutureListener.class));
    when(channel.close()).thenReturn(channelCloseFuture);

    // when - open
    networkChannel.open(openCallback);
    networkChannel.bind(localAddress, bindCallback);

    // then
    verify(bindCallback, timeout(100)).onFailure(exception);
    assertFalse(networkChannel.isOpen());
    assertFalse(networkChannel.isBound());
    assertFalse(networkChannel.isConnected());
}

From source file:org.restcomm.media.network.netty.NettyNetworkManagerTest.java

License:Open Source License

@SuppressWarnings("unchecked")
@Test/*  w  w w.  j av  a2s.  com*/
public void testOpenChannelAsyncFailure() throws Exception {
    // given
    this.eventLoopGroup = new NioEventLoopGroup(1);
    final Bootstrap bootstrap = PowerMockito.mock(Bootstrap.class);
    PowerMockito.when(bootstrap.group()).thenReturn(eventLoopGroup);
    this.eventExecutor = new DefaultEventExecutor();
    final FutureCallback<Channel> callback = mock(FutureCallback.class);
    final Channel channel = mock(Channel.class);
    final ChannelPromise promise = new DefaultChannelProgressivePromise(channel, eventExecutor);
    final Exception exception = new RuntimeException("Testing purposes!");

    try (final NettyNetworkManager networkManager = new NettyNetworkManager(bootstrap)) {
        // when
        when(bootstrap.clone()).thenReturn(bootstrap);
        when(bootstrap.register()).thenReturn(promise);
        promise.setFailure(exception);

        networkManager.openChannel(callback);

        // then
        verify(callback, timeout(100)).onFailure(exception);
    }
}

From source file:org.rzo.netty.ahessian.rpc.message.HessianRPCCallEncoder.java

License:Apache License

@Override
public void produceOutput(ChannelHandlerContext ctx, Object e, ChannelPromise promise) throws Exception {
    if (!ctx.channel().isActive())
        throw new RuntimeException("channel not active");
    try {/*ww w . jav a  2 s.  co  m*/
        /*
         * Object msg = e; if (msg instanceof FlushRequestMessage) {
         * ChannelPromise future = ctx.newPromise(); hOut.flush(future);
         * //future.sync(); return; } no longer required
         */
        HessianRPCCallMessage message = (HessianRPCCallMessage) e;
        message.setHasSessionFilter(_hasSessionFilter);
        hOut.resetReferences();
        hOut.call(message);
        if (_inverseServer)
            hOut.flush(promise);
    } catch (Exception ex) {
        Constants.ahessianLogger.warn("", ex);
        promise.setFailure(ex);
    }

}

From source file:org.rzo.netty.ahessian.rpc.message.HessianRPCReplyEncoder.java

License:Apache License

@Override
synchronized public void produceOutput(ChannelHandlerContext ctx, Object msg, ChannelPromise promise)
        throws Exception {
    if (ctx == null || ctx.channel() == null || !ctx.channel().isActive())
        throw new RuntimeException("channel closed");
    /*// w  w w.  j a  v a2  s . co m
     * if (msg instanceof FlushRequestMessage) { hOut.flush(promise);
     * promise.await(5000); return; }
     */

    try {
        // if (e.getMessage() instanceof Integer)
        // {
        // hOut.flush();
        // return;
        // }
        HessianRPCReplyMessage message = (HessianRPCReplyMessage) msg;
        // Constants.ahessianLogger.warn("encode reply for #"+message.getHeaders().get(Constants.CALL_ID_STRING));

        hOut.resetReferences();
        hOut.writeReply(message);
        // hOut.flush();
        // e.getFuture().setSuccess();
    } catch (Exception ex) {
        Constants.ahessianLogger.warn("", ex);
        promise.setFailure(ex);
    }
}

From source file:reactor.ipc.netty.channel.ChannelOperationsHandler.java

License:Open Source License

@Override
@SuppressWarnings("unchecked")
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
    if (log.isDebugEnabled()) {
        log.debug("Writing object {}", msg);
    }/*w  w w  .j  a v  a  2 s.c  o  m*/

    if (pendingWrites == null) {
        this.pendingWrites = QueueSupplier.unbounded().get();
        this.pendingWriteOffer = (BiPredicate<ChannelPromise, Object>) pendingWrites;
    }

    if (!pendingWriteOffer.test(promise, msg)) {
        promise.setFailure(new IllegalStateException("Send Queue full?!"));
    }
}

From source file:reactor.ipc.netty.channel.ChannelOperationsHandler.java

License:Open Source License

@SuppressWarnings("unchecked")
void drain() {//from w ww.  jav  a2  s.c  o m
    if (WIP.getAndIncrement(this) == 0) {

        for (;;) {
            if (removed) {
                discard();
                return;
            }

            if (pendingWrites == null || innerActive || !ctx.channel().isWritable()) {
                if (WIP.decrementAndGet(this) == 0) {
                    break;
                }
                continue;
            }

            ChannelPromise promise;
            Object v = pendingWrites.poll();

            try {
                promise = (ChannelPromise) v;
            } catch (Throwable e) {
                ctx.fireExceptionCaught(e);
                return;
            }

            boolean empty = promise == null;

            if (empty) {
                if (WIP.decrementAndGet(this) == 0) {
                    break;
                }
                continue;
            }

            v = pendingWrites.poll();

            if (v instanceof Publisher) {
                Publisher<?> p = (Publisher<?>) v;

                if (p instanceof Callable) {
                    @SuppressWarnings("unchecked")
                    Callable<?> supplier = (Callable<?>) p;

                    Object vr;

                    try {
                        vr = supplier.call();
                    } catch (Throwable e) {
                        promise.setFailure(e);
                        continue;
                    }

                    if (vr == null) {
                        promise.setSuccess();
                        continue;
                    }

                    if (inner.unbounded) {
                        doWrite(vr, promise, null);
                    } else {
                        innerActive = true;
                        inner.promise = promise;
                        inner.onSubscribe(Operators.scalarSubscription(inner, vr));
                    }
                } else {
                    innerActive = true;
                    inner.promise = promise;
                    p.subscribe(inner);
                }
            } else {
                doWrite(v, promise, null);
            }
        }
    }
}