use of org.kie.api.runtime.process.WorkItemHandler in project drools by kiegroup.
the class DefaultWorkItemManager method internalAbortWorkItem.
public void internalAbortWorkItem(long id) {
WorkItemImpl workItem = (WorkItemImpl) workItems.get(new Long(id));
// work item may have been aborted
if (workItem != null) {
WorkItemHandler handler = this.workItemHandlers.get(workItem.getName());
if (handler != null) {
handler.abortWorkItem(workItem, this);
} else {
workItems.remove(workItem.getId());
throw new WorkItemHandlerNotFoundException("Could not find work item handler for " + workItem.getName(), workItem.getName());
}
workItems.remove(workItem.getId());
}
}
use of org.kie.api.runtime.process.WorkItemHandler in project drools by kiegroup.
the class InjectionHelper method wireWIHs.
private static void wireWIHs(BeanCreator beanCreator, BeanCreator fallbackBeanCreator, ClassLoader cl, List<WorkItemHandlerModel> wihModels, KieSession kSession) {
for (WorkItemHandlerModel wihModel : wihModels) {
WorkItemHandler wih;
try {
wih = beanCreator.createBean(cl, wihModel.getType(), wihModel.getQualifierModel());
} catch (Exception e) {
try {
wih = fallbackBeanCreator.createBean(cl, wihModel.getType(), wihModel.getQualifierModel());
} catch (Exception ex) {
throw new RuntimeException("Cannot instance WorkItemHandler " + wihModel.getType(), e);
}
}
kSession.getWorkItemManager().registerWorkItemHandler(wihModel.getName(), wih);
}
}
use of org.kie.api.runtime.process.WorkItemHandler in project drools by kiegroup.
the class RegisterWorkItemHandlerTest method testRegisterWorkItemHandlerWithStatelessSession.
@Test
public void testRegisterWorkItemHandlerWithStatelessSession() {
String str = "package org.kie.workitem.test \n" + "import " + DefaultWorkItemManager.class.getCanonicalName() + "\n" + "import " + WorkItem.class.getCanonicalName() + "\n" + "import " + WorkItemImpl.class.getCanonicalName() + "\n" + "rule r1 when \n" + "then \n" + " WorkItem wi = new WorkItemImpl(); \n" + " wi.setName( \"wihandler\" ); \n" + " DefaultWorkItemManager wim = ( DefaultWorkItemManager ) kcontext.getKieRuntime().getWorkItemManager(); \n" + " wim.internalExecuteWorkItem(wi); \n" + "end \n";
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newByteArrayResource(str.getBytes()), ResourceType.DRL);
if (kbuilder.hasErrors()) {
fail(kbuilder.getErrors().toString());
}
InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addPackages(kbuilder.getKnowledgePackages());
final boolean[] answer = new boolean[] { false };
StatelessKieSession ks = kbase.newStatelessKieSession();
ks.execute(CommandFactory.newRegisterWorkItemHandlerCommand(new WorkItemHandler() {
public void executeWorkItem(org.kie.api.runtime.process.WorkItem workItem, WorkItemManager manager) {
answer[0] = true;
}
public void abortWorkItem(org.kie.api.runtime.process.WorkItem workItem, WorkItemManager manager) {
// TODO Auto-generated method stub
}
}, "wihandler"));
assertTrue(answer[0]);
}
use of org.kie.api.runtime.process.WorkItemHandler in project drools by kiegroup.
the class JPAWorkItemManager method internalExecuteWorkItem.
public void internalExecuteWorkItem(WorkItem workItem) {
Environment env = this.kruntime.getEnvironment();
WorkItemInfo workItemInfo = new WorkItemInfo(workItem, env);
PersistenceContext context = getPersistenceContext();
workItemInfo = (WorkItemInfo) context.persist(workItemInfo);
((WorkItemImpl) workItem).setId(workItemInfo.getId());
if (this.workItems == null) {
this.workItems = new HashMap<Long, WorkItemInfo>();
}
workItems.put(workItem.getId(), workItemInfo);
WorkItemHandler handler = (WorkItemHandler) this.workItemHandlers.get(workItem.getName());
if (handler != null) {
handler.executeWorkItem(workItem, this);
} else {
throwWorkItemNotFoundException(workItem);
}
}
use of org.kie.api.runtime.process.WorkItemHandler in project drools by kiegroup.
the class JPAWorkItemManager method internalAbortWorkItem.
public void internalAbortWorkItem(long id) {
PersistenceContext context = getPersistenceContext();
WorkItemInfo workItemInfo = (WorkItemInfo) context.findWorkItem(id);
// work item may have been aborted
if (workItemInfo != null) {
WorkItemImpl workItem = (WorkItemImpl) internalGetWorkItem(workItemInfo);
WorkItemHandler handler = (WorkItemHandler) this.workItemHandlers.get(workItem.getName());
if (handler != null) {
handler.abortWorkItem(workItem, this);
} else {
if (workItems != null) {
workItems.remove(id);
throwWorkItemNotFoundException(workItem);
}
}
if (workItems != null) {
workItems.remove(id);
}
context.remove(workItemInfo);
}
}
Aggregations