6 Replies Latest reply: Feb 2, 2012 10:39 AM by nt2005 RSS

Store variables in DB?

nt2005 Newbie

Hey Guys,

 

The 'problem' I have is a bit difficultly to describe, but I will give my best:

 

I use jBPM 5.2 in a Web-Application at a jBoss 6 AS with a PostgreSQL (just for info).

 

I start a process in a Seam bean like this:

 

public void startProcess() {

        EntityManagerFactory emf = entityManager.getEntityManagerFactory();
        TransactionManagerService tms = new TransactionManagerService();

        Environment env = KnowledgeBaseFactory.newEnvironment();
        env.set(EnvironmentName.ENTITY_MANAGER_FACTORY, emf);
        env.set(EnvironmentName.TRANSACTION_MANAGER, tms.getTransactionManager());

        Properties properties = new Properties();
        properties.put("drools.processInstanceManagerFactory","org.jbpm.persistence.processinstance.JPAProcessInstanceManagerFactory");
        properties.put("drools.processSignalManagerFactory","org.jbpm.persistence.processinstance.JPASignalManagerFactory");
        KnowledgeSessionConfiguration config = KnowledgeBaseFactory.newKnowledgeSessionConfiguration(properties);

        // load up the knowledge base
        KnowledgeBase kbase = readKnowledgeBase(processid);

        StatefulKnowledgeSession ksession = JPAKnowledgeService.newStatefulKnowledgeSession(kbase, config, env);

        JPAWorkingMemoryDbLogger logger = new JPAWorkingMemoryDbLogger(ksession);

        ksession.getWorkItemManager().registerWorkItemHandler(LoadDataWorkItem.NAME, new LoadDataWorkItem());
        ksession.getWorkItemManager().registerWorkItemHandler(TextOutputWorkItem.NAME, new TextOutputWorkItem());
        ksession.getWorkItemManager().registerWorkItemHandler(ImageOutputWorkItem.NAME, new ImageOutputWorkItem());

        String id = "blackdata";
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("id", id);

        process = ksession.startProcess("de.toni.new", params);

        logger.dispose();
    }

 

My jBPM-Process only contains my own WorkItems. The process works great and all.

 

In my process I have some process variables like:

 

<property id="image" itemSubjectRef="_imageItem"/>
<property id="text" itemSubjectRef="_textItem"/>

 

That I read and edit in my WorkItems:

 

String text = (String) workItem.getParameter("text");

Map<String, Object> results = new HashMap<String, Object>();
results.put("text", text);
manager.completeWorkItem(workItem.getId(), results);

 

The problem is, that these variables are in the process and not in the database. I thought, the persistence and serializable of all would save these in my db.

 

So my question to you:

How can I get these variables out of the process in a db?

I hope you understand what I mean, if not, just ask.

  • 1. Re: Store variables in DB?
    Esteban Aliverti Master

    Without further information, my guess is: If you are only using Syncrhonous Tasks, like the WorkItemHandler you shown us, then the process runtime information (including its variables) are never persisted. JBPM5 only stores the information needed to continue with the execution of a process. In your case (again, I'm guessing here since I don't know your complete process) the process runs in one shot: when the execution returns from ksession.startProcess("de.toni.new", params); the process is already finished.

    Just for testing purposes try to configure an asynchronous WorkItemHandler to one of your Tasks to see if serialization happens.

     

    Best Regards,

  • 2. Re: Store variables in DB?
    nt2005 Newbie

    Correct. When I execute ksession.startProcess("de.toni.new", params); The process is really fast finish. But it is only a example task.

    The correct task should also contains inputs of values/options. (<- Only possible with human tasks?)

     

    Please can you explain, what a asynchronous WorkItemHandler is and maybe do you have a little example?

     

    Thank you in advance!

  • 3. Re: Store variables in DB?
    Esteban Aliverti Master

    Here you can find a good explanation abour Asynchronous WorkItemHandlers and wait-states: https://community.jboss.org/message/600431#600431

     

    Best Regards,

  • 4. Re: Store variables in DB?
    nt2005 Newbie

    Thank you very much. Know I got it (0.01 % ) and can store all necessary variables.

     

    Okay one more question:

    There are StatefulKnowledgeSession ids and also ProcessInstance ids.

    It seems it is not necessary if I use the Session which starts a new process or an newer Session with an older process.

     

    Please can you explain when we should use a new Session or can use the old session? I don´t not know really what it does...

  • 5. Re: Store variables in DB?
    Esteban Aliverti Master

    Are you asking if in order to restore a process instance you have to use the same session used to start it? The answer is yes. Even if it would be possible to use a fresh session, it is always recomended to use the original one.

     

    Best Regards,

  • 6. Re: Store variables in DB?
    nt2005 Newbie

    Okay, thank you very much.

     

    One more questions:

    I have got many users who can start own processes. I would give each user one session for all his processes. How long exist these session? Or should I seperate different processes in different sessions?