javadroolsjbpmrule-enginekie

Drools: When using JPAWorkingMemoryDbLogger, the processInstanceId is always 1


We've been executing a JBPM decision tree in memory for our call center. This works great, but we'd really like to be able to render diagrams in our BusinessCentral instance. This means we have to add JPAWorkingMemoryDbLogger so it logs stuff out to the drools tables. We're not using kie-server to execute our JBPM, but executing it in the follow code.

What we're finding is that every process instance id is 1, whereas other things JBPM we execute via kie-server manage to get an incremented PID.

What do we need to change in the setup of the KieSession so it it increments the process instance id?

@ApplicationScoped
@Transactional(TxType.MANDATORY)
public class JbpmService {
    @Inject
    private Logger log;
    @Inject
    @Config(defaultValue = "decision-tree.bpmn")
    private String modelName;
    @Inject
    @Config(defaultValue = "decision-tree.decision-tree")
    private String processName;
    @PersistenceUnit(unitName = "org.jbpm.persistence.jpa")
    private EntityManagerFactory emf;

    private KieHelper kieHelper;
    private KieBase kieBase;

    @PostConstruct
    public void postConstruct() {
        kieHelper = new KieHelper();
        kieBase = kieHelper.addResource(newClassPathResource(modelName)).build();
    }

    public WorkflowProcessInstance runJbpmModel(final Map<String, Object> parameters) {
        log.debug("runJbpmModel() parameters:{}", parameters);
        final KieSession kieSession = kieBase.newKieSession();
        kieSession.addEventListener(new JPAWorkingMemoryDbLogger(emf));
        final ProcessInstance startProcess = kieSession.startProcess(processName, parameters);
        log.info("startProcess:{}", startProcess.getId());
        return (WorkflowProcessInstance) startProcess;
    }
}

Result, every time:

startProcess:1

Solution

  • You could try like this:

    @Override
    public ProcessInstance startProcess(String processId) {
        if (session == null) {
            // load the process
            KieBase kbase = createKnowledgeBase();
            // create a new session
            Properties properties = new Properties();
            properties.put("drools.processInstanceManagerFactory", "org.jbpm.process.instance.impl.DefaultProcessInstanceManagerFactory");
            properties.put("drools.processSignalManagerFactory", "org.jbpm.process.instance.event.DefaultSignalManagerFactory");
            SessionConfiguration config = SessionConfiguration.newInstance(properties);
            session = kbase.newKieSession(config, createEnvironment(context));
            
            new JPAWorkingMemoryDbLogger(session);
            session.getWorkItemManager().registerWorkItemHandler("Human Task", new SystemOutWorkItemHandler());
        }
        return session.startProcess(processId);
    }