}
// 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();
}