Class Messenger
- java.lang.Object
-
- com.yahoo.messagebus.Messenger
-
- All Implemented Interfaces:
java.lang.Runnable
public class Messenger extends java.lang.Object implements java.lang.Runnable
This class implements a single thread that is able to process arbitrary tasks. Tasks are enqueued using the synchronized
enqueue(Task)
method, and are run in the order they were enqueued.- Author:
- Simon Thoresen Hult
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interface
Messenger.Task
Defines the required interface for tasks to be posted to this worker.
-
Constructor Summary
Constructors Constructor Description Messenger()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
deliverMessage(Message msg, MessageHandler handler)
Convenience method to post aMessenger.Task
that delivers aMessage
to aMessageHandler
to the queue of tasks to be executed.void
deliverReply(Reply reply, ReplyHandler handler)
Convenience method to post aMessenger.Task
that delivers aReply
to aReplyHandler
to the queue of tasks to be executed.boolean
destroy()
Sets the destroyed flag to true.void
enqueue(Messenger.Task task)
Enqueues the given task in the list of tasks that this worker is to process.void
run()
void
start()
Starts the internal thread.void
sync()
Handshakes with the internal thread.
-
-
-
Method Detail
-
start
public void start()
Starts the internal thread. This must be done AFTER all recurrent tasks have been added.
- See Also:
addRecurrentTask(Task)
-
deliverMessage
public void deliverMessage(Message msg, MessageHandler handler)
Convenience method to post a
Messenger.Task
that delivers aMessage
to aMessageHandler
to the queue of tasks to be executed.- Parameters:
msg
- The message to send.handler
- The handler to send to.
-
deliverReply
public void deliverReply(Reply reply, ReplyHandler handler)
Convenience method to post a
Messenger.Task
that delivers aReply
to aReplyHandler
to the queue of tasks to be executed.- Parameters:
reply
- The reply to return.handler
- The handler to return to.
-
enqueue
public void enqueue(Messenger.Task task)
Enqueues the given task in the list of tasks that this worker is to process. If this thread has been destroyed previously, this method invokes
Messenger.Task.destroy()
.- Parameters:
task
- The task to enqueue.
-
sync
public void sync()
Handshakes with the internal thread. If this method is called using the messenger thread, this will deadlock.
-
destroy
public boolean destroy()
Sets the destroyed flag to true. The very first time this method is called, it cleans up all its dependencies. Even if you retain a reference to this object, all of its content is allowed to be garbage collected.
- Returns:
- True if content existed and was destroyed.
-
run
public void run()
- Specified by:
run
in interfacejava.lang.Runnable
-
-