1 2 Previous Next 20 Replies Latest reply on Aug 1, 2006 9:19 AM by maxip

    Persistance

    maxip

      Hi,

      i want to save a process instance (seems to work) and to get back a saved instance.

      But the fetched list is empty

      heres my code:

      whats wrong?

      in the log i can see that my process is started and saved...


      
      import java.util.List;
      
      import org.jbpm.JbpmConfiguration;
      import org.jbpm.JbpmContext;
      import org.jbpm.graph.def.ProcessDefinition;
      import org.jbpm.graph.exe.ProcessInstance;
      import org.jbpm.identity.Entity;
      import org.jbpm.identity.xml.IdentityXmlParser;
      
      public class TestClass {
      
       /**
       * @param args
       */
       public static void main(String[] args) {
       JbpmConfiguration.getInstance().createSchema();
       JbpmContext jbpmContext = JbpmConfiguration.getInstance().createJbpmContext();
      
       try {
       //jbpmContext.setActorId("1");
       deployProcessDefinition();
      
       ProcessInstance processInstance = jbpmContext.newProcessInstanceForUpdate( "SimpleDefinition1" );
       System.out.println( "definition: " + processInstance.getProcessDefinition().getName());
       processInstance.signal();
       jbpmContext.save(processInstance);
      
      
       List mylist = jbpmContext.getTaskList("1");
       processInstance = (ProcessInstance) mylist.get(0);
      
       System.out.println( "definition: " + processInstance.getProcessDefinition().getName());
       }
       finally {
       jbpmContext.close();
       }
       }
      
       public static void deployProcessDefinition() {
       ProcessDefinition processDefinition = ProcessDefinition
       .parseXmlString( "<process-definition name='SimpleDefinition1'>"
       + " <start-state name='start'>"
       + " <transition to='s' />"
       + " </start-state>"
       + " <state name='s'>"
       + " <transition to='end' />"
       + " </state>"
       + " <end-state name='end' />"
       + "</process-definition>" );
      
       JbpmContext jbpmContext = JbpmConfiguration.getInstance().createJbpmContext();
       try {
       jbpmContext.deployProcessDefinition( processDefinition );
       }
       finally {
       jbpmContext.close();
       }
       }
      }
      


        • 1. Re: Persistance
          maxip

           

          Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
          at java.util.ArrayList.RangeCheck(Unknown Source)
          at java.util.ArrayList.get(Unknown Source)
          at de.fhkl.bda.TestClass.main(TestClass.java:32)


          • 2. Re: Persistance
            olivier_debels

            You should use JbpmContext.loadProcessInstance() to load a process instance.

            What you're trying to do here doesn't make any sense.

            Take a look at chapter 3 in the document.

            • 3. Re: Persistance
              maxip

              Thank you

              But how do i fetch a list with all started / active process instances ?

              • 4. Re: Persistance
                olivier_debels

                Well,

                You could try something like:

                GraphSession.findAllProcessDefinitions() and loop through these ones and get all processInstances for each processDefinition (GraphSession.findProcessInstances(long)) or you could create a custom query to retrieve them in one shot.

                • 5. Re: Persistance
                  maxip

                  OK

                  but i think i have a probleme with the persistance.

                  i deployd withd graphsession.deploy(processDefinition);

                  but the definition cant be found


                  thats my code :

                  package de.fhkl.bda;
                  
                  import java.util.List;
                  import java.util.Map;
                  import java.util.Set;
                  
                  import org.jbpm.JbpmConfiguration;
                  import org.jbpm.JbpmContext;
                  import org.jbpm.db.GraphSession;
                  import org.jbpm.graph.def.ProcessDefinition;
                  import org.jbpm.graph.exe.ProcessInstance;
                  import org.jbpm.graph.exe.Token;
                  import org.jbpm.identity.Entity;
                  import org.jbpm.identity.xml.IdentityXmlParser;
                  
                  public class TestClass {
                   private static String actorID = "1";
                   /**
                   * @param args
                   */
                   public static void main(String[] args) {
                   JbpmConfiguration.getInstance().createSchema();
                   JbpmContext jbpmContext = JbpmConfiguration.getInstance().createJbpmContext();
                  
                   try {
                   GraphSession graphSession = jbpmContext.getGraphSession();
                   ProcessDefinition processDefinition = findProcessDefinitionsFromServer("SimpleDefinition1", graphSession);
                   System.out.println(processDefinition.getId());
                   }
                   finally {
                   jbpmContext.close();
                   }
                   }
                  
                   public static void deployProcessDefinition(GraphSession graphSession) {
                   ProcessDefinition processDefinition = ProcessDefinition
                   .parseXmlString( "<process-definition name='SimpleDefinition1'>"
                   + " <start-state name='start'>"
                   + " <transition to='s' />"
                   + " </start-state>"
                   + " <state name='s'>"
                   + " <transition to='t' />"
                   + " </state>"
                   + " <state name='t'>"
                   + " <transition to='end' />"
                   + " </state>"
                   + " <end-state name='end' />"
                   + "</process-definition>" );
                   graphSession.deployProcessDefinition(processDefinition);
                   }
                  
                   public static ProcessDefinition findProcessDefinitionsFromServer(String name, GraphSession graphSession) {
                   ProcessDefinition processDefinition =
                   graphSession.findLatestProcessDefinition(name);
                   if(processDefinition == null) System.out.println("No process definitions found for '" + name + "' !!!");
                   return processDefinition;
                   }
                  }
                  


                  • 6. Re: Persistance
                    maxip

                    it only works if i deploy the processdefinition at the same time:

                    JbpmConfiguration.getInstance().createSchema();
                     JbpmContext jbpmContext = JbpmConfiguration.getInstance().createJbpmContext();
                    
                     try {
                     GraphSession graphSession = jbpmContext.getGraphSession();
                    
                     deployProcessDefinition(graphSession); // ************
                    
                     findAllProcessDefinitions(graphSession);
                    
                    
                     ProcessDefinition processDefinition = findProcessDefinitionsFromServer("SimpleDefinition1", graphSession);
                     System.out.println(processDefinition.getId());
                     }
                     finally {
                     jbpmContext.close();
                     }
                     }


                    • 7. Re: Persistance
                      olivier_debels

                      The process definition is not persisted probably because you forget to close the JbpmContext when deploying. Closing the context will perform a commit on the database.

                      So in a realistic example you need to open a JbpmContext, deploy and close. Afterwards you create another JbpmContext to retrieve the process instances (and close this one).

                      • 8. Re: Persistance
                        maxip

                        I always create and close


                        so do i always create an new one ?

                        how can i reopen an existing jbpm context ?



                        btw. : thank you :)

                        • 9. Re: Persistance
                          olivier_debels

                          You can reuse JbpmContext by using JbpmContext.getCurrentJbpmContext(). This is stored in a thread local variable.

                          So if you open a context, only perform a deploy and then close the context the process definition is not saved?

                          • 10. Re: Persistance
                            maxip

                            hm

                            but how do i reuse a jbpm context with different classes ?

                            i want to write some classes as an example how to use jbpm for our univeristy.

                            so i want to deploy a process definition. close the program

                            then i want to start a programm which uses this context, too and starts a process instance

                            and there should be one more program that shows all open tasks

                            so actually i have to keep the programm running all the time ? it should be more like a server / client

                            i have the jbpm server running (starters kit) but it seems that eclipse only starts a "local" jbpm contaxt which is lost after my programm has terminated


                            thats the programm that works:

                            package de.fhkl.bda;
                            
                            import java.util.List;
                            import java.util.Map;
                            import java.util.Set;
                            
                            import org.jbpm.JbpmConfiguration;
                            import org.jbpm.JbpmContext;
                            import org.jbpm.db.GraphSession;
                            import org.jbpm.graph.def.ProcessDefinition;
                            import org.jbpm.graph.exe.ProcessInstance;
                            import org.jbpm.graph.exe.Token;
                            import org.jbpm.identity.Entity;
                            import org.jbpm.identity.xml.IdentityXmlParser;
                            
                            public class TestClass {
                             private static String actorID = "1";
                             /**
                             * @param args
                             */
                             public static void main(String[] args) {
                             JbpmConfiguration.getInstance().createSchema();
                             JbpmContext jbpmContext = JbpmConfiguration.getInstance().createJbpmContext();
                            
                             try {
                             GraphSession graphSession = jbpmContext.getGraphSession();
                            
                             deployProcessDefinition(graphSession);
                            
                             findAllProcessDefinitions(graphSession);
                            
                            
                             ProcessDefinition processDefinition = findProcessDefinitionsFromServer("SimpleDefinition1", graphSession);
                             System.out.println(processDefinition.getId());
                             }
                             finally {
                             jbpmContext.close();
                             }
                             }
                            
                             public static void deployProcessDefinition(GraphSession graphSession) {
                             ProcessDefinition processDefinition = ProcessDefinition
                             .parseXmlString( "<process-definition name='SimpleDefinition1'>"
                             + " <start-state name='start'>"
                             + " <transition to='s' />"
                             + " </start-state>"
                             + " <state name='s'>"
                             + " <transition to='t' />"
                             + " </state>"
                             + " <state name='t'>"
                             + " <transition to='end' />"
                             + " </state>"
                             + " <end-state name='end' />"
                             + "</process-definition>" );
                             graphSession.deployProcessDefinition(processDefinition);
                             graphSession.saveProcessDefinition(processDefinition);
                             }
                            
                             public static ProcessDefinition findProcessDefinitionsFromServer(String name, GraphSession graphSession) {
                             ProcessDefinition processDefinition =
                             graphSession.findLatestProcessDefinition(name);
                             if(processDefinition == null) System.out.println("No process definitions found for '" + name + "' !!!");
                             return processDefinition;
                             }
                            
                             public static void findAllProcessDefinitions(GraphSession graphSession) {
                             System.out.println(graphSession.findAllProcessDefinitions());
                             }
                            }
                            



                            But as i told above i want to re-use the same context in several different programs.

                            so how can i use the server to store it forever ?

                            now i guess why i cant find anything i deployed / started in earlier times

                            • 11. Re: Persistance
                              olivier_debels

                              In your case it should work if:

                              * you create and close a context when deploying
                              * you create and close a context when retrieving process instances
                              * you create and close a context when showing all open tasks


                              No need to reuse a context here.

                              • 12. Re: Persistance
                                maxip

                                Thank you for your help but it does not work :(

                                i createt a context deployed the process definition.


                                then i started another main class which created e new context and tried to get all process definitions.

                                but none were found.

                                How can i ensure that the server us used which is running ? i have the feeling its every time just a temporaily created jbpm server which stops when my main class ends.

                                • 13. Re: Persistance
                                  olivier_debels

                                  Strange,

                                  If you are using hsqldb, see 8.3 in documentation to check if process definitions are actually stored in the database. You can also try to deploy the definitions using the gpd designer tool.

                                  • 14. Re: Persistance
                                    maxip

                                    OK

                                    so i deployed a process definition by the designer (deployment was successfull)

                                    but i cant find any process definition

                                    public static void main(String[] args) {
                                    JbpmConfiguration.getInstance().createSchema();
                                    JbpmContext jbpmContext = JbpmConfiguration.getInstance().createJbpmContext();

                                    try {
                                    GraphSession graphSession = jbpmContext.getGraphSession();

                                    // deployProcessDefinition(graphSession);

                                    findAllProcessDefinitions(graphSession);


                                    //ProcessDefinition processDefinition = findProcessDefinitionsFromServer("SimpleDefinition1", graphSession);
                                    //System.out.println(processDefinition.getId());
                                    }
                                    finally {
                                    jbpmContext.close();
                                    }
                                    }

                                    public static void findAllProcessDefinitions(GraphSession graphSession) {
                                    System.out.println(graphSession.findAllProcessDefinitions());
                                    }


                                    1 2 Previous Next