Package io.netty.channel
Class ManualIoEventLoop
- java.lang.Object
-
- java.util.concurrent.AbstractExecutorService
-
- io.netty.util.concurrent.AbstractEventExecutor
-
- io.netty.util.concurrent.AbstractScheduledEventExecutor
-
- io.netty.channel.ManualIoEventLoop
-
- All Implemented Interfaces:
EventLoop,EventLoopGroup,IoEventLoop,IoEventLoopGroup,EventExecutor,EventExecutorGroup,OrderedEventExecutor,ThreadAwareExecutor,java.lang.Iterable<EventExecutor>,java.util.concurrent.Executor,java.util.concurrent.ExecutorService,java.util.concurrent.ScheduledExecutorService
public final class ManualIoEventLoop extends AbstractScheduledEventExecutor implements IoEventLoop
IoEventLoopimplementation that is owned by the user and so needs to be driven by the user manually with the givenThread. This means that the user is responsible to call eitherrunNow()orrun(long)to execute IO and tasks that were submitted to thisIoEventLoop.This is for advanced use-cases only, where the user wants to own the
Threadthat drives theIoEventLoopto also do other work. Care must be taken that theor {@link #waitAndRun()}methods are called in a timely fashion.
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class io.netty.util.concurrent.AbstractEventExecutor
AbstractEventExecutor.LazyRunnable
-
-
Constructor Summary
Constructors Constructor Description ManualIoEventLoop(IoEventLoopGroup parent, java.lang.Thread owningThread, IoHandlerFactory factory)Create a newIoEventLoopthat is owned by the user and so needs to be driven by the user with the givenThread.ManualIoEventLoop(IoEventLoopGroup parent, java.lang.Thread owningThread, IoHandlerFactory factory, Ticker ticker)Create a newIoEventLoopthat is owned by the user and so needs to be driven by the user with the givenThread.ManualIoEventLoop(java.lang.Thread owningThread, IoHandlerFactory factory)Create a newIoEventLoopthat is owned by the user and so needs to be driven by the user with the givenThread.
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description booleanawaitTermination(long timeout, java.util.concurrent.TimeUnit unit)voidexecute(java.lang.Runnable command)booleaninEventLoop(java.lang.Thread thread)Returntrueif the givenThreadis executed in the event loop,falseotherwise.<T> java.util.List<java.util.concurrent.Future<T>>invokeAll(java.util.Collection<? extends java.util.concurrent.Callable<T>> tasks)<T> java.util.List<java.util.concurrent.Future<T>>invokeAll(java.util.Collection<? extends java.util.concurrent.Callable<T>> tasks, long timeout, java.util.concurrent.TimeUnit unit)<T> TinvokeAny(java.util.Collection<? extends java.util.concurrent.Callable<T>> tasks)<T> TinvokeAny(java.util.Collection<? extends java.util.concurrent.Callable<T>> tasks, long timeout, java.util.concurrent.TimeUnit unit)booleanisCompatible(java.lang.Class<? extends IoHandle> handleType)Returnstrueif the given type is compatible with thisIoEventLoopGroupand so can be registered to the containedIoEventLoops,falseotherwise.booleanisIoType(java.lang.Class<? extends IoHandler> handlerType)booleanisShutdown()booleanisShuttingDown()Returnstrueif and only if allEventExecutors managed by thisEventExecutorGroupare being shut down gracefully or was shut down.booleanisTerminated()ManualIoEventLoopnext()Returns one of theEventExecutors managed by thisEventExecutorGroup.IoEventLoopGroupparent()Return theEventExecutorGroupwhich is the parent of thisEventExecutor,ChannelFutureregister(Channel channel)Deprecated.ChannelFutureregister(ChannelPromise promise)Deprecated.ChannelFutureregister(Channel channel, ChannelPromise promise)Deprecated.Future<IoRegistration>register(IoHandle handle)intrun(long waitNanos)Run all ready IO and tasks for thisIoEventLoop.intrun(long waitNanos, long runAllTasksTimeoutNanos)Run all ready IO and tasks for thisIoEventLoop.intrunNonBlockingTasks(long timeoutNanos)Poll and run tasks from the task queue, until the task queue is empty or the given deadline is exceeded.
IftimeoutNanosis less or equals 0, no deadline is applied.intrunNow()Run all ready IO and tasks for thisIoEventLoop.intrunNow(long runAllTasksTimeoutNanos)Executes all ready IO and tasks for thisIoEventLoop.voidsetOwningThread(java.lang.Thread owningThread)Set the owning thread that will callrun(io.netty.channel.IoHandlerContext, long).voidshutdown()Deprecated.Future<?>shutdownGracefully(long quietPeriod, long timeout, java.util.concurrent.TimeUnit unit)Signals this executor that the caller wants the executor to be shut down.Future<?>terminationFuture()Returns theFuturewhich is notified when allEventExecutors managed by thisEventExecutorGrouphave been terminated.Tickerticker()The ticker for this executor.voidwakeup()Force a wakeup and so therun(long)method will unblock and return even if there was nothing to do.-
Methods inherited from class io.netty.util.concurrent.AbstractScheduledEventExecutor
afterScheduledTaskSubmitted, beforeScheduledTaskSubmitted, cancelScheduledTasks, deadlineToDelayNanos, delayNanos, fetchFromScheduledTaskQueue, getCurrentTimeNanos, hasScheduledTasks, initialNanoTime, nanoTime, nextScheduledTaskDeadlineNanos, nextScheduledTaskNano, pollScheduledTask, pollScheduledTask, schedule, schedule, scheduleAtFixedRate, scheduleWithFixedDelay, validateScheduled
-
Methods inherited from class io.netty.util.concurrent.AbstractEventExecutor
iterator, lazyExecute, newTaskFor, newTaskFor, runTask, safeExecute, shutdownGracefully, shutdownNow, submit, submit, submit
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface io.netty.util.concurrent.EventExecutor
inEventLoop, isExecutorThread, isSuspended, newFailedFuture, newProgressivePromise, newPromise, newSucceededFuture, trySuspend
-
Methods inherited from interface io.netty.util.concurrent.EventExecutorGroup
iterator, schedule, schedule, scheduleAtFixedRate, scheduleWithFixedDelay, shutdownGracefully, shutdownNow, submit, submit, submit
-
-
-
-
Constructor Detail
-
ManualIoEventLoop
public ManualIoEventLoop(java.lang.Thread owningThread, IoHandlerFactory factory)Create a newIoEventLoopthat is owned by the user and so needs to be driven by the user with the givenThread. This means that the user is responsible to call eitherrunNow()orrun(long)to execute IO or tasks that were submitted to thisIoEventLoop.- Parameters:
owningThread- theThreadthat executes the IO and tasks for thisIoEventLoop. The user will use thisThreadto callrunNow()orrun(long)to make progress.factory- theIoHandlerFactorythat will be used to create theIoHandlerthat is used by thisIoEventLoop.
-
ManualIoEventLoop
public ManualIoEventLoop(IoEventLoopGroup parent, java.lang.Thread owningThread, IoHandlerFactory factory)
Create a newIoEventLoopthat is owned by the user and so needs to be driven by the user with the givenThread. This means that the user is responsible to call eitherrunNow()orrun(long)to execute IO or tasks that were submitted to thisIoEventLoop.- Parameters:
parent- the parentIoEventLoopGroupornullif no parent.owningThread- theThreadthat executes the IO and tasks for thisIoEventLoop. The user will use thisThreadto callrunNow()orrun(long)to make progress. Ifnull, must be set later usingsetOwningThread(Thread).factory- theIoHandlerFactorythat will be used to create theIoHandlerthat is used by thisIoEventLoop.
-
ManualIoEventLoop
public ManualIoEventLoop(IoEventLoopGroup parent, java.lang.Thread owningThread, IoHandlerFactory factory, Ticker ticker)
Create a newIoEventLoopthat is owned by the user and so needs to be driven by the user with the givenThread. This means that the user is responsible to call eitherrunNow()orrun(long)to execute IO or tasks that were submitted to thisIoEventLoop.- Parameters:
parent- the parentIoEventLoopGroupornullif no parent.owningThread- theThreadthat executes the IO and tasks for thisIoEventLoop. The user will use thisThreadto callrunNow()orrun(long)to make progress. Ifnull, must be set later usingsetOwningThread(Thread).factory- theIoHandlerFactorythat will be used to create theIoHandlerthat is used by thisIoEventLoop.ticker- Theticker()to use for this event loop. Note that theIoHandlerdoes not use the ticker, so if the ticker advances faster than system time, you may have towakeup()this event loop manually.
-
-
Method Detail
-
ticker
public Ticker ticker()
Description copied from interface:EventExecutorGroupThe ticker for this executor. Usually theEventExecutorGroup.schedule(java.lang.Runnable, long, java.util.concurrent.TimeUnit)methods will follow thesystem ticker(i.e.System.nanoTime()), but especially for testing it is sometimes useful to have more control over the ticker. In that case, this method will be overridden. Code that schedules tasks on this executor should use this ticker in order to stay consistent with the executor (e.g. not be surprised by scheduled tasks running "early").- Specified by:
tickerin interfaceEventExecutorGroup- Overrides:
tickerin classAbstractScheduledEventExecutor- Returns:
- The ticker for this scheduler
-
runNonBlockingTasks
public int runNonBlockingTasks(long timeoutNanos)
Poll and run tasks from the task queue, until the task queue is empty or the given deadline is exceeded.
IftimeoutNanosis less or equals 0, no deadline is applied.- Parameters:
timeoutNanos- the maximum time in nanoseconds to run tasks.
-
runNow
public int runNow(long runAllTasksTimeoutNanos)
Executes all ready IO and tasks for thisIoEventLoop. This methods will NOT block and wait for IO / tasks to be ready, it will just return directly if there is nothing to do.Must be called from the owning
Threadthat was passed as a parameter on construction.- Parameters:
runAllTasksTimeoutNanos- the maximum time in nanoseconds to run tasks. If= 0, no timeout is applied; if< 0it just perform I/O tasks.- Returns:
- the number of IO and tasks executed.
- Throws:
java.lang.IllegalStateException- if the method is not called from the owningThread.
-
runNow
public int runNow()
Run all ready IO and tasks for thisIoEventLoop. This methods will NOT block and wait for IO / tasks to be ready, it will just return directly if there is nothing to do.Must be called from the owning
Threadthat was passed as a parameter on construction.- Returns:
- the number of IO and tasks executed.
-
run
public int run(long waitNanos, long runAllTasksTimeoutNanos)Run all ready IO and tasks for thisIoEventLoop. This methods will block and wait for IO / tasks to be ready if there is nothing to process atm for the givenwaitNanos.Must be called from the owning
Threadthat was passed as an parameter on construction.- Parameters:
runAllTasksTimeoutNanos- the maximum time in nanoseconds to run tasks. If= 0, no timeout is applied; if< 0it just perform I/O tasks.waitNanos- the maximum amount of nanoseconds to wait before returning. IF0it will block until there is some IO / tasks ready, if-1will not block at all and just return directly if there is nothing to run (likerunNow()).- Returns:
- the number of IO and tasks executed.
-
run
public int run(long waitNanos)
Run all ready IO and tasks for thisIoEventLoop. This methods will block and wait for IO / tasks to be ready if there is nothing to process atm for the givenwaitNanos.Must be called from the owning
Threadthat was passed as an parameter on construction.- Parameters:
waitNanos- the maximum amount of nanoseconds to wait before returning. IF0it will block until there is some IO / tasks ready, if-1will not block at all and just return directly if there is nothing to run (likerunNow()).- Returns:
- the number of IO and tasks executed.
-
wakeup
public void wakeup()
Force a wakeup and so therun(long)method will unblock and return even if there was nothing to do.
-
next
public ManualIoEventLoop next()
Description copied from interface:EventExecutorGroupReturns one of theEventExecutors managed by thisEventExecutorGroup.- Specified by:
nextin interfaceEventExecutorGroup- Specified by:
nextin interfaceEventLoopGroup- Specified by:
nextin interfaceIoEventLoop- Specified by:
nextin interfaceIoEventLoopGroup- Overrides:
nextin classAbstractEventExecutor
-
parent
public IoEventLoopGroup parent()
Description copied from interface:EventExecutorReturn theEventExecutorGroupwhich is the parent of thisEventExecutor,- Specified by:
parentin interfaceEventExecutor- Specified by:
parentin interfaceEventLoop- Overrides:
parentin classAbstractEventExecutor
-
register
@Deprecated public ChannelFuture register(Channel channel)
Deprecated.Description copied from interface:EventLoopGroupRegister aChannelwith thisEventLoop. The returnedChannelFuturewill get notified once the registration was complete.- Specified by:
registerin interfaceEventLoopGroup- Specified by:
registerin interfaceIoEventLoopGroup
-
register
@Deprecated public ChannelFuture register(ChannelPromise promise)
Deprecated.Description copied from interface:EventLoopGroupRegister aChannelwith thisEventLoopusing aChannelFuture. The passedChannelFuturewill get notified once the registration was complete and also will get returned.- Specified by:
registerin interfaceEventLoopGroup- Specified by:
registerin interfaceIoEventLoopGroup
-
register
public Future<IoRegistration> register(IoHandle handle)
Description copied from interface:IoEventLoop- Specified by:
registerin interfaceIoEventLoop- Specified by:
registerin interfaceIoEventLoopGroup- Parameters:
handle- theIoHandleto register.- Returns:
- the
Futurethat is notified once the operations completes.
-
register
@Deprecated public ChannelFuture register(Channel channel, ChannelPromise promise)
Deprecated.Description copied from interface:EventLoopGroupRegister aChannelwith thisEventLoop. The passedChannelFuturewill get notified once the registration was complete and also will get returned.- Specified by:
registerin interfaceEventLoopGroup
-
isCompatible
public boolean isCompatible(java.lang.Class<? extends IoHandle> handleType)
Description copied from interface:IoEventLoopGroupReturnstrueif the given type is compatible with thisIoEventLoopGroupand so can be registered to the containedIoEventLoops,falseotherwise.- Specified by:
isCompatiblein interfaceIoEventLoop- Specified by:
isCompatiblein interfaceIoEventLoopGroup- Parameters:
handleType- the type of theIoHandle.- Returns:
- if compatible of not.
-
isIoType
public boolean isIoType(java.lang.Class<? extends IoHandler> handlerType)
Description copied from interface:IoEventLoopGroup- Specified by:
isIoTypein interfaceIoEventLoop- Specified by:
isIoTypein interfaceIoEventLoopGroup- Parameters:
handlerType- the type of theIoHandler.- Returns:
- if used or not.
-
inEventLoop
public boolean inEventLoop(java.lang.Thread thread)
Description copied from interface:EventExecutorReturntrueif the givenThreadis executed in the event loop,falseotherwise.- Specified by:
inEventLoopin interfaceEventExecutor
-
setOwningThread
public void setOwningThread(java.lang.Thread owningThread)
Set the owning thread that will callrun(io.netty.channel.IoHandlerContext, long). May only be called once, and only if the owning thread was not set in the constructor already.- Parameters:
owningThread- The owning thread
-
shutdownGracefully
public Future<?> shutdownGracefully(long quietPeriod, long timeout, java.util.concurrent.TimeUnit unit)
Description copied from interface:EventExecutorGroupSignals this executor that the caller wants the executor to be shut down. Once this method is called,EventExecutorGroup.isShuttingDown()starts to returntrue, and the executor prepares to shut itself down. UnlikeEventExecutorGroup.shutdown(), graceful shutdown ensures that no tasks are submitted for 'the quiet period' (usually a couple seconds) before it shuts itself down. If a task is submitted during the quiet period, it is guaranteed to be accepted and the quiet period will start over.- Specified by:
shutdownGracefullyin interfaceEventExecutorGroup- Parameters:
quietPeriod- the quiet period as described in the documentationtimeout- the maximum amount of time to wait until the executor is EventExecutorGroup.shutdown() regardless if a task was submitted during the quiet periodunit- the unit ofquietPeriodandtimeout- Returns:
- the
EventExecutorGroup.terminationFuture()
-
shutdown
@Deprecated public void shutdown()
Deprecated.- Specified by:
shutdownin interfaceEventExecutorGroup- Specified by:
shutdownin interfacejava.util.concurrent.ExecutorService- Specified by:
shutdownin classAbstractEventExecutor
-
terminationFuture
public Future<?> terminationFuture()
Description copied from interface:EventExecutorGroupReturns theFuturewhich is notified when allEventExecutors managed by thisEventExecutorGrouphave been terminated.- Specified by:
terminationFuturein interfaceEventExecutorGroup
-
isShuttingDown
public boolean isShuttingDown()
Description copied from interface:EventExecutorGroupReturnstrueif and only if allEventExecutors managed by thisEventExecutorGroupare being shut down gracefully or was shut down.- Specified by:
isShuttingDownin interfaceEventExecutorGroup
-
isShutdown
public boolean isShutdown()
- Specified by:
isShutdownin interfacejava.util.concurrent.ExecutorService
-
isTerminated
public boolean isTerminated()
- Specified by:
isTerminatedin interfacejava.util.concurrent.ExecutorService
-
awaitTermination
public boolean awaitTermination(long timeout, java.util.concurrent.TimeUnit unit) throws java.lang.InterruptedException- Specified by:
awaitTerminationin interfacejava.util.concurrent.ExecutorService- Throws:
java.lang.InterruptedException
-
execute
public void execute(java.lang.Runnable command)
- Specified by:
executein interfacejava.util.concurrent.Executor
-
invokeAny
public <T> T invokeAny(java.util.Collection<? extends java.util.concurrent.Callable<T>> tasks) throws java.lang.InterruptedException, java.util.concurrent.ExecutionException- Specified by:
invokeAnyin interfacejava.util.concurrent.ExecutorService- Overrides:
invokeAnyin classjava.util.concurrent.AbstractExecutorService- Throws:
java.lang.InterruptedExceptionjava.util.concurrent.ExecutionException
-
invokeAny
public <T> T invokeAny(java.util.Collection<? extends java.util.concurrent.Callable<T>> tasks, long timeout, java.util.concurrent.TimeUnit unit) throws java.lang.InterruptedException, java.util.concurrent.ExecutionException, java.util.concurrent.TimeoutException- Specified by:
invokeAnyin interfacejava.util.concurrent.ExecutorService- Overrides:
invokeAnyin classjava.util.concurrent.AbstractExecutorService- Throws:
java.lang.InterruptedExceptionjava.util.concurrent.ExecutionExceptionjava.util.concurrent.TimeoutException
-
invokeAll
public <T> java.util.List<java.util.concurrent.Future<T>> invokeAll(java.util.Collection<? extends java.util.concurrent.Callable<T>> tasks) throws java.lang.InterruptedException- Specified by:
invokeAllin interfacejava.util.concurrent.ExecutorService- Overrides:
invokeAllin classjava.util.concurrent.AbstractExecutorService- Throws:
java.lang.InterruptedException
-
invokeAll
public <T> java.util.List<java.util.concurrent.Future<T>> invokeAll(java.util.Collection<? extends java.util.concurrent.Callable<T>> tasks, long timeout, java.util.concurrent.TimeUnit unit) throws java.lang.InterruptedException- Specified by:
invokeAllin interfacejava.util.concurrent.ExecutorService- Overrides:
invokeAllin classjava.util.concurrent.AbstractExecutorService- Throws:
java.lang.InterruptedException
-
-