11 Replies Latest reply on Sep 13, 2011 2:01 PM by francesco.pietrobelli

    Basic API Question

    diogosaad

      Hello Folks,

       

      What's the API to detect in which Node from the Workflow a processInstance curently is?

       

      Thanks,

      Diogo.

        • 1. Re: Basic API Question
          francesco.pietrobelli

          Hi Diogo,

          you can use the method

          Collection<NodeInstance> getNodeInstances()

           

          for example:

          import org.jbpm.workflow.instance.WorkflowProcessInstance;
          WorkflowProcessInstance processInstance=(WorkflowProcessInstance)ksession.startProcess("processId");
          Collection<NodeInstance> currentNodeInstances=processInstance.getNodeInstances();
          

           

          in collection currentNodeInstances you'll find all active nodes of processInstance!

           

          I have answered your question?

          Francesco

          • 2. Re: Basic API Question
            salaboy21

            a more basic question is why do you need that?

            We usually try to avoid doing that...

            Cheers

            • 3. Re: Basic API Question
              diogosaad

              For reporting, I would like to list all process instances that I have and which status it has, along with "where" (node) they are stopped.

               

              Do you suggest another way of doing this?

              Why do you usually avoid it?

               

              Thanks,

              Diogo.

              • 4. Re: Basic API Question
                francesco.pietrobelli

                If you enable persistence and BAM feature as explain in user guide (http://docs.jboss.org/jbpm/v5.1/userguide/ch07.html#d0e2836) you can query your database to retrieve all NodeInstanceLog that has an entry with type = 0 but not his related entry with type = 1.

                 

                Francesco.

                • 5. Re: Basic API Question
                  diogosaad

                  Nice!! I've read the ch07 but I could not find examples on how to retrieve the ProcessInstanceLog/NodeInstanceLog objects. Can you please provide examples?

                   

                  Thanks.

                  • 6. Re: Basic API Question
                    francesco.pietrobelli

                    You can use the EntityManagerFactory that you put in the Enviroment to create a SteatefulKnowledgeSession to get an EntityManager and create a query like

                    SELECT n FROM org.jbpm.process.audit.NodeInstanceLog WHERE ...

                    • 7. Re: Basic API Question
                      calca

                      Diogo,

                       

                      You can check how it is used in console:

                      https://github.com/droolsjbpm/jbpm/blob/master/jbpm-gwt/jbpm-gwt-graph/src/main/java/org/jbpm/integration/console/graph/GraphViewerPluginImpl.java

                       

                      Or for example this test:

                      https://github.com/calcacuervo/JBPM5-Samples/blob/master/human-tasks/src/test/java/com/test/HumanTaskTest.java

                       

                      Basically, you create a logger to save information in bam tables:

                      new JPAWorkingMemoryDbLogger(session);

                       

                      and then you can use a

                       

                      JPAProcessInstanceDbLog processLog = new JPAProcessInstanceDbLog(session.getEnvironment());

                       

                      to get the information with its methods:

                      findProcessInstances()

                      findProcessInstance(processInstanceId)

                      findNodesInstances(processInstances)

                      findVariableInstances(processInstances)

                      etc

                      Regards,

                       

                      Demian   

                      • 8. Re: Basic API Question
                        salaboy21

                        I usually recommend to use the history logs or a custom processEventListner to find out that information. Using the java apis will probably not give you enough flexibility to have a query mechanism for all the situations.

                        Cheers

                        • 9. Re: Basic API Question
                          diogosaad

                          I'm trying to follow the path indicated by Demian (save and retrieve information from BAM tables) but I simply can not make persistence work.

                          I get this exception where I try to excute the following junit test :

                           

                           

                                              SimpleNamingContextBuilder builder = SimpleNamingContextBuilder.emptyActivatedContextBuilder();

                           

                                                // Construct DataSource

                                              JdbcDataSource ds = new org.h2.jdbcx.JdbcDataSource();

                                              ds.setURL("jdbc:h2:jdbc/DataSource");

                                              ds.setUser("sa");

                                              ds.setPassword("sa");

                                               builder.bind("jdbc/DataSource", ds);

                           

                           

                                              Environment env = KnowledgeBaseFactory.newEnvironment();

                                              env.set( EnvironmentName.ENTITY_MANAGER_FACTORY, Persistence.createEntityManagerFactory("org.jbpm.persistence.jpa"));

                                              env.set( EnvironmentName.TRANSACTION_MANAGER, TransactionManagerServices.getTransactionManager() );

                                              session = JPAKnowledgeService.newStatefulKnowledgeSession(kbase, null, env);

                           

                           

                          :

                          Caused by: java.lang.reflect.InvocationTargetException

                                    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)

                                    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)

                                    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)

                                    at java.lang.reflect.Constructor.newInstance(Constructor.java:513)

                                    at org.drools.persistence.jpa.KnowledgeStoreServiceImpl.buildCommanService(KnowledgeStoreServiceImpl.java:116)

                                    ... 33 more

                          Caused by: java.lang.RuntimeException: Could not commit session or rollback

                                    at org.drools.persistence.SingleSessionCommandService.rollbackTransaction(SingleSessionCommandService.java:317)

                                    at org.drools.persistence.SingleSessionCommandService.<init>(SingleSessionCommandService.java:130)

                                    ... 38 more

                          Caused by: java.lang.RuntimeException: Unable to rollback transaction

                                    at org.drools.persistence.jta.JtaTransactionManager.rollback(JtaTransactionManager.java:189)

                                    at org.drools.persistence.SingleSessionCommandService.rollbackTransaction(SingleSessionCommandService.java:314)

                                    ... 39 more

                          Caused by: java.lang.NullPointerException

                                    at org.drools.persistence.jta.JtaTransactionManager.rollback(JtaTransactionManager.java:185)

                           

                           

                          Please find attached my persistence.xml file

                           

                           

                          What am I missing?

                           

                          Thanks,

                          Diogo.

                          • 10. Re: Basic API Question
                            calca

                            I am not sure which transaction-type is default, but could you try adding transaction-type="JTA" to your persistence.xml?

                            • 11. Re: Basic API Question
                              francesco.pietrobelli

                              I also haven't no idea!

                              maybe if you are in a Java Standard Edition environment you should set also the TransactionManager in environment and then handle the transaction boundary your self as explain in user guide (http://docs.jboss.org/jbpm/v5.1/userguide/ch07.html#d0e2820). Instead if you are in Java Enterprise Edition i haven't any suggestion...

                               

                              I have used bitronix 1.3.3 in SE!

                               

                              Of course your problem is related to transactions....