|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectnaga.eventmachine.EventMachine
public class EventMachine
EventMachine is a simple event service for driving asynchronous and delayed tasks together with the a Naga NIOService.
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 Summary | |
---|---|
EventMachine()
Creates a new EventMachine with an embedded NIOService. |
Method Summary | |
---|---|
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. |
ExceptionObserver |
getObserver()
The current ExceptionObserver used by this service. |
java.util.Queue<naga.eventmachine.DelayedAction> |
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 |
start()
Causes the event machine to start running on a separate thread together with the NIOService. |
void |
stop()
Stops the event machine thread. |
long |
timeOfNextEvent()
Returns the time when the next scheduled event will execute. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
public EventMachine() throws java.io.IOException
java.io.IOException
- if we fail to set up the internal NIOService.Method Detail |
---|
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, may not be null.
java.lang.NullPointerException
- if the observer is null.public long timeOfNextEvent()
public void start()
NIOService.selectNonBlocking()
and related
functions) on another thread if the EventMachine is used.
public void stop()
public ExceptionObserver getObserver()
Will default to ExceptionObserver.DEFAULT if no observer was set.
public NIOService getNIOService()
public java.util.Queue<naga.eventmachine.DelayedAction> getQueue()
public int getQueueSize()
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |