class Conversation
extends java.lang.Object
implements java.lang.Runnable
This conversation mode concept has the attribute that when one side of the session is active, the other side is blocked and vice versa. This makes all processing synchronous and single-threaded. Control flow transfers back and forth between each side of the session which is why this is described as cooperative.
A dedicated thread exists on each side of such a session. This thread uses a re-entrant blocking function which uses the thread's stack to naturally manages the call/return semantics of each reply to a synchronous request. This same blocking function is where all inbound synchronous requests are processed (asynchronous requests are still processed in the dispatcher).
There are 2 ways of using this class: with a new daemon thread or
by capturing an already existing thread. If you require a new thread
to be created for this processing, then use this class as a runnable
and then start the thread (set it as daemon mode first). If you wish to
re-use an existing thread, then once the Queue
shifts into
conversation mode, the next call into any synchronous message processing
will "capture" the thread for the duration of that processing (until the
reply message is received). This is useful in cases where there is an
existing "main" thread which calls into a primary entry point and then
stays there for the duration of the session. In this second (capture)
approach, it is important to note that the session will not properly
respond to synchronous requests until the thread is captured.
Modifier and Type | Field and Description |
---|---|
private Dispatcher |
dispatcher
The singleton instance of the dispatcher.
|
private boolean |
halted
Flag to track if the thread is still active.
|
private java.lang.Object |
lock
Internal lock mechanism.
|
private static java.util.logging.Logger |
LOG
Logger.
|
private Message |
msg
An inbound message to process.
|
private NetResource |
nr
Security manager plugin for network resources
|
private Queue |
queue
The session-specific network transport.
|
private boolean |
request
Request message if
true , otherwise it is a reply. |
private java.lang.Thread |
worker
Thread instance on which we are running, must be dedicated to this instance.
|
Constructor and Description |
---|
Conversation(Queue queue,
java.lang.Thread worker)
Constructs an instance.
|
Modifier and Type | Method and Description |
---|---|
private Message |
block()
This is the main message processing loop for this side of the
conversation.
|
(package private) void |
halt()
Halt the conversation.
|
(package private) void |
reply(Message msg)
Notify the conversation that a reply message has been received.
|
(package private) void |
request(Message msg)
Notify the conversation that a request message has been received.
|
void |
run()
Implementation of
Runnable interface. |
(package private) Message |
waitMessage(int reqId)
Block until a reply message is received where the request ID matches
the given ID.
|
private static final java.util.logging.Logger LOG
private Message msg
private boolean request
true
, otherwise it is a reply.private Queue queue
private Dispatcher dispatcher
private NetResource nr
private java.lang.Object lock
private java.lang.Thread worker
private volatile boolean halted
Conversation(Queue queue, java.lang.Thread worker)
queue
- Queue used by the conversation for message passing.worker
- In the mode where a new thread is not created for this instance, this is the
thread on which the conversation will be executing. This MUST be done if we are
in the "capturing" mode where there is an existing thread that will be executing
as the conversation thread. If a new thread is being created, then run()
will be called on that thread and this parameter should be null
.public void run()
Runnable
interface. Initializes the security context and
then calls the main message processing loop for this side of the conversation. This method
is NOT expected to ever execute when we are in "capturing" mode. It only executes when
a new thread is being created for the conversation.run
in interface java.lang.Runnable
void halt()
void request(Message msg)
msg
- The incoming message.void reply(Message msg)
msg
- The incoming message.Message waitMessage(int reqId) throws java.lang.InterruptedException, RestrictedUseException, java.lang.IllegalAccessException, java.lang.NoSuchMethodException, ApplicationRequestedStop, ProtocolViolation
reqId
- The request ID to wait for.java.lang.InterruptedException
RestrictedUseException
java.lang.IllegalAccessException
java.lang.NoSuchMethodException
ApplicationRequestedStop
ProtocolViolation
private Message block() throws java.lang.InterruptedException, RestrictedUseException, java.lang.IllegalAccessException, java.lang.NoSuchMethodException, ApplicationRequestedStop
This method will not return until it receives a reply message.
java.lang.InterruptedException
RestrictedUseException
java.lang.IllegalAccessException
java.lang.NoSuchMethodException
ApplicationRequestedStop