6 Replies Latest reply on Jan 8, 2015 1:56 AM by swiderski.maciej

    Restore processes instances from database.

    qball

      Hello, i succesfully connected jbpm to my database (PostgreSQL) and i store logs into it. I made it by:

      KieServices ks = KieServices.Factory.get();
      KieContainer kContainer = ks.getKieClasspathContainer();
      KieSession kSession = kContainer.newKieSession("WorkflowSession");
      EntityManagerFactory emf = new EnvironmentProducer().getEntityManagerFactory();
      AbstractAuditLogger auditLogger = AuditLoggerFactory.newJPAInstance(emf);
      kSession.addEventListener(auditLogger);
      

       

      I would like to restore all active processes after server falling. For example:

      1. Start scenario (start process)

      2. Server fall down (the process is in database register as active)

      3. After turn on server again have this process loaded to my new KieSession

       

      Please help me with this problem.

       

      Thanks

        • 1. Re: Restore processes instances from database.
          swiderski.maciej

          there is no need to restore process instances after server shutdown/restart. Whenever you need to work on given process instance (signal, complete task etc) session will load the process instance on demand.

          P.S.

          I would recommend to use runtime manager whenever working with processes, see docs.

           

          HTH

          • 2. Re: Restore processes instances from database.
            qball

            Thank you for answer.

             

            I still have some questions:

            When i create RuntimeEnvironment i have to put as parametr: ResourceFactory.newClassPathResource("BPMN2-ScriptTask.bpmn2").

                 I don't know what should be exactly in newClassPathResource().

               

            I would like to restore all processes after server restart and it i thought that Per Process Strategy will be good, but i still don't know how to use it.

             

            Best regards

            • 3. Re: Restore processes instances from database.
              swiderski.maciej

              you put all the assets (process and rules) into the environment that you want to use later on when executing your processes.

               

              Again, there is no need to manually restore process instances as they are fetched on demand when work is to be performed on them - like signal, complete task, get process instance.

               

              HTH

              • 4. Re: Re: Restore processes instances from database.
                qball
                public class WorkflowKnowlageBase
                {
                  private static RuntimeManager manager;
                
                
                  static
                  {
                  RuntimeEnvironmentBuilder builder = new RuntimeEnvironmentBuilder();
                  RuntimeEnvironment env = builder.get();
                  env.usePersistence();
                  manager = RuntimeManagerFactory.Factory.get()
                  .newPerProcessInstanceRuntimeManager(env);
                  }
                
                
                  public void createProcessInstance(String procName,
                  Map<String, Object> params, String scenarioName)
                  {
                  RuntimeEngine runtime = manager.getRuntimeEngine(EmptyContext.get());
                  KieSession kSession = runtime.getKieSession();
                  ProcessInstance pi = kSession.createProcessInstance(procName, params);
                  kSession.startProcessInstance(pi.getId());
                  manager.disposeRuntimeEngine(runtime);
                  }
                
                
                
                
                
                
                }}
                
                

                 

                Here is my class to make sessions and start processes and it doesn't work, what is wrong?

                I've checked and i can't put processes into environment.

                 

                Best regards

                • 5. Re: Restore processes instances from database.
                  c00823ue

                  In my application, human task can be restore automatically after server restart. However, there will have error saying that workitemhandler are missing. I can only fix this error by start the same process again than I can have the missing workitemhandler back. Do you have other solution? THX!     

                  • 6. Re: Re: Re: Restore processes instances from database.
                    swiderski.maciej

                    Here is the code that would be needed to create RuntimeManager properly:

                    EntityManagerFactory emf = Persistence.createEntityManagerFactory("org.jbpm.persistence.jpa");
                    RuntimeEnvironment environment = RuntimeEnvironmentBuilder.Factory.get()
                    .newDefaultBuilder()
                    .entityManagerFactory(emf)
                    .addAsset(ResourceFactory.newClassPathResource("BPMN2-ScriptTask.bpmn2"), ResourceType.BPMN2)
                    .addAsset(ResourceFactory.newClassPathResource("BPMN2-UserTask.bpmn2"), ResourceType.BPMN2)
                    .get();
                    
                    manager = RuntimeManagerFactory.Factory.get().newPerProcessInstanceRuntimeManager(environment); 
                    

                    with that you provide entity manager factory and processes that should be there. Without these two there is no way for the engine to know what to do.

                     

                    HTH