8 Replies Latest reply: Jan 28, 2006 5:11 AM by Rainer Alfoeldi RSS

What exactly is ProcessInstance ?

Aaron Novice

All,

I'm trying to comprehend the methods used in JBpm. I'm building a helpdesk for my company using JBoss, and JBpm will really help me out.

My confusion is in the ProcessDefinition and ProcessInstance. In the tutorial for JBpm3.0 (Chapter 3.2 Database Example), a processDefinition is deployed with the name "Hello World".

Easy enough.

Where I fall short on, is how do you "attach" a java object instance to a specific instance in JBpm? The tutorial loads it as:

JbpmSession jbpmSession = jbpmSessionFactory.openJbpmSession();
jbpmSession.beginTransaction();
jbpmSession
 .getGraphSession()
 .saveProcessDefinition(processDefinition);
..... close the session and transaction ....

How does JBpm know that my, lets say, HolidayRequest#1234 is the object that is attached to that session?

It goes on, mimicking a seperate request:
ProcessDefinition processDefinition =
 jbpmSession
 .getGraphSession()
 .findLatestProcessDefinition("hello world");
ProcessInstance processInstance =
 new ProcessInstance(processDefinition);

 Token token = processInstance.getRootToken();

Okay, so I loaded up the process-definition "hello world" which is my workflow "map" so-to-speak, that all my HolidayRequests use. But, again, how does JBpm know that I'm talking about HolidayRequest#1234 when I "getRootToken()" ?

Obviously, HolidayRequest#1234 could be in the "pending" state, while HolidayRequest#567 could be in the "approved" state. How does JBpm know which object I'm talking about, when all I see is "findLatestProcessDefinition" and "getRootToken".

The HolidayRequest object is fictitious, but I could just as well be saying, TroubleTicket, or DocumentApproval objects. Any objects that would be defined to flow through the specific Process Definition.

I hope I'm clear on my confusion. I'm still scouring the docs, as I'm sure the answer is there somewhere.

Can anyone elaborate for me?

Thanks!
Aaron

  • 1. Re: What exactly is ProcessInstance ?
    Rainer Alfoeldi Novice

    Hi Aaron,

    jBPM doesn't know... unless you tell it.

    How about calling processInstance.getContextInstance().setVariable()?

    Greetings

  • 2. Re: What exactly is ProcessInstance ?
    Elmo Novice

    Hi Aaron,

    The process instance has a getId() method that returns the unique number of your process instance. You can use that to retrieve your process instances. If you want to retrieve using your defined names you can store it as a variable like Ralfoeldi has suggested. If you're ok with retrieving it using sql, you can check it out in the jbpm_variableinstance table, it contains the variable and the related process instance id.

    Regards,

    Elmo

  • 3. Re: What exactly is ProcessInstance ?
    Aaron Novice

    Thanks RAlfoeldi and enazareno! I appreciate you taking the time to help me understand!

    "enazareno" wrote:

    The process instance has a getId() method that returns the unique number of your process instance.


    So, keeping with this method, I can store the processInstanceID in my objects, to load at any time?

    public class Request {
    
     String name;
     Date dateCreated;
     Date dateRequested;
     long workflowID;
    
     public long getWorkflowID() {
     if (workflowID == null) {
     // obtain a JBpm session and processInstance
     workflowID = processInstance.getId();
     }
     return workflowID;
     }
    
     public void setWorkflowID(long id) {
     this.workflowID = id;
     }
    }
    


    Am I on the right track so far? Again, keeping to this effect, I can load the JBpm process using:

     processInstance = graphSession.loadProcessInstance(request.getWorkflowID());
    


    Is my understanding right?

    Thanks for your help, and your time! If I'm correct in my understanding for processInstances, my next learning challenge is task lists and assignments ((grin))

    Again, thanks.
    ~~Aaron

  • 4. Re: What exactly is ProcessInstance ?
    Rainer Alfoeldi Novice

    Hi Aaron,

    just a thought: isn't your processInstance your HolidayRequest?

    Things get a lot easier if the processInstance is the leading entity.

    There might be situations where the other way around is required i.e. an entity keeps track of it's processInstances (e.g. if more than one processInstance can be connected to an entity) but that isn't the usual situation.

    Greetings

  • 5. Re: What exactly is ProcessInstance ?
    Aaron Novice

     

    "RAlfoeldi" wrote:

    isn't your processInstance your HolidayRequest?


    Yes it is. I'm using EJB3 EntityBeans for my project.

    "RAlfoeldi" wrote:

    Things get a lot easier if the processInstance is the leading entity.


    What do you mean by "leading entity"? Maybe that's where I'm getting confused at.

    Thanks!!

  • 6. Re: What exactly is ProcessInstance ?
    Rainer Alfoeldi Novice

    Hi Aaron,

    another attempt:

    Your processInstance is a data object that traverses a process as defined in the processDefinition. It can basically handle any variables you might need. What I was suggesting is that there might not be any need for a seperate HolidayRequest object. Just use the processInstance. If the required data is to large for jBPM to efficiently handle just keep a key to an external data obejct, but the 'leading entity', the one that knows about state etc. remains the processInstance.

    The other way around would be something like: long running contract (years) with various processes that are executed over a certain period, maybe even concurrently. Then it would make sense to let the contract keep track of the processInstances doing something to it. (The implementation might be the other way around, but that isn't the point.) In this case you would maybe want to ask: who is working on this contract?

    Just my 2 cents. As always there are a thousand different ways to solve things.

    Greetings

  • 7. Re: What exactly is ProcessInstance ?
    Aaron Novice

    Rainer,

    I see now. Thanks for taking the extra time to explain to me.

    Basically, you are saying let the processInstance be my main object, storing the variables I need.

    That makes sense. It keeps things simple and centralized.

    Unfortunately, I think I'd rather do it the other way (i.e. Having my Entity Bean reference the processID), as my Entity Beans will be used by other outside processes (such as Crystal Reports, other external systems, etc).

    So, I will need a table for my object's data, and then reference JBpm to assess the current state, and it's workflow.

    Please correct me if I'm wrong.

    I think I'm ready to start planning my architecture.

    Thanks again!

  • 8. Re: What exactly is ProcessInstance ?
    Rainer Alfoeldi Novice

    Hi Aaron,

    you're on the right track.

    If the required data is to large for jBPM to efficiently handle just keep a key to an external data obejct, but the 'leading entity', the one that knows about state etc. remains the processInstance.


    Greetings