Java Code Examples for io.netty.util.concurrent.EventExecutor#execute()

The following examples show how to use io.netty.util.concurrent.EventExecutor#execute() . 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: AbstractChannelHandlerContext.java    From netty-4.1.22 with Apache License 2.0 6 votes vote down vote up
static void invokeChannelReadComplete(final AbstractChannelHandlerContext next) {
    EventExecutor executor = next.executor();
    if (executor.inEventLoop()) {
        next.invokeChannelReadComplete();
    } else {
        Runnable task = next.invokeChannelReadCompleteTask;
        if (task == null) {
            next.invokeChannelReadCompleteTask = task = new Runnable() {
                @Override
                public void run() {
                    next.invokeChannelReadComplete();
                }
            };
        }
        executor.execute(task);
    }
}
 
Example 2
Source File: AbstractChannelHandlerContext.java    From netty4.0.27Learn with Apache License 2.0 6 votes vote down vote up
@Override
public ChannelHandlerContext read() {
    invokedPrevRead = true;
    final AbstractChannelHandlerContext next = findContextOutbound();
    EventExecutor executor = next.executor();
    if (executor.inEventLoop()) {
        next.invokeRead();
    } else {
        Runnable task = next.invokeReadTask;
        if (task == null) {
            next.invokeReadTask = task = new Runnable() {
                @Override
                public void run() {
                    next.invokeRead();
                }
            };
        }
        executor.execute(task);
    }
    return this;
}
 
Example 3
Source File: DefaultChannelPipeline.java    From netty-4.1.22 with Apache License 2.0 6 votes vote down vote up
private void destroyUp(AbstractChannelHandlerContext ctx, boolean inEventLoop) {
    final Thread currentThread = Thread.currentThread();
    final AbstractChannelHandlerContext tail = this.tail;
    for (;;) {
        if (ctx == tail) {
            destroyDown(currentThread, tail.prev, inEventLoop);
            break;
        }

        final EventExecutor executor = ctx.executor();
        if (!inEventLoop && !executor.inEventLoop(currentThread)) {
            final AbstractChannelHandlerContext finalCtx = ctx;
            executor.execute(new Runnable() {
                @Override
                public void run() {
                    destroyUp(finalCtx, true);
                }
            });
            break;
        }

        ctx = ctx.next;
        inEventLoop = false;
    }
}
 
Example 4
Source File: Lz4FrameEncoder.java    From netty-4.1.22 with Apache License 2.0 6 votes vote down vote up
/**
 * Close this {@link Lz4FrameEncoder} and so finish the encoding.
 * The given {@link ChannelFuture} will be notified once the operation
 * completes and will also be returned.关闭这个Lz4FrameEncoder,然后完成编码。一旦操作完成,给定的ChannelFuture将被通知,并且也将被返回。
 */
public ChannelFuture close(final ChannelPromise promise) {
    ChannelHandlerContext ctx = ctx();
    EventExecutor executor = ctx.executor();
    if (executor.inEventLoop()) {
        return finishEncode(ctx, promise);
    } else {
        executor.execute(new Runnable() {
            @Override
            public void run() {
                ChannelFuture f = finishEncode(ctx(), promise);
                f.addListener(new ChannelPromiseNotifier(promise));
            }
        });
        return promise;
    }
}
 
Example 5
Source File: DefaultChannelPipeline.java    From netty4.0.27Learn with Apache License 2.0 6 votes vote down vote up
private void destroyUp(AbstractChannelHandlerContext ctx) {
    final Thread currentThread = Thread.currentThread();
    final AbstractChannelHandlerContext tail = this.tail;
    for (;;) {
        if (ctx == tail) {
            destroyDown(currentThread, tail.prev);
            break;
        }

        final EventExecutor executor = ctx.executor();
        if (!executor.inEventLoop(currentThread)) {
            final AbstractChannelHandlerContext finalCtx = ctx;
            executor.execute(new OneTimeTask() {
                @Override
                public void run() {
                    destroyUp(finalCtx);
                }
            });
            break;
        }

        ctx = ctx.next;
    }
}
 
Example 6
Source File: JdkZlibEncoder.java    From netty4.0.27Learn with Apache License 2.0 6 votes vote down vote up
@Override
public ChannelFuture close(final ChannelPromise promise) {
    ChannelHandlerContext ctx = ctx();
    EventExecutor executor = ctx.executor();
    if (executor.inEventLoop()) {
        return finishEncode(ctx, promise);
    } else {
        final ChannelPromise p = ctx.newPromise();
        executor.execute(new Runnable() {
            @Override
            public void run() {
                ChannelFuture f = finishEncode(ctx(), p);
                f.addListener(new ChannelPromiseNotifier(promise));
            }
        });
        return p;
    }
}
 
