8 Replies Latest reply on Aug 13, 2009 3:17 PM by Ronald van Kuijk

    How to Generate ad-hoc Task, activities & process ??

    Faisal Basra Newbie

      I am developing with jBPM4 and need some help how can I generate task, activities or whole process on runtime or ad-hoc ????

      The doc is not completely helpful ??

      Thanks in advance...

        • 1. Re: How to Generate ad-hoc Task, activities & process ??
          Ronald van Kuijk Master

          what is 'ad-hoc' for you? Generating a process 'runtime' is possible like is done in many unit tests ins the source.

          Adding tasks runtime to the process instance I'm not sure about. In 3 this is possible, 4 I have no clue.

          • 2. Re: How to Generate ad-hoc Task, activities & process ??
            Roger Yan Newbie

            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@gmail.com">Roger Yan</a>
            public class ApplicationListener implements EventListener {
             // private GroupManager groupManager;
             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)
             // 1. creating an activity
             ActivityImpl newActivity = fork.getProcessDefinition().createActivity("æ��交副导师信æ�¯1");
             TaskActivity ta = new TaskActivity();
             // 2.creating an outgoing transition
             TransitionImpl outgoing = newActivity.createOutgoingTransition();
             // outgoing.setDestination((ActivityImpl) join);
             // 3.creating an execution
             ExecutionImpl newExec = execution.createExecution();
             TaskImpl task = (TaskImpl) taskService.newTask();
             HistoryEvent.fire(new TaskActivityStart(task), newExec);

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

            • 3. Re: How to Generate ad-hoc Task, activities & process ??
              Ronald van Kuijk Master

              good to know it kind works the same in 4. thanks

              • 4. Re: How to Generate ad-hoc Task, activities & process ??
                Faisal Basra Newbie

                Thanks very much rogerofyan, interesting to see the detailed code about to programaticaly creating task & activities.

                But this is placed in jBPM docs, ProcessFactory or ProcessBuilder what are they and how we can achieve this to simply 2 activities and 1 transition from A to B.

                ClientProcessDefinition processDefinition = ProcessFactory.build()
                .activity("a").initial().behaviour(new WaitState())
                .activity("b").behaviour(new WaitState())

                Actually, your code is bit complex, if you can tell the process diagram, will be very thankful to you.

                • 5. Re: How to Generate ad-hoc Task, activities & process ??
                  Torsten R Newbie


                  as far as I understand there is a difference in the two code examples.

                  Rogerofyan is changing a running execution by adding additional steps. The process definition currently used is still in place, right? Is there no impact of the mismatch between process definition and running execution?

                  faisalgeek, your code creates a new completely new definition and thus need to instanciate a new execution first. I think this is a different scenario because there is no active process instance changed. Anyways, is there a way to persist a definition created like this into the DB?I think JPDL is persisted as XML LOB not as objects...


                  • 6. Re: How to Generate ad-hoc Task, activities & process ??
                    Ronald van Kuijk Master

                    Yes, two different scenarios, but he both requested them.

                    In 4 the jpdl is not only persisted as lob, al implementing classes are instantiated and that (java) object graph is persisted and executed once a process instance is started.

                    If you take a look at the internals of the PVM, you can see this happening. So in the end, jpdl is parsed into objects and persisted, just like directly persisting objects.

                    • 7. Re: How to Generate ad-hoc Task, activities & process ??
                      Faisal Basra Newbie

                      But, i am not getting "ProcessFactory" class in jbpm4 lib, but its mentioned in jbpm4 docs ?

                      any clue ?

                      and yes, if any one can help creating new process + task by code, I will be thankful to you.

                      • 8. Re: How to Generate ad-hoc Task, activities & process ??
                        Ronald van Kuijk Master

                        Look in the sourcecode of the pvm and jpdl. Lots of examples on how to create processes from code.