[jboss-user] [JBoss jBPM] - Re: sessions and forks
pedrosacosta
do-not-reply at jboss.com
Tue Oct 10 06:25:27 EDT 2006
Here is the SpawningMultipleTransitions
|
| public class SpawningMultipleTransitions extends Node implements Parsable, ActionHandler {
|
|
| private final Logger log = Logger.getLogger(getClass().getName());
|
|
| public void execute(ExecutionContext executionContext) {
| Token token = executionContext.getToken();
|
| // Get process instance
| Long pid = (Long) executionContext.getVariable("processInstanceId");
| JbpmContext jbpmContext = JbpmConfiguration.getInstance().createJbpmContext();
| ProcessInstance processInstance = jbpmContext.loadProcessInstance(pid);
| //---------------------------------------------
|
| // phase one: collect all the transitionNames
| List forkedTokens = new ArrayList();
|
| // by default, the fork spawns a token for each leaving transition
| Collection transitionNames = token.getNode().getLeavingTransitionsMap().keySet();
| //-------------------------
| //----------------------------------------
|
| // phase two: create forked tokens for the collected transition names
| Iterator iter = transitionNames.iterator();
| while (iter.hasNext()) {
| String transitionName = (String) iter.next();
| forkedTokens.add(createForkedToken(token, transitionName));
| }
| //---------------------------------------
|
| // phase three: launch child tokens from the fork over the given transitions
| iter = forkedTokens.iterator();
| while( iter.hasNext() ) {
|
| ForkedToken forkedToken = (ForkedToken) iter.next();
| Token childToken = forkedToken.token;
| String leavingTransitionName = forkedToken.leavingTransitionName;
|
| ExecutionContext childExecutionContext = new ExecutionContext(childToken);
| if (leavingTransitionName!=null) {
| childToken.getNode().leave(childExecutionContext, leavingTransitionName);
| jbpmContext.save(processInstance);
| jbpmContext.save(token);
| log.info("BEM-INFO: Process instance saved with id: " + processInstance.getId());//TODO:2DEL
|
| } else {
| childToken.getNode().leave(childExecutionContext);
| }
| }
| //---------------------------------
|
| jbpmContext.close();
| }
|
| protected ForkedToken createForkedToken(Token parent, String transitionName) {
| // instantiate the new token
| Token childToken = new Token(parent, getTokenName(parent, transitionName));
|
| // create a forked token
| ForkedToken forkedToken = new ForkedToken(childToken, transitionName);
|
| return forkedToken;
| }
|
| protected String getTokenName(Token parent, String transitionName) {
| String tokenName = null;
| if ( transitionName != null ) {
| if ( ! parent.hasChild( transitionName ) ) {
| tokenName = transitionName;
| } else {
| int i = 2;
| tokenName = transitionName + Integer.toString( i );
| while ( parent.hasChild( tokenName ) ) {
| i++;
| tokenName = transitionName + Integer.toString( i );
| }
| }
| } else { // no transition name
| int size = ( parent.getChildren()!=null ? parent.getChildren().size()+1 : 1 );
| tokenName = Integer.toString(size);
| }
| return tokenName;
| }
|
| static class ForkedToken {
| Token token = null;
| String leavingTransitionName = null;
| public ForkedToken(Token token, String leavingTransitionName) {
| this.token = token;
| this.leavingTransitionName = leavingTransitionName;
| }
| }
| }
|
Here is the method that is executed before enter SpawningMultipleTransitions
| public Map nextState(String transition, Long processInstanceId){
|
| beginSessionTransaction(); // inicializar JBPM object
| ProcessInstance processInstance = jbpmContext.loadProcessInstance(processInstanceId);
|
| Token token = processInstance.getRootToken();
| String previousNodeName = token.getNode().getName();
|
| token.signal();// It's here that enter in the
| // SpawningMultipleTransitions
|
| log.info("BEM-INFO: Node transition: PreviousNode --> ActualNode\n"
| + previousNodeName + " --> " + token.getNode().getName());//TODO:2DEL
|
| jbpmContext.save(processInstance);
| jbpmContext.save(token);
| log.info("BEM-INFO: Process instance saved with id: " + processInstance.getId());//TODO:2DEL
|
| commitAndCloseSession(); // commit and close JBPM objects
|
| return result;
| }
|
View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=3977186#3977186
Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=3977186
More information about the jboss-user
mailing list