Example 7
Source File: JZlibEncoder.java    From netty-4.1.22 with Apache License 2.0 6 votes vote down vote up
@Override
public ChannelFuture close(final ChannelPromise promise) {
    ChannelHandlerContext ctx = ctx();
    EventExecutor executor = ctx.executor();
    if (executor.inEventLoop()) {
        return finishEncode(ctx, promise);
    } else {
        final ChannelPromise p = ctx.newPromise();
        executor.execute(new Runnable() {
            @Override
            public void run() {
                ChannelFuture f = finishEncode(ctx(), p);
                f.addListener(new ChannelPromiseNotifier(promise));
            }
        });
        return p;
    }
}
 
Example 8
Source File: AbstractChannelHandlerContext.java    From netty4.0.27Learn with Apache License 2.0 6 votes vote down vote up
@Override
public ChannelHandlerContext fireChannelWritabilityChanged() {
    final AbstractChannelHandlerContext next = findContextInbound();
    EventExecutor executor = next.executor();
    if (executor.inEventLoop()) {
        next.invokeChannelWritabilityChanged();
    } else {
        Runnable task = next.invokeChannelWritableStateChangedTask;
        if (task == null) {
            next.invokeChannelWritableStateChangedTask = task = new Runnable() {
                @Override
                public void run() {
                    next.invokeChannelWritabilityChanged();
                }
            };
        }
        executor.execute(task);
    }
    return this;
}
 
Example 9
Source File: AbstractChannelHandlerContext.java    From netty4.0.27Learn with Apache License 2.0 6 votes vote down vote up
@Override
public ChannelHandlerContext fireUserEventTriggered(final Object event) {
    if (event == null) {
        throw new NullPointerException("event");
    }

    final AbstractChannelHandlerContext next = findContextInbound();
    EventExecutor executor = next.executor();
    if (executor.inEventLoop()) {
        next.invokeUserEventTriggered(event);
    } else {
        executor.execute(new OneTimeTask() {
            @Override
            public void run() {
                next.invokeUserEventTriggered(event);
            }
        });
    }
    return this;
}
 
Example 10
Source File: AbstractChannelHandlerContext.java    From netty-4.1.22 with Apache License 2.0 6 votes vote down vote up
static void invokeExceptionCaught(final AbstractChannelHandlerContext next, final Throwable cause) {
    ObjectUtil.checkNotNull(cause, "cause");
    EventExecutor executor = next.executor();
    if (executor.inEventLoop()) {
        next.invokeExceptionCaught(cause);
    } else {
        try {
            executor.execute(new Runnable() {
                @Override
                public void run() {
                    next.invokeExceptionCaught(cause);
                }
            });
        } catch (Throwable t) {
            if (logger.isWarnEnabled()) {
                logger.warn("Failed to submit an exceptionCaught() event.", t);
                logger.warn("The exceptionCaught() event that was failed to submit was:", cause);
            }
        }
    }
}
 
Example 11
Source File: AbstractChannelHandlerContext.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
static void invokeChannelActive(final AbstractChannelHandlerContext next) {
    EventExecutor executor = next.executor();
    if (executor.inEventLoop()) {
        next.invokeChannelActive();
    } else {
        executor.execute(new Runnable() {
            @Override
            public void run() {
                next.invokeChannelActive();
            }
        });
    }
}
 
Example 12
Source File: AbstractChannelHandlerContext.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
private static void safeExecute(EventExecutor executor, Runnable runnable, ChannelPromise promise, Object msg) {
    try {
        executor.execute(runnable);
    } catch (Throwable cause) {
        try {
            promise.setFailure(cause);
        } finally {
            if (msg != null) {
                ReferenceCountUtil.release(msg);
            }
        }
    }
}
 
Example 13
Source File: AbstractChannelHandlerContext.java    From netty4.0.27Learn with Apache License 2.0 5 votes vote down vote up
private static void safeExecute(EventExecutor executor, Runnable runnable, ChannelPromise promise, Object msg) {
    try {
        executor.execute(runnable);
    } catch (Throwable cause) {
        try {
            promise.setFailure(cause);
        } finally {
            if (msg != null) {
                ReferenceCountUtil.release(msg);
            }
        }
    }
}
 
