public class EventMachine
extends java.lang.Object
Creating and starting an event machine:
EventMachine em = new EventMachine(); // Start our event machine thread: em.start();Delayed execution:
em.executeLater(new Runnable() { public void run() { // Code here will execute after 1 second on the nio thread. } }, 1000);Asynchronous execution, i.e. putting a task from another thread to be executed on the EventMachine thread.
em.asyncExecute(new Runnable() { public void run() { // Code here will be executed on the nio thread. } });It is possible to cancel scheduled tasks:
// Schedule an event DelayedEvent event = em.executeLater(new Runnable() public void run() { // Code to run in 1 minute. } }, 60000); // Cancel the event before it is executed. event.cancel();
Constructor | Description |
---|---|
EventMachine() |
Creates a new EventMachine with an embedded NIOService.
|
Modifier and Type | Method | Description |
---|---|---|
void |
asyncExecute(java.lang.Runnable runnable) |
Execute a runnable on the Event/NIO thread.
|
DelayedEvent |
executeAt(java.lang.Runnable runnable,
java.util.Date date) |
Execute a runnable on the Event/NIO thread after at a certain time.
|
DelayedEvent |
executeLater(java.lang.Runnable runnable,
long msDelay) |
Execute a runnable on the Event/NIO thread after a delay.
|
NIOService |
getNIOService() |
Returns the NIOService used by this event service.
|
java.util.Queue<DelayedEvent> |
getQueue() |
Return the current event service queue.
|
int |
getQueueSize() |
Return the current queue size.
|
void |
setObserver(ExceptionObserver observer) |
Sets the ExceptionObserver for this service.
|
void |
shutdown() |
Stops the event machine and closes the underlying NIO service, it is not possible to
restart the event machine after shutdown.
|
void |
start() |
Causes the event machine to start running on a separate thread together with the
NIOService.
|
void |
stop() |
Stops the event machine thread, it may be restarted using start()
|
long |
timeOfNextEvent() |
Returns the time when the next scheduled event will execute.
|
public EventMachine() throws java.io.IOException
java.io.IOException
- if we fail to set up the internal NIOService.public void asyncExecute(java.lang.Runnable runnable)
This method is thread-safe.
runnable
- the runnable to execute on the server thread as soon as possible,public DelayedEvent executeLater(java.lang.Runnable runnable, long msDelay)
This is the primary way to execute delayed events, typically time-outs and similar behaviour.
This method is thread-safe.
runnable
- the runnable to execute after the given delay.msDelay
- the delay until executing this runnable.public DelayedEvent executeAt(java.lang.Runnable runnable, java.util.Date date)
runnable
- the runnable to execute at the given time.date
- the time date when this runnable should execute.public void setObserver(ExceptionObserver observer)
The observer will receive all exceptions thrown by the underlying NIOService and by queued events.
This method is thread-safe.
observer
- the observer to use, null will cause exceptions to log to stderrpublic long timeOfNextEvent()
public void start()
NIOService.selectNonBlocking()
and related
functions) on another thread if the EventMachine is used.public void stop()
public void shutdown()
public NIOService getNIOService()
public java.util.Queue<DelayedEvent> getQueue()
public int getQueueSize()