Example usage for io.netty.channel ChannelPromise await

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

Introduction

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

Prototype

boolean await(long timeoutMillis) throws InterruptedException;

Source Link

Document

Waits for this future to be completed within the specified time limit.

Usage

From source file:org.apache.activemq.artemis.core.remoting.impl.netty.NettyConnection.java

License:Apache License

public void write(ActiveMQBuffer buffer, final boolean flush, final boolean batched,
        final ChannelFutureListener futureListener) {

    try {/*  ww  w  .  j  a va2s .  c o  m*/
        writeLock.acquire();

        try {
            if (batchBuffer == null && batchingEnabled && batched && !flush) {
                // Lazily create batch buffer

                batchBuffer = ActiveMQBuffers.dynamicBuffer(BATCHING_BUFFER_SIZE);
            }

            if (batchBuffer != null) {
                batchBuffer.writeBytes(buffer, 0, buffer.writerIndex());

                if (batchBuffer.writerIndex() >= BATCHING_BUFFER_SIZE || !batched || flush) {
                    // If the batch buffer is full or it's flush param or not batched then flush the buffer

                    buffer = batchBuffer;
                } else {
                    return;
                }

                if (!batched || flush) {
                    batchBuffer = null;
                } else {
                    // Create a new buffer

                    batchBuffer = ActiveMQBuffers.dynamicBuffer(BATCHING_BUFFER_SIZE);
                }
            }

            // depending on if we need to flush or not we can use a voidPromise or
            // use a normal promise
            final ByteBuf buf = buffer.byteBuf();
            final ChannelPromise promise;
            if (flush || futureListener != null) {
                promise = channel.newPromise();
            } else {
                promise = channel.voidPromise();
            }

            EventLoop eventLoop = channel.eventLoop();
            boolean inEventLoop = eventLoop.inEventLoop();
            if (!inEventLoop) {
                if (futureListener != null) {
                    channel.writeAndFlush(buf, promise).addListener(futureListener);
                } else {
                    channel.writeAndFlush(buf, promise);
                }
            } else {
                // create a task which will be picked up by the eventloop and trigger the write.
                // This is mainly needed as this method is triggered by different threads for the same channel.
                // if we not do this we may produce out of order writes.
                final Runnable task = new Runnable() {
                    @Override
                    public void run() {
                        if (futureListener != null) {
                            channel.writeAndFlush(buf, promise).addListener(futureListener);
                        } else {
                            channel.writeAndFlush(buf, promise);
                        }
                    }
                };
                // execute the task on the eventloop
                eventLoop.execute(task);
            }

            // only try to wait if not in the eventloop otherwise we will produce a deadlock
            if (flush && !inEventLoop) {
                while (true) {
                    try {
                        boolean ok = promise.await(10000);

                        if (!ok) {
                            ActiveMQClientLogger.LOGGER.timeoutFlushingPacket();
                        }

                        break;
                    } catch (InterruptedException e) {
                        throw new ActiveMQInterruptedException(e);
                    }
                }
            }
        } finally {
            writeLock.release();
        }
    } catch (InterruptedException e) {
        throw new ActiveMQInterruptedException(e);
    }
}

From source file:org.hornetq.core.remoting.impl.netty.NettyConnection.java

License:Apache License

public void write(HornetQBuffer buffer, final boolean flush, final boolean batched) {

    try {/*w w  w . j  av a2 s  . co m*/
        writeLock.acquire();

        try {
            if (batchBuffer == null && batchingEnabled && batched && !flush) {
                // Lazily create batch buffer

                batchBuffer = HornetQBuffers.dynamicBuffer(BATCHING_BUFFER_SIZE);
            }

            if (batchBuffer != null) {
                batchBuffer.writeBytes(buffer, 0, buffer.writerIndex());

                if (batchBuffer.writerIndex() >= BATCHING_BUFFER_SIZE || !batched || flush) {
                    // If the batch buffer is full or it's flush param or not batched then flush the buffer

                    buffer = batchBuffer;
                } else {
                    return;
                }

                if (!batched || flush) {
                    batchBuffer = null;
                } else {
                    // Create a new buffer

                    batchBuffer = HornetQBuffers.dynamicBuffer(BATCHING_BUFFER_SIZE);
                }
            }

            // depending on if we need to flush or not we can use a voidPromise or
            // use a normal promise
            final ByteBuf buf = buffer.byteBuf();
            final ChannelPromise promise;
            if (flush) {
                promise = channel.newPromise();
            } else {
                promise = channel.voidPromise();
            }

            EventLoop eventLoop = channel.eventLoop();
            boolean inEventLoop = eventLoop.inEventLoop();
            if (!inEventLoop) {
                channel.writeAndFlush(buf, promise);
            } else {
                // create a task which will be picked up by the eventloop and trigger the write.
                // This is mainly needed as this method is triggered by different threads for the same channel.
                // if we not do this we may produce out of order writes.
                final Runnable task = new Runnable() {
                    @Override
                    public void run() {
                        channel.writeAndFlush(buf, promise);
                    }
                };
                // execute the task on the eventloop
                eventLoop.execute(task);
            }

            // only try to wait if not in the eventloop otherwise we will produce a deadlock
            if (flush && !inEventLoop) {
                while (true) {
                    try {
                        boolean ok = promise.await(10000);

                        if (!ok) {
                            HornetQClientLogger.LOGGER.timeoutFlushingPacket();
                        }

                        break;
                    } catch (InterruptedException e) {
                        throw new HornetQInterruptedException(e);
                    }
                }
            }
        } finally {
            writeLock.release();
        }
    } catch (InterruptedException e) {
        throw new HornetQInterruptedException(e);
    }
}