StateType
- public abstract class TaskActivity<StateType> extends AbstractActivity<StateType> implements ActivityStateListener
AbstractActivity
that
manages certain conversations and uses the state changes in the conversations
as triggers for transactions in own state. Unlike Conversation activities,
messages are accepted even if the task is not in a state where a transition
is possible with that message. This class ensures implementors that those
messages are saved and will trigger transformations when (if ever) the task
enters an appropriate state.
The task is also registered against the PeerInterface
and
routes all messages received through that interface to the appropriate
conversations.endState, startState, stateChangedLatch
Constructor and Description |
---|
TaskActivity(HyperGraphPeer thisPeer,
StateType start,
StateType end) |
TaskActivity(HyperGraphPeer thisPeer,
java.util.UUID taskId,
StateType start,
StateType end) |
Modifier and Type | Method and Description |
---|---|
protected Conversation<?> |
createNewConversation(Json msg)
Create a new conversation based on the message content.
|
protected void |
doRun()
Overriden by implementors to do the actual work
|
PeerInterface |
getPeerInterface() |
java.util.UUID |
getTaskId() |
HyperGraphPeer |
getThisPeer() |
protected void |
handleActivity(java.lang.reflect.Method method,
AbstractActivity<?> activity)
Executes a transition
|
void |
handleMessage(Json msg)
Called by the peer interface when a message arrives for this task.
|
protected void |
initiate()
This method is called the first time the task is scheduled to run.
|
protected void |
registerConversation(Conversation<?> conversation,
java.util.UUID conversationId) |
protected void |
registerConversationHandler(StateType fromState,
java.lang.Object conversationState,
java.lang.String functionName,
StateType toState)
Whenever the task is in state
fromState and a conversation is in state
conversationState the functionName function is called after
the current state of the task is set to toState Working (this ensures
that no other messages are processed while executing the function). |
protected void |
sendReply(Json originalMsg,
Json reply) |
void |
setTaskId(java.util.UUID taskId) |
void |
stateChanged(java.lang.Object newState,
AbstractActivity<?> activity) |
afterStateChanged, compareAndSetState, getEndState, getStartState, getState, isStopped, run, setState, setStateListener
public TaskActivity(HyperGraphPeer thisPeer, StateType start, StateType end)
public TaskActivity(HyperGraphPeer thisPeer, java.util.UUID taskId, StateType start, StateType end)
protected void sendReply(Json originalMsg, Json reply)
protected void handleActivity(java.lang.reflect.Method method, AbstractActivity<?> activity)
method
- activity
- public void stateChanged(java.lang.Object newState, AbstractActivity<?> activity)
stateChanged
in interface ActivityStateListener
public void handleMessage(Json msg)
Called by the peer interface when a message arrives for this task. Can be
overridden by derived TaskActivity
implementations. The
default implementation delegates to the conversation identified by the
CONVERSATION_ID
attribute of the message (if any). If there's
no conversation associated with this message, an attempt to create a new one
is made by calling createNewConversation
which can be overridden
also. If createNewConversation
returns a non-null
Conversation
instance, the conversation is assigned an ID and
registered with this task.
So a task implementation can chose to implement handleMessage
and
respond to messages directly on it own or it can just implement createNewConversation
to delegate the work to conversation implementations. Or it can have a combination
of both like the following pattern:
public void handleMessage(Object msg)
{
if msg should trigger a new conversation between this peer and
the sending peer then
super.handleMessage(msg);
else
respond to msg directly here...
}
public void createNewConversation(Object msg)
{
// handleMessage determined that a new conversation must be started
// so based on the content of the message, create and return
// an appropriate conversation here.
}
msg
- The message just received.protected void initiate()
This method is called the first time the task is scheduled to run. It is
an initialization method called after the task was constructed.
The default implementation of startTask
does nothing.
initiate
in class AbstractActivity<StateType>
protected void doRun()
AbstractActivity
doRun
in class AbstractActivity<StateType>
protected void registerConversation(Conversation<?> conversation, java.util.UUID conversationId)
protected Conversation<?> createNewConversation(Json msg)
Create a new conversation based on the message content. This method should be overridden by all tasks that need to create conversations between two peers. The default implementation does not create any conversations.
msg
- The message just received from some peer in the context of this
task.Conversation
. The default implementation
returns null
.protected void registerConversationHandler(StateType fromState, java.lang.Object conversationState, java.lang.String functionName, StateType toState)
Whenever the task is in state fromState
and a conversation is in state
conversationState
the functionName
function is called after
the current state of the task is set to toState
Working (this ensures
that no other messages are processed while executing the function).
fromState
- conversationState
- functionName
- toState
- public java.util.UUID getTaskId()
public void setTaskId(java.util.UUID taskId)
public HyperGraphPeer getThisPeer()
public PeerInterface getPeerInterface()