[jboss-user] [jBPM] - Re: Problem with multiple sessions
Gareth Edwards
do-not-reply at jboss.com
Thu Apr 4 13:54:44 EDT 2013
Gareth Edwards [https://community.jboss.org/people/garethed] created the discussion
"Re: Problem with multiple sessions"
To view the discussion, visit: https://community.jboss.org/message/806545#806545
--------------------------------------------------------------
Hello again Maciej,
I have a method to start a new session:
public synchronized int startNewSession(WorkflowSettings wfs) throws APIException{
purgeDeadSessions();
KnowledgeBase kbase = packageService.getKnowledgeBase(wfs.getPackageName(), wfs.getPackageVersion());
StatefulKnowledgeSession ksession = null;
ksession = getSession(kbase);
log.debug("Starting new session:" + ksession.getId());
wfs.setSessionId(ksession.getId());
WorkflowSessionTask wst = new WorkflowSessionTask(workflowSessionService, ksession, wfs);
// Make a copy of the user to enable updates of workflowSession table
final User u = new SessionUser(wfs.getUser().getId());
wst.startWorkFlow(true, u);
this.workFlowSessionTasks.add(wst);
log.info("WorkflowSettings:" + wfs.toString());
return wst.getSessionId();
}
The WorkflowSessionTask is an inner class that I'm using to contain the session.
I add the instance of this to an ArrayList. When adding a new session I purge this list of any that are marked as completed.
public void startWorkFlow(boolean startProcess, final User user) {
// pwss is a spring service that performs CRUD on a database table using hibernate
this.completed = false;
JPAWorkingMemoryDbLogger logger = new JPAWorkingMemoryDbLogger(ksession);
String connectorName = "Hornet" + UUID.randomUUID().toString();
AsyncHornetQHTWorkItemHandler humanTaskHandler = new AsyncHornetQHTWorkItemHandler(new AsyncHornetQTaskClient(connectorName), ksession, OnErrorAction.LOG);
humanTaskHandler.setIpAddress(ipAddress);
humanTaskHandler.setOwningSessionOnly(true);
// The CMTDisposeCommand disposes the session synchronised with the transaction
final CMTDisposeCommand dc = new CMTDisposeCommand();
dc.setHumanTaskHandler(humanTaskHandler);
ksession.getWorkItemManager().registerWorkItemHandler("Human Task", humanTaskHandler);
WorkflowCurrentTaskHandler currentTaskHandler = new WorkflowCurrentTaskHandler();
currentTaskHandler.setUser(user);
currentTaskHandler.setWorkflowSessionService(pwss);
currentTaskHandler.setSessionId(ksession.getId());
dc.setCurrentTaskHandler(currentTaskHandler);
ksession.getWorkItemManager().registerWorkItemHandler("UpdateWorkflowCurrentTask",currentTaskHandler);
// The workflow event listener uses the afterNodeTriggered event to update a database table
WorkflowEventListener wel = new WorkflowEventListener();
wel.setPatientWorkflowSessionService(pwss);
wel.setSessionId(ksession.getId());
wel.setUser(user);
dc.setWorkflowEventListener(wel);
ksession.addEventListener(wel);
ksession.addEventListener(new DefaultProcessEventListener(){
@Override
public void afterProcessCompleted(ProcessCompletedEvent event){
log.info("~~~~~~~~~Workflow Session:" + ksession.getId() + " Completed~~~~~~~~~");
WorkflowSession pws = null;
try {
pws = pwss.get(ksession.getId());
pws.setActive(false);
pwss.update(pws, user);
} catch (APIException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
completed = true;
workflowSettings.setCompleted(true);
if (pws.getTId() != null){
String[] nextWorkflow = pwss.getNextWorkflow(pws.getTId(), pws.getWorkflowRef());
if (nextWorkflow != null){
WorkflowSettings settings = new WorkflowSettings();
settings.setActive(true);
settings.setCompleted(false);
settings.setPackageName(nextWorkflow[0]);
settings.setPackageVersion("LATEST");
settings.setWorkflowName(nextWorkflow[0] + "." + nextWorkflow[1]);
settings.setPId(pws.getPId());
settings.setTId(pws.getTId());
settings.setUser(user);
HashMap<String, Object> params = new HashMap<String, Object>();
params.put("username",user.getUsername());
params.put("pId", pws.getPId());
params.put("tId", pws.getTId().intValue());
params.put("packageName", settings.getPackageName());
params.put("packageVersion", settings.getPackageVersion());
settings.setWorkFlowData(params);
queueWorkflow(settings);
}
}
log.info("Disposing of " + event.getProcessInstance().getProcessName() + "!");
ksession.execute(dc);
}
});
if (startProcess)
ksession.startProcess(workflowSettings.getWorkflowName(),workflowSettings.getWorkFlowData());
}
Hopefully this helps.
--------------------------------------------------------------
Reply to this message by going to Community
[https://community.jboss.org/message/806545#806545]
Start a new discussion in jBPM at Community
[https://community.jboss.org/choose-container!input.jspa?contentType=1&containerType=14&container=2034]
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/jboss-user/attachments/20130404/075d9f33/attachment-0001.html
More information about the jboss-user
mailing list