[jboss-user] [JBoss jBPM] - Re: Possible bug in fork-joins

JerWah do-not-reply at jboss.com
Wed Sep 6 12:42:49 EDT 2006


I recently battled this very issue.  Attached is the code for a custom join node that seems to work in all scenarios. (I edited out some of our specific code so as posted may not exactly work but you should get the gist.)


  | 
  | public class join implements ActionHandler
  | {
  | 	private static final long serialVersionUID = 1L;
  | 	private static final Log log = LogFactory.getLog(Join.class);
  | 
  | 	public void execute(ExecutionContext executionContext)
  | 	{
  | 		Node ThisNode = executionContext.getNode();
  | 		int tokenCount = 0;
  | 		try
  | 		{
  | 			tokenCount = (Integer) executionContext.getVariable(ThisNode.getName() + "tokenCount");
  | 		}
  | 		catch (Exception e)
  | 		{
  | 			log.debug(e);
  | 		}
  | 		Token token = executionContext.getToken();
  | 		if(token.isAbleToReactivateParent())
  | 		{
  | 			log.debug("Set  token: "  + token.getFullName()+ " now unable to reactivate parent.");
  | 			token.setAbleToReactivateParent(false);
  | 		}
  | 		if(!token.hasEnded())
  | 		{
  | 			token.end();
  | 			log.debug("Set token:" + token.getFullName()+ " now set to ended.");
  | 		}
  | 		//Sanity Check
  | 		log.debug("Inbound token ended status=" + token.hasEnded());
  | 		tokenCount++;
  | 		
  | 		int transitionCount = ThisNode.getArrivingTransitions().size();
  | 		
  | 		// If we have all of our tokens, then we can proceed.
  | 		//But we need to see if our parent has all it needs done or not.
  | 		// Decide if the parent fork is ready to go or not.
  | 
  | 		ExecutionContext exitContext = CalculateCurrentExitContext(token);
  | 		if(tokenCount >= transitionCount)
  | 		{
  | 			log.debug("All tokens are here..keep going...");
  | 			ThisNode.leave(exitContext);
  | 		}
  | 		else
  | 		{
  | 			log.debug("We received: " + token.getFullName()+ " and it is able to reactivate parent = " + token.isAbleToReactivateParent());
  | 			log.debug("Received: "  +  tokenCount  + " tokens out of: " + transitionCount + " paths.  Waiting for remaining");
  | 		}
  | 		executionContext.setVariable(ThisNode.getName() + "tokenCount", tokenCount);
  | 		executionContext.getJbpmContext().save(executionContext.getProcessInstance());
  | 	}
  | 
  | 	// Returns the current Execution Context to leave the current node with
  | 	public ExecutionContext CalculateCurrentExitContext(Token Token)
  | 	{
  | 		// We need to work our way up the tree to find the lowest fork that all
  | 		// paths are complete and use this to set our context.
  | 
  | 		ExecutionContext exitContext = null;
  | 		Token parentToken = Token.getParent();
  | 		ExecutionContext currentContext = new ExecutionContext(Token);
  | 		// 	See if we have a parent token
  | 		if ( parentToken != null )
  | 		{
  | 			log.debug("Parent Token = "+parentToken.toString());
  | 			// See if that parent has any active children
  | 			if (!parentToken.hasActiveChildren())
  | 			{
  | 				// NO:
  | 				// Recursively call ourselves to see if the parent of
  | 				// the parent has active children
  | 				log.debug("No Active Children - Making Recursive Call");
  | 				exitContext = CalculateCurrentExitContext(parentToken);
  | 			}else{
  | 				// YES:
  | 				// Our parent has active children then
  | 				// our current context is this token, not the parent
  | 				log.debug("Active Children: "+parentToken.getActiveChildren().toString());
  | 				log.debug("Children - Setting to currentContext");
  | 				exitContext = currentContext;
  | 			}
  | 		}else{
  | 			// If parentToken = null then we have no parent
  | 			exitContext = currentContext;
  | 		}
  | 		log.debug("Calculated Highest Possible execution context:"  + exitContext.toString());
  | 		return exitContext;
  | 	}
  | }
  | 

View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=3969801#3969801

Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=3969801



More information about the jboss-user mailing list