Tobias Wittur [
http://community.jboss.org/people/tob1as] created the discussion
"Re: How to access a processInstance within a work item"
To view the discussion, visit:
http://community.jboss.org/message/623389#623389
--------------------------------------------------------------
Thanks Tihomir.
Here some example code.
processTest.java
package TestingBPMN;
import java.util.HashMap;
import java.util.Map;
import org.drools.KnowledgeBase;
import org.drools.builder.KnowledgeBuilder;
import org.drools.builder.KnowledgeBuilderFactory;
import org.drools.builder.ResourceType;
import org.drools.io.ResourceFactory;
import org.drools.logger.KnowledgeRuntimeLogger;
import org.drools.logger.KnowledgeRuntimeLoggerFactory;
import org.drools.runtime.StatefulKnowledgeSession;
import org.drools.runtime.process.WorkflowProcessInstance;
import org.jbpm.process.workitem.wsht.WSHumanTaskHandler;
import org.jbpm.bpmn2.handler.SendTaskHandler;
import org.jbpm.bpmn2.handler.ServiceTaskHandler;
/**
* This is a sample file to launch a process.
*/
public class ProcessTest {
public static final void main(String[] args) {
try {
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newClassPathResource("TestingBPMN/TestingBPMN2.bpmn2"),
ResourceType.BPMN2);
KnowledgeBase kbase = kbuilder.newKnowledgeBase();
StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();
/*
* Register work item handlers
*
*/
ksession.getWorkItemManager().registerWorkItemHandler(
"Service Task",
//Pass session into work item handler
new CustomerServiceTaskHandler(ksession)
);
ksession.getWorkItemManager().registerWorkItemHandler(
"Send Task",
new CustomSendTaskHandler()
);
/*
* Create parameters
*/
HashMap<String, String> m = new HashMap<String, String>();
m.put("name", "Karsten");
m.put("request", "test");
/*
* Message based process start passing values
*/
ksession.signalEvent("Message-StartProcessMessage", m);
ksession.signalEvent("Message-StartProcessMessage", m);
ksession.signalEvent("Message-StartProcessMessage", m);
ksession.signalEvent("Message-StartProcessMessage", m);
ksession.signalEvent("Message-StartProcessMessage", m);
} catch (Throwable t) {
t.printStackTrace();
}
}
CustomerServiceTaskHandler.java
package TestingBPMN;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import org.drools.runtime.StatefulKnowledgeSession;
import org.drools.runtime.process.WorkItem;
import org.drools.runtime.process.WorkItemHandler;
import org.drools.runtime.process.WorkItemManager;
import org.drools.runtime.process.ProcessInstance;
/*
*
https://github.com/droolsjbpm/jbpm/blob/master/jbpm-bpmn2/src/main/java/o...
https://github.com/droolsjbpm/jbpm/blob/master/jbpm-bpmn2/src/main/java/o...
*
*/
public class CustomerServiceTaskHandler implements WorkItemHandler {
private StatefulKnowledgeSession ksession = null;
public CustomerServiceTaskHandler(StatefulKnowledgeSession ksession) {
this.ksession = ksession;
}
public void executeWorkItem(WorkItem workItem, WorkItemManager manager) {
//Print Process Instance Id
System.out.println(workItem.getProcessInstanceId());
//Get Current process Instance
ProcessInstance p =
this.ksession.getProcessInstance(workItem.getProcessInstanceId());
//Abort the current process if it has an id of 3
if(workItem.getProcessInstanceId()==3){
this.ksession.abortProcessInstance(workItem.getProcessInstanceId());
}
//if the current state is 3, the process has been aborted!
if(p.getState()==3){
System.out.println("aborted!");
}
System.out.println("");
}
public void abortWorkItem(WorkItem workItem, WorkItemManager manager) {}
--------------------------------------------------------------
Reply to this message by going to Community
[
http://community.jboss.org/message/623389#623389]
Start a new discussion in jBPM at Community
[
http://community.jboss.org/choose-container!input.jspa?contentType=1&...]