Drools:使用 JPAWorkingMemoryDbLogger 时,processInstanceId 始终为 1

Drools: When using JPAWorkingMemoryDbLogger, the processInstanceId is always 1

我们一直在为我们的呼叫中心在内存中执行 JBPM 决策树。这很好用,但我们真的希望能够在我们的 BusinessCentral 实例中呈现图表。这意味着我们必须添加 JPAWorkingMemoryDbLogger,以便它将内容记录到 drools 表中。我们没有使用 kie-server 来执行我们的 JBPM,而是在下面的代码中执行它。

我们发现每个流程实例 ID 都是 1,而我们通过 kie-server 执行的其他 JBPM 设法获得递增的 PID。

我们需要在 KieSession 的设置中更改什么,以便它增加流程实例 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;
    }
}

结果,每次:

startProcess:1

你可以试试 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);
}