11 Replies Latest reply on Jun 27, 2006 11:00 AM by David Hosier

    Creating a new instance from an application

    Abdiel Jaramillo Newbie

      hi,

      i defined a process and deploy it to the jbpm local server then i made an application that creates a new instance and then assigns values to a couple of variables. i assign the start node to user ernie, but when i login to the web application as user ernie i have no new instances in the task list. Can sombody tell me what i'm doing wrong ?

      Thanks.

        • 1. Re: Creating a new instance from an application
          Ronald van Kuijk Master

          no, but it is a good habit in these kinds of circumstances to at least post the processdefinition and relevant parts of the code you created. Otherwise it would be shooting in the dark

          • 2. Re: Creating a new instance from an application
            Abdiel Jaramillo Newbie

            Thanks for the advise. Here is the java source code and process definition:

            SOURCE CODE

            public void createInstance() throws Exception
            {

            // Extract a process definition from the processdefinition.xml file.
            FileInputStream fis = new FileInputStream("processes/solicitudes/prestamo/processdefinition.xml");
            ProcessDefinition processDefinition = ProcessDefinition.parseXmlInputStream(fis);

            // Create an instance of the process definition.
            ProcessInstance instance = new ProcessInstance(processDefinition);
            ContextInstance contextInstance = (ContextInstance) instance.getInstance(ContextInstance.class);

            contextInstance.setVariable("Codigo Cliente", "1");
            contextInstance.setVariable("Nombre Cliente", "ABDIEL JARAMILLO OJEDIS");

            }

            PROCESS DEFINITION

            <?xml version="1.0" encoding="UTF-8"?>

            <process-definition
            xmlns="urn:jbpm.org:jpdl-3.1" name="Solicitud Prestamo">






            <start-state name="RevisarDatos">







            </start-state>
            <task-node name="RevisarCredito">








            </task-node>
            <end-state name="end1"></end-state>
            </process-definition>

            • 3. Re: Creating a new instance from an application
              Abdiel Jaramillo Newbie

              don't know what happened to the process definition it was fine in the editor. Here it is again:

              PROCESS DEFINITION

              <?xml version="1.0" encoding="UTF-8"?>

              <process-definition
              xmlns="urn:jbpm.org:jpdl-3.1" name="Solicitud Prestamo">






              <start-state name="RevisarDatos">







              </start-state>
              <task-node name="RevisarCredito">








              </task-node>
              <end-state name="end1"></end-state>
              </process-definition>

              • 4. Re: Creating a new instance from an application
                Abdiel Jaramillo Newbie

                i don't know why some lines are getting deleted from the process definition

                • 5. Re: Creating a new instance from an application
                  David Hosier Novice

                  Two things....

                  1. Use code tags when you want to post XML and other code. That's why lines of your process definition keep getting deleted from the post. Look right under the Subject text box when posting a reply, and you'll see the Code tag button.

                  2. I think you need to do something like the following to get a start state to actually be created. This code also handles the case when there is no task attached to the start state. I believe that simply adapted this from the jbpm webapp, and this is what I use in our application when starting a process.

                  ProcessInstance processInstance = context.newProcessInstance(processName);
                  if (processInstance.getProcessDefinition().getTaskMgmtDefinition().getStartTask() == null)
                  {
                   processInstance.signal();
                  }
                  else
                  {
                   TaskInstance startTask =
                   processInstance.getTaskMgmtInstance().createStartTaskInstance();
                   context.save(startTask);
                  }
                  


                  • 6. Re: Creating a new instance from an application
                    Abdiel Jaramillo Newbie

                    Thanks. I'm sorry, i'm new to JBPM. But how do i get the context variable before creating the ProcessInstance ?

                    Thanks.

                    • 7. Re: Creating a new instance from an application
                      David Hosier Novice

                      Oh, I'm sorry. That's actually unrelated to process instances. You really should take a look at the User Guide and the source code examples for tips on proper use of the API. I'll answer this question though. This is what you want to do....create the context like below and then put your code in a try-finally block so that the context is always closed before your method exits:

                      JbpmContext context = JbpmConfiguration.getInstance().createJbpmContext();
                      try
                      {
                       // do some stuff
                      }
                      finally
                      {
                       context.close();
                      }


                      I think that tip is even described in the javadocs. So if you are new to jBPM, look at the User Guide, look at the javadocs, and then look at the code examples that can be found in the jBPM distribution that you downloaded. That's what I had do, and I was able to figure most of it out.

                      • 8. Re: Creating a new instance from an application
                        Abdiel Jaramillo Newbie

                        i'm getting this error:

                        Exception in thread "main" java.lang.NoClassDefFoundError: org/hibernate/Session
                         at org.jbpm.persistence.db.DbPersistenceServiceFactory.openService(DbPersistenceServiceFactory.java:55)
                         at org.jbpm.svc.Services.getService(Services.java:136)
                         at org.jbpm.svc.Services.getPersistenceService(Services.java:175)
                         at org.jbpm.JbpmContext.getPersistenceService(JbpmContext.java:515)
                         at org.jbpm.JbpmContext.getGraphSession(JbpmContext.java:431)
                         at org.jbpm.JbpmContext.newProcessInstance(JbpmContext.java:266)
                         at solicitudes.SolicitudPrestamo.createInstance(SolicitudPrestamo.java:36)
                         at solicitudes.Tester.main(Tester.java:13)
                        


                        HERE IS THE PROCESS DEFINITION:

                        <?xml version="1.0" encoding="UTF-8"?>
                        
                        <process-definition
                         xmlns="urn:jbpm.org:jpdl-3.1" name="SolicitudPrestamo">
                         <swimlane name="RevisarDatos">
                         <assignment expression='user(bert)'></assignment>
                         </swimlane>
                         <swimlane name="RevisarCredito">
                         <assignment expression='user(ernie)'></assignment>
                         </swimlane>
                         <start-state name="RevisarDatos">
                         <task name="RevisarDatos" swimlane="RevisarDatos">
                         <controller>
                         <variable name="Codigo Cliente" access="read,write,required"></variable>
                         <variable name="Nombre Cliente" access="read,write,required"></variable>
                         </controller>
                         </task>
                         <transition name="to_VerificarCredito" to="RevisarCredito"></transition>
                         </start-state>
                         <task-node name="RevisarCredito">
                         <task name="RevisarCredito" swimlane="RevisarCredito">
                         <controller>
                         <variable name="Codigo Cliente" access="read"></variable>
                         <variable name="Nombre Cliente" access="read"></variable>
                         <variable name="Calificacion"></variable>
                         </controller>
                         </task>
                         <transition name="to_end" to="end1"></transition>
                         </task-node>
                         <end-state name="end1"></end-state>
                        </process-definition>
                        


                        AND JAVA SOURCE CODE:

                        JbpmContext jbpmContext = JbpmConfiguration.getInstance().createJbpmContext();
                        try
                        {
                        
                         ProcessInstance processInstance = jbpmContext.newProcessInstance ("SolicitudPrestamo");
                        
                         if (processInstance.getProcessDefinition().getTaskMgmtDefinition ().getStartTask() == null)
                         {
                         processInstance.signal();
                         }
                         else
                         {
                         TaskInstance startTask =
                         processInstance.getTaskMgmtInstance().createStartTaskInstance();
                         jbpmContext.save(startTask);
                         }
                        }
                        finally
                        {
                         jbpmContext.close();
                        }
                        


                        I looked over the javadocs and User Guide but there is no reference to this kind of situation.

                        Thanks.

                        • 9. Re: Creating a new instance from an application
                          David Hosier Novice

                          Looks to me like you don't have the hibernate jars deployed to the server. You should have at least hibernate3.jar and ehcache-1.1.jar deployed. These should be available in the jbpm distribution lib/hibernate directory.

                          • 10. Re: Creating a new instance from an application
                            Abdiel Jaramillo Newbie

                            I debugged the aplication and the problem seems to be in the newProcessInstance method. When it tries to create the process with the provided Process Definition Name it returns a null ProcessInstance, although the Name is the same in the process definition and the source code.

                            • 11. Re: Creating a new instance from an application
                              David Hosier Novice

                              I'm not sure why you felt the need to debug the application at this point, since the stack trace you posted tells you exactly what the problem is. It seems to me that you've missed some fundamental steps along the way here. Maybe you should check out the Getting Started Guide if you haven't already: http://wiki.jboss.org/wiki/Wiki.jsp?page=JbpmGettingStarted
                              And since you also seem to be missing things from your server deployment, you should check out this Deployment link: http://docs.jboss.org/jbpm/v3/userguide/deployment.html.

                              Another thing...are you running your application code inside JBoss or outside of JBoss? Maybe you already said and I just missed it.