F
- type of FirstTurn
L
- type of LastTurn
O
- type of OutputTurn
I
- type of InputTurn
C
- type of DialogueContext
public final class SynchronousDialogueChannel<I extends InputTurn,O extends OutputTurn,F extends FirstTurn,L extends LastTurn,C extends DialogueContext<I,O>> extends Object implements DialogueChannel<I,O>
DialogueChannel
allowing turns to be apparently
exchanged in a regular synchronous fashion while underneath, it is done in
asynchronously.
This is the most useful implementation of DialogueChannel
. It is used
by the DialogueServlet
to handle different HTTP requests occurring in
separate threads.
When the controller (such as the DialogueServlet
) uses the
SynchronousDialogueChannel
, it uses the
start(Dialogue, FirstTurn, Duration, DialogueContext)
method to
start the dialogue and the doTurn(InputTurn, Duration)
method to
send input turns to the dialogue. In exchange, the
SynchronousDialogueChannel
will return a Step
:
OutputTurnStep
: if the dialogue sends an OutputTurn in
responseLastTurnStep
: when the dialogue is doneErrorStep
: if an error occurred following the delivery of the
InputTurn
The SynchronousDialogueChannel
has states related to the dialogue:
isDialogueStarted()
and isDialogueDone()
methods
correspond to those states. Additionally, the isDialogueActive()
method tells if the dialogue is started but not yet done.
The SynchronousDialogueChannel
internally keeps two
SynchronousQueues
:
Dialogue
to the
controllerDialogue
setDefaultReceiveFromControllerTimeout(Duration)
and
setDefaultReceiveFromDialogueTimeout(Duration)
. These properties are
used if the timeout
parameter is null
for the
doTurn(OutputTurn, Duration)
,
start(Dialogue, FirstTurn, Duration, DialogueContext)
and
doTurn(InputTurn, Duration)
methods.
If not set, defaultReceiveFromControllerTimeout
defaults to 5
minutes and defaultReceiveFromDialogueTimeout
defaults to
1 minute. This gives 5 minutes for the controller (like the
DialogueServlet
) to provide the InputTurn
and 1 minute for
the controller to provide the next turn or error before a Timeout
exception is raised.
Note: There also exists a less important sendTimeout
property which indicates the maximum duration for send operations. A send
operation occurs when the controller sends the input turn to the dialogue and
when the dialogue send the output turn, the last turn or an error to the
controller. Since we expect the dialogue and the controller to already be in
a waiting state when the other thread sends the turn, the send operation
should not block. For this reason, the default value for this property is
5 seconds. This property can be set with the
setSendTimeout(Duration)
property.
DialogueServlet
,
Step
,
OutputTurnStep
,
LastTurnStep
,
ErrorStep
Constructor and Description |
---|
SynchronousDialogueChannel() |
Modifier and Type | Method and Description |
---|---|
void |
addListener(DialogueChannelListener<I,O> listener)
Adds a listener to this dialogue channel.
|
Step<O,L> |
doTurn(I turn,
Duration timeout)
Performs a turn exchange: the dialogue channel will return the Step
|
I |
doTurn(O turn,
Duration timeout)
Performs a turn exchange: the dialogue channel will return the
InputTurn |
Duration |
getDefaultReceiveFromControllerTimeout()
Gets the maximum duration the dialogue thread can wait for a turn from
the controller thread when not specified by the dialogue.
|
Duration |
getDefaultReceiveFromDialogueTimeout()
Retrieves the maximum duration for send operations.
|
Duration |
getSendTimeout()
Gets the maximum duration for send operations.
|
boolean |
isDialogueActive()
Tells if the dialogue has started but not yet ended.
|
boolean |
isDialogueDone()
Tells if the dialogue has ended.
|
boolean |
isDialogueStarted()
Tells if the dialogue has started.
|
void |
join(Duration timeout)
Waits for the dialogue thread to end.
|
void |
removeListener(DialogueChannelListener<I,O> listener)
Removes a listener from this dialogue channel.
|
void |
setDefaultReceiveFromControllerTimeout(Duration defaultReceiveFromControllerTimeout)
Sets the maximum duration the dialogue thread can wait for a turn from
the controller thread when not specified by the dialogue.
|
void |
setDefaultReceiveFromDialogueTimeout(Duration defaultReceiveFromDialogueTimeout)
Sets the maximum duration the controller thread can wait for a turn or an
error from the dialogue thread when not specified by the controller.
|
void |
setLogger(Logger logger)
Sets the logger for this dialogue channel.
|
void |
setSendTimeout(Duration sendTimeout)
Sets the maximum duration for send operations.
|
Step<O,L> |
start(Dialogue<I,O,F,L,C> dialogue,
F firstTurn,
Duration timeout,
C context)
Starts a
Dialogue in a new thread. |
void |
stop()
Stops the dialogue.
|
void |
stop(Duration timeout)
Stops the dialogue and wait for the dialogue thread to end.
|
public SynchronousDialogueChannel()
public Duration getSendTimeout()
public void setSendTimeout(Duration sendTimeout)
sendTimeout
- The timeout value. Cannot be null
. A
value of Duration.ZERO (or equivalent) means to wait forever.public Duration getDefaultReceiveFromDialogueTimeout()
public void setDefaultReceiveFromDialogueTimeout(Duration defaultReceiveFromDialogueTimeout)
defaultReceiveFromDialogueTimeout
- The default timeout to use when
not specified by the controller. Cannot be null
.public Duration getDefaultReceiveFromControllerTimeout()
public void setDefaultReceiveFromControllerTimeout(Duration defaultReceiveFromControllerTimeout)
defaultReceiveFromControllerTimeout
- The default timeout to use
when not specified by the dialogue. Cannot be
null
.public void setLogger(Logger logger)
logger
- The logger. Cannot be null
.public Step<O,L> start(Dialogue<I,O,F,L,C> dialogue, F firstTurn, Duration timeout, C context) throws Timeout, InterruptedException
Dialogue
in a new thread.dialogue
- Dialogue to start. Cannot be null
.firstTurn
- First turn used passed to
Dialogue.run(FirstTurn, DialogueContext)
method of the
dialogue. Cannot be null
.timeout
- maximum time allowed to receive the turn from the
dialogue. If null
, uses the
defaultReceiveFromDialogueTimeout
property. A
value of Duration.ZERO (or equivalent) means to wait forever.context
- Dialogue context to pass to
Dialogue.run(FirstTurn, DialogueContext)
method of the
dialogue. Cannot be null
.Timeout
- If no result can be obtain from dialogue after delay
specified by timeout
parameter.InterruptedException
- if the dialogue has been interrupted.public boolean isDialogueStarted()
true
if the dialogue has started, false
otherwise.public boolean isDialogueDone()
true
if the dialogue has ended, false
otherwise.public boolean isDialogueActive()
true
if the dialogue has started but not ended yet,
false
otherwise.public void stop(Duration timeout) throws InterruptedException
timeout
- Time to wait for dialogue thread to terminate. A value of
Duration.ZERO (or equivalent) means to wait forever.InterruptedException
- if the current thread was interrupted while
waiting for the dialogue thread to terminate.public void stop()
public void join(Duration timeout) throws InterruptedException
timeout
- maximum time to wait for the thread to end. A value of
Duration.ZERO (or equivalent) means to wait forever.InterruptedException
- if the current thread was interrupted while
waiting for the dialogue thread to terminate.public I doTurn(O turn, Duration timeout) throws Timeout, InterruptedException
InputTurn
doTurn
in interface DialogueChannel<I extends InputTurn,O extends OutputTurn>
turn
- The output turn to send. Cannot be null
.timeout
- maximum time allowed to receive the turn from the
dialogue. If null
, uses the
defaultReceiveFromControllerTimeout
. A value of
Duration.ZERO (or equivalent) means to wait forever.InputTurn
. Cannot be null
.Timeout
- if the dialogue channel has not been able to give the
InputTurn before timeout
parameter.InterruptedException
- if the thread has been interrupted while
waiting for the result.public Step<O,L> doTurn(I turn, Duration timeout) throws Timeout, InterruptedException
turn
- the input turn to send to the dialoguetimeout
- maximum time allowed to receive the turn from the
dialogue. If null
, uses the
defaultReceiveFromDialogueTimeout
property. A
value of Duration.ZERO (or equivalent) means to wait forever.Step
wrapping the dialogue next stepTimeout
- If no result can be obtain from dialogue after delay
specified by timeout
parameter.InterruptedException
- if the thread was interrupted wile waiting
for the dialogue step.public void addListener(DialogueChannelListener<I,O> listener)
DialogueChannel
addListener
in interface DialogueChannel<I extends InputTurn,O extends OutputTurn>
listener
- the listener to add.public void removeListener(DialogueChannelListener<I,O> listener)
DialogueChannel
removeListener
in interface DialogueChannel<I extends InputTurn,O extends OutputTurn>
listener
- the listener to remove.Copyright © 2019 Nu Echo Inc..