0 Replies Latest reply on Feb 17, 2011 4:43 AM by michelemauro

    How to configure jbpm5 to store and retrieve Knowledge Sessions from the database

    michelemauro

      I'm trying to setup a web application to work with jbpm5 processes, but I'm stuck at the jbpm5 configuration. I use spring, and I can write up (messing a bit with the various examples) a test case that can create and launch a process, with a configuration along these lines in the applicationContext.xml:

       

      <drools:ksession id="jpaSingleSessionCommandService" type="stateful" kbase="knowledgeBase">

           <drools:configuration>

                  <drools:work-item-handlers>

                      <drools:work-item-handler name="Human Task" ref="humanTaskHandler" />

                  </drools:work-item-handlers>

                 <drools:jpa-persistence load="1">

                      <drools:transaction-manager ref="transactionManager" />

                      <drools:entity-manager-factory ref="entityManagerFactory" />

                  </drools:jpa-persistence>

          </drools:configuration>

      </drools:ksession>

       

      However, this setup creates a new ksession each time the application is loaded. In my case, I need to load a specific ksession by id, work with the process instance contained in it, inspect it for current status and results, and then persist it back.

       

      I've tried setting up something more basic in Spring, that is building only the KnowledgeBase and then getting the session with:

       

      StatefulKnowledgeSession ks = JPAKnowledgeService.loadStatefulKnowledgeSession(id, kbase, null, env);

       

      but I keep getting strange NPEs and errors. In particular, I found that, while the documentation states (jBPM 5.0.0 manual, chapter 8) that you should build the environment to pass the JPAKnowledgeService in this way:

       

      Environment env = KnowledgeBaseFactory.newEnvironment();
      env.set( EnvironmentName.ENTITY_MANAGER_FACTORY, emf );
      env.set( EnvironmentName.TRANSACTION_MANAGER,
               TransactionManagerServices.getTransactionManager() );

       

      the SingleSessionCommandService, that is the final implementation of the KnowledgeSession that gets called, wants a different environment:

       

      https://github.com/droolsjbpm/droolsjbpm/blob/master/drools-persistence-jpa/src/main/java/org/drools/persistence/SingleSessionCommandService.java

       

      line 56:

      if ( env.get( EnvironmentName.ENTITY_MANAGER_FACTORY ) == null &&

                   env.get( EnvironmentName.PERSISTENCE_CONTEXT_MANAGER ) == null  ) {

                  throw new IllegalArgumentException( "Environment must have an EntityManagerFactory " +

                                                        "or a PersistenceContextManager instance" );

       

      Now I'm completely confused. How does this work?

       

      Michele