how to add user and user task in Hello World Project of jBPM6 maven

271 views
Skip to first unread message

Shuo Chen

unread,
Mar 6, 2016, 8:26:50 AM3/6/16
to jBPM Usage

I'm new to jBPM and I am using jBPM-6.3.0-Final.

enter image description hereenter image description here

I want to add a new User task and assigned to a user named jack like this.

enter image description here

enter image description here

Then I added the code in the main class to run task 3.

package com.sample;

import java.util.List;

import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

import org.jbpm.test.JBPMHelper;
import org.kie.api.KieBase;
import org.kie.api.KieServices;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;
import org.kie.api.runtime.manager.RuntimeEngine;
import org.kie.api.runtime.manager.RuntimeEnvironmentBuilder;
import org.kie.api.runtime.manager.RuntimeManager;
import org.kie.api.runtime.manager.RuntimeManagerFactory;
import org.kie.api.task.TaskService;
import org.kie.api.task.model.TaskSummary;

public class ProcessMain {

    public static void main(String[] args) {
        KieServices ks = KieServices.Factory.get();
        KieContainer kContainer = ks.getKieClasspathContainer();
        KieBase kbase = kContainer.getKieBase("kbase");

        RuntimeManager manager = createRuntimeManager(kbase);
        RuntimeEngine engine = manager.getRuntimeEngine(null);
        KieSession ksession = engine.getKieSession();
        TaskService taskService = engine.getTaskService();

        ksession.startProcess("com.sample.bpmn.hello");

        // let john execute Task 1
        List<TaskSummary> list = taskService.getTasksAssignedAsPotentialOwner("john", "en-UK");
        TaskSummary task = list.get(0);
        System.out.println("John is executing task " + task.getName());
        taskService.start(task.getId(), "john");
        taskService.complete(task.getId(), "john", null);

        // let mary execute Task 2
        list = taskService.getTasksAssignedAsPotentialOwner("mary", "en-UK");
        task = list.get(0);
        System.out.println("Mary is executing task " + task.getName());
        taskService.start(task.getId(), "mary");
        taskService.complete(task.getId(), "mary", null);

        // let jack execute Task 3
        list = taskService.getTasksAssignedAsPotentialOwner("jack", "en-UK");
        task = list.get(0);
        System.out.println("Jack is executing task " + task.getName());
        taskService.start(task.getId(), "jack");
        taskService.complete(task.getId(), "jack", null);

        manager.disposeRuntimeEngine(engine);
        System.exit(0);
    }

    private static RuntimeManager createRuntimeManager(KieBase kbase) {
        JBPMHelper.startH2Server();
        JBPMHelper.setupDataSource();
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("org.jbpm.persistence.jpa");
        RuntimeEnvironmentBuilder builder = RuntimeEnvironmentBuilder.Factory.get().newDefaultBuilder()
                .entityManagerFactory(emf).knowledgeBase(kbase);
        return RuntimeManagerFactory.Factory.get().newSingletonRuntimeManager(builder.get(), "com.sample:example:1.0");
    }

}

How ever this code can just run task 1 and task 2. When it comes to task 3, it shows the following error:

John is executing task Task 1
Mary is executing task Task 2
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
    at java.util.ArrayList.rangeCheck(ArrayList.java:653)
    at java.util.ArrayList.get(ArrayList.java:429)
    at com.sample.ProcessMain.main(ProcessMain.java:50)

So how to add a user and how to add a user task correctly.

r...@legosoft.com.mx

unread,
Dec 1, 2017, 2:41:35 PM12/1/17
to jBPM Usage
You can do it in two  different ways:

(a) One is that you can add a register in the ORGANIZATIONALENTITY enentiy or doing programmatically as follows:

taskInternalService.addUser(TaskModelProvider.getFactory().newUser("someUser"));

taskInternalService.addGroup(TaskModelProvider.getFactory().newGroup("someGroup"));


(a) But I recommend you to use a UserGroupCallback instance when you create a new RunTimeManger. Here is how you can do it:

private static RuntimeManager createRuntimeManager(KieBase kbase) {

JBPMHelper.startH2Server();

JBPMHelper.setupDataSource();

EntityManagerFactory emf = Persistence.createEntityManagerFactory("org.jbpm.persistence.jpa");


Properties properties = new Properties();

properties.setProperty("someUser", "someGroup");

properties.setProperty("mary", "");

properties.setProperty("john", "");


UserGroupCallback userGroupCallback = new JBossUserGroupCallbackImpl(properties);

RuntimeEnvironmentBuilder builder = RuntimeEnvironmentBuilder.Factory.get()

   .newDefaultBuilder()                    .userGroupCallback(userGroupCallback).persistence(true)    .entityManagerFactory(emf)

   .knowledgeBase(kbase);

return RuntimeManagerFactory.Factory.get()

            .newSingletonRuntimeManager(bulder.get(), "com.sample:example:1.0");

}

Reply all
Reply to author
Forward
0 new messages