[jboss-user] [JBoss jBPM] - Re: How to Generate ad-hoc Task, activities & process ??

rogerofyan do-not-reply at jboss.com
Wed Aug 12 09:34:55 EDT 2009


Sure, this is definitely ok for jBPM4.
faisalgeek, I suppose you means generating task, activities & process at run time, not only at the process definition(jpdl), don't u?

I use to have similar requirement, and I found few documents about the internal jBPM api. So I had to read the source code of jBPM4. 

I can share my code of generating task & activity at runtime. 

  | import java.util.List;
  | 
  | import org.jbpm.api.Execution;
  | import org.jbpm.api.ExecutionService;
  | import org.jbpm.api.TaskService;
  | import org.jbpm.api.listener.EventListener;
  | import org.jbpm.api.listener.EventListenerExecution;
  | import org.jbpm.jpdl.internal.activity.TaskActivity;
  | import org.jbpm.pvm.internal.env.Environment;
  | import org.jbpm.pvm.internal.env.SpringContext;
  | import org.jbpm.pvm.internal.history.HistoryEvent;
  | import org.jbpm.pvm.internal.history.events.TaskActivityStart;
  | import org.jbpm.pvm.internal.model.Activity;
  | import org.jbpm.pvm.internal.model.ActivityImpl;
  | import org.jbpm.pvm.internal.model.ExecutionImpl;
  | import org.jbpm.pvm.internal.model.Transition;
  | import org.jbpm.pvm.internal.model.TransitionImpl;
  | import org.jbpm.pvm.internal.model.ExecutionImpl.Propagation;
  | import org.jbpm.pvm.internal.session.DbSession;
  | import org.jbpm.pvm.internal.task.TaskImpl;
  | import org.jbpm.pvm.internal.util.Clock;
  | 
  | /**
  |  * 自动为各办学学院创建一个任务,为学习中心创建两个任务。
  |  * 
  |  * @author <a href="mailto:rogerofyan at gmail.com">Roger Yan</a>
  |  * 
  |  */
  | public class ApplicationListener implements EventListener {
  | 	// private GroupManager groupManager;
  | 	@Override
  | 	public void notify(EventListenerExecution execution) throws Exception {
  | 		ExecutionImpl exe = (ExecutionImpl) execution;
  | 		ActivityImpl fork = exe.getActivity();
  |                 //creating two tasks and associated activities
  | 		placeViceMentorTask(exe, "center1");
  | 		placeViceMentorTask(exe, "center2");
  | 	}
  | 
  | 	private void placeViceMentorTask(ExecutionImpl execution, String assignee) {
  | 		Environment env = Environment.getCurrent();
  | 		TaskService taskService = env.get(TaskService.class);
  | 		DbSession dbSession = env.get(DbSession.class);
  | 
  | 		// 获取当前执行的结点相åÂ
³ä¿¡æ¯
  | 		ActivityImpl fork = execution.getActivity();
  | 		List<Transition> trans = fork.getOutgoingTransitions();
  | 		TransitionImpl transition = (TransitionImpl) trans.get(0);
  | 		ActivityImpl join = (ActivityImpl) transition.getDestination().getOutgoingTransitions().get(0)
  | 				.getDestination();
  | 
  | 		// 1. creating an activity
  | 		ActivityImpl newActivity = fork.getProcessDefinition().createActivity("提交副导师信息1");
  | 		TaskActivity ta = new TaskActivity();
  | 		newActivity.setBehaviour(ta);
  | 
  | 		// 2.creating an outgoing transition
  | 		TransitionImpl outgoing = newActivity.createOutgoingTransition();
  | 		// outgoing.setDestination((ActivityImpl) join);
  | 		outgoing.setName("至教学服务中心审核");
  | 		join.addIncomingTransition(outgoing);
  | 
  | 		// 3.creating an execution
  | 		ExecutionImpl newExec = execution.createExecution();
  | 		newExec.setActivity(newActivity);
  | 		newExec.setState(Execution.STATE_ACTIVE_CONCURRENT);
  | 		newExec.setName("viceMentorExecution");
  | 		newExec.setTransition(outgoing);
  | 		newExec.setPropagation(Propagation.UNSPECIFIED);
  | 		newExec.setHistoryActivityStart(Clock.getCurrentTime());
  | 		dbSession.save(newExec);
  | 
  | 		TaskImpl task = (TaskImpl) taskService.newTask();
  | 		task.setAssignee(assignee);
  | 		task.setName("提交副导师资料1");
  | 		task.setExecution(newExec);
  | 		task.setSignalling(true);
  | 		taskService.saveTask(task);
  | 
  | 		HistoryEvent.fire(new TaskActivityStart(task), newExec);
  | 		newExec.waitForSignal();
  | 	}
  | }
  | 

The code looks ugly, but it just works for me.
Take it as a reference.

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

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




More information about the jboss-user mailing list