public final class Eventloop extends Object implements Runnable, EventloopExecutor, Scheduler, CurrentTimeProvider, Initializable<Eventloop>, EventloopJmxMBeanEx
Eventloop thread must be avoided. Async versions
of such operations should be used.
Eventloop represents infinite loop with only one blocking operation
selector.select() which selects a set of keys whose corresponding
channels are ready for I/O operations. With these keys and queues with
tasks, which was added to Eventloop from the outside, it begins
asynchronous executing from one thread it in method run() which is
overridden because it is implementation of Runnable. Working of this
eventloop will be ended, when it has not selected keys and its queues with
tasks are empty.
| Modifier and Type | Class and Description |
|---|---|
class |
Eventloop.Scope |
| Modifier and Type | Field and Description |
|---|---|
static AsyncTimeoutException |
CONNECT_TIMEOUT |
static Duration |
DEFAULT_IDLE_INTERVAL |
THREAD_LOCAL_TIME_PROVIDER| Modifier and Type | Method and Description |
|---|---|
void |
breakEventloop() |
void |
closeChannel(SelectableChannel channel) |
void |
closeChannel(SelectionKey channelKey) |
void |
completeExternalTask()
Notifies the event loop about completion of corrensponding operation in other threads.
|
Stage<SocketChannel> |
connect(SocketAddress address)
Connects to given socket address asynchronously.
|
Stage<SocketChannel> |
connect(SocketAddress address,
int timeout)
Connects to given socket address asynchronously with a specified timeout value.
|
static Eventloop |
create() |
static Eventloop |
create(CurrentTimeProvider currentTimeProvider) |
static DatagramChannel |
createDatagramChannel(DatagramSocketSettings datagramSocketSettings,
InetSocketAddress bindAddress,
InetSocketAddress connectAddress)
Registers new UDP connection in this eventloop.
|
long |
currentTimeMillis()
Returns current time of this eventloop
|
void |
execute(Runnable runnable)
Posts a new task from other threads.
|
static Eventloop |
getCurrentEventloop() |
Eventloop |
getEventloop() |
Thread |
getEventloopThread() |
FatalErrorHandler |
getFatalErrorHandler() |
Duration |
getIdleInterval() |
boolean |
getKeepAlive() |
int |
getLoop() |
EventloopStats |
getStats() |
int |
getThreadPriority() |
ThrottlingController |
getThrottlingController() |
long |
getTick() |
boolean |
inEventloopThread() |
boolean |
isExtendedMonitoring() |
void |
keepAlive(boolean keepAlive)
Sets the flag keep alive, if it is true it means that working of this Eventloop will be
continued even in case when all tasks have been executed and it doesn't have selected keys.
|
ServerSocketChannel |
listen(InetSocketAddress address,
ServerSocketSettings serverSocketSettings,
AcceptCallback acceptCallback)
Creates
ServerSocketChannel that listens on InetSocketAddress. |
void |
post(Runnable runnable)
Posts a new task to the beginning of localTasks.
|
void |
postLater(Runnable runnable)
Posts a new task to the end localTasks.
|
void |
recordFatalError(Throwable e,
Object context) |
long |
refreshTimestampAndGet() |
void |
run()
Overridden method from Runnable that executes tasks while this eventloop is alive.
|
ScheduledRunnable |
schedule(long timestamp,
Runnable runnable)
Schedules new task.
|
ScheduledRunnable |
scheduleBackground(long timestamp,
Runnable runnable)
Schedules new background task.
|
static void |
setGlobalFatalErrorHandler(FatalErrorHandler handler) |
void |
setIdleInterval(Duration idleInterval) |
void |
startExtendedMonitoring() |
void |
startExternalTask()
Notifies the event loop about concurrent operation in other threads.
|
void |
stopExtendedMonitoring() |
<T> CompletableFuture<T> |
submit(AsyncCallable<T> asyncCallable)
Works the same as
submit(Runnable) except for AsyncCallable |
<T> CompletableFuture<T> |
submit(Callable<T> callable)
Works the same as
submit(Runnable) except for Callable |
CompletableFuture<Void> |
submit(Runnable runnable)
Submits
Runnable to eventloop for execution |
long |
tick() |
String |
toString() |
Eventloop.Scope |
useCurrentThread() |
Eventloop |
withCurrentThread() |
Eventloop |
withFatalErrorHandler(FatalErrorHandler fatalErrorHandler) |
Eventloop |
withIdleInterval(Duration idleInterval) |
Eventloop |
withSelectorProvider(SelectorProvider selectorProvider) |
Eventloop |
withThreadName(String threadName) |
Eventloop |
withThreadPriority(int threadPriority) |
Eventloop |
withThrottlingController(ThrottlingController throttlingController) |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitdelay, delay, delayBackground, delayBackground, schedule, scheduleBackgroundcurrentInstant, ofSystem, ofThreadLocal, ofTimeSequenceinitialize, initializegetSmoothingWindow, resetStats, setSmoothingWindowpublic static final AsyncTimeoutException CONNECT_TIMEOUT
public static final Duration DEFAULT_IDLE_INTERVAL
public static Eventloop create()
public static Eventloop create(CurrentTimeProvider currentTimeProvider)
public Eventloop withThreadPriority(int threadPriority)
public Eventloop withThrottlingController(@Nullable ThrottlingController throttlingController)
public Eventloop withFatalErrorHandler(FatalErrorHandler fatalErrorHandler)
public Eventloop withSelectorProvider(SelectorProvider selectorProvider)
public Eventloop withCurrentThread()
public Eventloop.Scope useCurrentThread()
public static Eventloop getCurrentEventloop()
@Nullable public ThrottlingController getThrottlingController()
public void closeChannel(SelectionKey channelKey)
public void closeChannel(SelectableChannel channel)
public boolean inEventloopThread()
public void keepAlive(boolean keepAlive)
keepAlive - flag for settingpublic void breakEventloop()
public void run()
public ServerSocketChannel listen(InetSocketAddress address, ServerSocketSettings serverSocketSettings, AcceptCallback acceptCallback) throws IOException
ServerSocketChannel that listens on InetSocketAddress.address - InetSocketAddress that server will listenserverSocketSettings - settings from this server channelacceptCallback - callback that will be called when new incoming connection is being accepted. It can be called multiple times.IOException - If some I/O error occurspublic static DatagramChannel createDatagramChannel(DatagramSocketSettings datagramSocketSettings, @Nullable InetSocketAddress bindAddress, @Nullable InetSocketAddress connectAddress) throws IOException
bindAddress - address for binding DatagramSocket for this connection.IOException - if an I/O error occurs on opening DatagramChannelpublic Stage<SocketChannel> connect(SocketAddress address)
address - socketChannel's addresspublic Stage<SocketChannel> connect(SocketAddress address, int timeout)
address - socketChannel's addresstimeout - the timeout value to be used in milliseconds, 0 as default system connection timeoutpublic long tick()
public void post(Runnable runnable)
runnable - runnable of this taskpublic void postLater(Runnable runnable)
runnable - runnable of this taskpublic void execute(Runnable runnable)
public ScheduledRunnable schedule(long timestamp, Runnable runnable)
ScheduledRunnable with this runnable.public ScheduledRunnable scheduleBackground(long timestamp, Runnable runnable)
ScheduledRunnable with this runnable.
If eventloop contains only background tasks, it will be closed
scheduleBackground in interface Schedulertimestamp - timestamp after which task will be ranrunnable - runnable of this taskpublic void startExternalTask()
public void completeExternalTask()
public long refreshTimestampAndGet()
public long currentTimeMillis()
currentTimeMillis in interface CurrentTimeProviderpublic Eventloop getEventloop()
getEventloop in interface EventloopJmxMBeanpublic CompletableFuture<Void> submit(Runnable runnable)
Runnable to eventloop for execution
Runnable is executed in the eventloop thread
submit in interface EventloopExecutorrunnable - to be executedCompletableFuture that completes when runnable completespublic <T> CompletableFuture<T> submit(Callable<T> callable)
submit(Runnable) except for Callablesubmit in interface EventloopExecutorpublic <T> CompletableFuture<T> submit(AsyncCallable<T> asyncCallable)
submit(Runnable) except for AsyncCallablesubmit in interface EventloopExecutorpublic static void setGlobalFatalErrorHandler(FatalErrorHandler handler)
public void startExtendedMonitoring()
public void stopExtendedMonitoring()
public boolean isExtendedMonitoring()
public int getLoop()
public long getTick()
public FatalErrorHandler getFatalErrorHandler()
public int getThreadPriority()
public boolean getKeepAlive()
public EventloopStats getStats()
public Duration getIdleInterval()
public void setIdleInterval(Duration idleInterval)
Copyright © 2019. All rights reserved.