Example 14
Source File: DefaultChannelPipeline.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
@Override
    public final ChannelPipeline addLast(EventExecutorGroup group, String name, ChannelHandler handler) {
        final AbstractChannelHandlerContext newCtx;
        synchronized (this) {
//            检查handler是否加了@Sharable注释,是否是共享的
            checkMultiplicity(handler);

            newCtx = newContext(group, filterName(name, handler), handler);

//            把handler添加到handler上下文的最后一个位置
            addLast0(newCtx);

            // If the registered is false it means that the channel was not registered on an eventloop yet.
            // In this case we add the context to the pipeline and add a task that will call
            // ChannelHandler.handlerAdded(...) once the channel is registered.//如果注册的是false,这意味着通道还没有在eventloop上注册。
//在本例中,我们将上下文添加到管道中,并添加一个将调用的任务
// ChannelHandler.handlerAdded(…)一旦注册了通道。
            if (!registered) {
                newCtx.setAddPending();
                callHandlerCallbackLater(newCtx, true);
                return this;
            }

            EventExecutor executor = newCtx.executor();
            if (!executor.inEventLoop()) {
                newCtx.setAddPending();
                executor.execute(new Runnable() {
                    @Override
                    public void run() {
                        callHandlerAdded0(newCtx);
                    }
                });
                return this;
            }
        }
        callHandlerAdded0(newCtx);
        return this;
    }
 
Example 15
Source File: AbstractChannelHandlerContext.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
static void invokeChannelUnregistered(final AbstractChannelHandlerContext next) {
    EventExecutor executor = next.executor();
    if (executor.inEventLoop()) {
        next.invokeChannelUnregistered();
    } else {
        executor.execute(new Runnable() {
            @Override
            public void run() {
                next.invokeChannelUnregistered();
            }
        });
    }
}
 
Example 16
Source File: SingleThreadEventLoopTest.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
private static void assertRejection(EventExecutor loop) {
    try {
        loop.execute(NOOP);
        fail("A task must be rejected after shutdown() is called.");
    } catch (RejectedExecutionException e) {
        // Expected
    }
}
 
Example 17
Source File: CombinedChannelDuplexHandler.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
final void remove() {
    EventExecutor executor = executor();
    if (executor.inEventLoop()) {
        remove0();
    } else {
        executor.execute(new Runnable() {
            @Override
            public void run() {
                remove0();
            }
        });
    }
}
 
Example 18
Source File: AbstractChannelHandlerContext.java    From netty4.0.27Learn with Apache License 2.0 5 votes vote down vote up
@Override
public ChannelHandlerContext fireExceptionCaught(final Throwable cause) {
    if (cause == null) {
        throw new NullPointerException("cause");
    }

    final AbstractChannelHandlerContext next = this.next;

    EventExecutor executor = next.executor();
    if (executor.inEventLoop()) {
        next.invokeExceptionCaught(cause);
    } else {
        try {
            executor.execute(new OneTimeTask() {
                @Override
                public void run() {
                    next.invokeExceptionCaught(cause);
                }
            });
        } catch (Throwable t) {
            if (logger.isWarnEnabled()) {
                logger.warn("Failed to submit an exceptionCaught() event.", t);
                logger.warn("The exceptionCaught() event that was failed to submit was:", cause);
            }
        }
    }

    return this;
}
 
Example 19
Source File: ColocatedEventLoopGroup.java    From reactor-netty with Apache License 2.0 4 votes vote down vote up
void clean() {
	for (EventExecutor ex : eventLoopGroup) {
		ex.execute(() -> localLoop.set(null));
	}
}
 
Example 20
Source File: Session.java    From lannister with Apache License 2.0 4 votes vote down vote up
public void dispose(boolean sendWill) {
	setConnected(false);

	if (sendWill && will != null) { // [MQTT-3.1.2-12]
		Topic topic = Topic.NEXUS.prepare(will);
		topic.publish(will);

		will(null); // [MQTT-3.1.2-10]
	}

	ChannelId channelId = null;
	ChannelHandlerContext ctx = NEXUS.channelHandlerContext(clientId);
	if (ctx != null) {
		ctx.channel().disconnect().addListener(ChannelFutureListener.CLOSE);
		channelId = ctx.channel().id();
	}

	logger.debug("Session disposed [clientId={}, channelId={}]", clientId, ctx == null ? "null" : channelId);

	EventExecutor executor = ctx != null ? ctx.channel().eventLoop() : GlobalEventExecutor.INSTANCE;
	executor.execute(
			() -> Plugins.INSTANCE.get(DisconnectEventListener.class).disconnected(new DisconnectEventArgs() {
				@Override
				public String clientId() {
					return clientId;
				}

				@Override
				public Boolean cleanSession() {
					return cleanSession;
				}

				@Override
				public Boolean byDisconnectMessage() {
					return !sendWill;
				}
			}));

	// TODO WHY => Current thread is not owner of the lock! -> <not-locked>
	// disposeLock.lock();
	// try {
	if (cleanSession) {
		TopicSubscriber.NEXUS.removeByClientId(clientId);
		TopicSubscription.NEXUS.removeByClientId(clientId);
		OutboundMessageStatus.NEXUS.removeByClientId(clientId);
	}

	NEXUS.remove(this);
	// }
	// finally {
	// disposeLock.unlock();
	// }

	ClusterDataDisposer.INSTANCE.disposeLock(disposeLock);

}