[jboss-user] [JBoss jBPM] - Re: token on tasks instance created at runtime

steve.j.hall@tektronix.com do-not-reply at jboss.com
Thu Aug 14 13:10:59 EDT 2008


Thanks for posting this question.  It helped me look at some alternatives in a test case I had been working on.  

The example shows two alternatives: 
1. Loding and updating a process at runtime to add a task to a TaskNode, persisting the process change to the database.  Later loading the process instance including the new task we added via our program.
2. Adding a task to a TaskNode at runtime, where the task is not defined in the process definition.  In the code below see: Task rtTask = new Task("RuntimeTask");

In this example node 'r' is a TaskNode.

I have checked the database and see that the task instances and their variables are persisted as one would hope.

private void deployProcessDefinition(){
		// load the process definition from a file
		ProcessDefinition processDefinition = ProcessDefinition.parseXmlResource("pm/processdefinition.xml");
		assertNotNull("Definition should not be null", processDefinition);
		
		// create a delegate and an event
		org.jbpm.instantiation.Delegation del = new org.jbpm.instantiation.Delegation(new com.sample.action.StartPklblTaskActionHandler());
		Action actn = new Action(del);
		Event evt = new Event(Event.EVENTTYPE_TASK_START);
		evt.addAction(actn);
		del.setClassName("com.sample.action.StartPklblTaskActionHandler");
		del.setConfigType("bean");
	
		// create a task and assign the event
		Task pklblTask = new Task("PackingLabel");
		pklblTask.setSignalling(true);	
		pklblTask.setBlocking(true);
		pklblTask.addEvent(evt);		
		
		// can set a breakpoint here to look at the tasks the process knows about.
		Map taskMap = processDefinition.getTaskMgmtDefinition().getTasks();
	
		
		
		JbpmContext jbpmContext = jbpmConfiguration.createJbpmContext();
		try{
			// Deploy and persist to db
			jbpmContext.deployProcessDefinition(processDefinition);
			jbpmContext.close();
			
			// find and load the process from the database then
			// add a task via the program execution and persist to db
			jbpmContext = jbpmConfiguration.createJbpmContext();
			GraphSession gs = jbpmContext.getGraphSession();
			ProcessDefinition pd = gs.findProcessDefinition("hello world", 1);
			// get the task node named r
			// add a task to it via the API
			TaskNode rNode = (TaskNode) pd.getNode("r");
			del.setProcessDefinition(pd);
			pklblTask.setProcessDefinition(pd);
			rNode.addTask(pklblTask);
			pklblTask.setTaskMgmtDefinition(pd.getTaskMgmtDefinition());
			jbpmContext.close();			
		} catch(Exception e){
			e.printStackTrace();
			fail(); 
		} 	
	}
	
	private void emulateUserOne(){
		JbpmContext jbpmContext = jbpmConfiguration.createJbpmContext();
		
		try{
			// load the process definition
		   ProcessDefinition pd = findProcessDefinition(jbpmContext, "hello world", 1);
		
			// create a process instance
			ProcessInstance pi = new ProcessInstance(pd);
			TaskMgmtInstance tmi = pi.getTaskMgmtInstance();
			pi.setStart(new Date());
			
			TaskMgmtSession tms = jbpmContext.getTaskMgmtSession();
			ContextInstance ci = pi.getContextInstance();
			ci.setVariable("lotId", "100009");
			
			Token rootToken = pi.getRootToken();
			ExecutionContext exc = new ExecutionContext(rootToken);
		
			System.out.println("currentNode: " + rootToken.getNode().getName());
			TaskNode rNode = (TaskNode) pd.getNode("r");
			Set rNodeTasks = rNode.getTasks();
			
			Task rtTask = new Task("RuntimeTask");
			rtTask.setSignalling(true);	
			rtTask.setBlocking(true);
			rNodeTasks.add(rtTask);
			
			rootToken.signal();
			System.out.println("currentNode: " + rootToken.getNode().getName());
			
			
						
			Node node = rootToken.getNode();
			if(node instanceof TaskNode){				
				Collection taskInstances = tmi.getTaskInstances();
				Iterator iter = taskInstances.iterator();
				while(iter.hasNext()){
					TaskInstance ti = (TaskInstance) iter.next();
					ti.start();
					ti.setVariable(ti.getName() + "_LongVar", new Long("9999"));
					ti.setVariable(ti.getName() + "_StringVar", "TaskInstanceStringValue");
					Thread.currentThread().sleep(2000);
					ti.end();
				}
			}
			
			System.out.println("currentNode: " + rootToken.getNode().getName());
			node = rootToken.getNode();
			
			if(node instanceof TaskNode){
				TaskNode tn = (TaskNode) node;
				// fetch all task instances even those that are finished.
				Collection taskInstances = tmi.getTaskInstances();
				Iterator iter = taskInstances.iterator();
				while(iter.hasNext()){
					TaskInstance ti = (TaskInstance) iter.next();
					if(ti.getStart() == null){
						ti.start();
					}
					Thread.currentThread().sleep(2000);
					
					if(ti.getEnd() == null){
						ti.end();
					}
				}
			}
		
			// persist 
			jbpmContext.save(pi);			
		} catch (Exception e){
			e.printStackTrace();
			fail();
		} finally {
			try{
				jbpmContext.close();
			} catch (Exception e){
				e.printStackTrace();
				fail();
			}
		}
	}

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

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



More information about the jboss-user mailing list