Search in sources :

Example 1 with WorkItemHandler

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());
    }
}
Also used : WorkItemHandler(org.kie.api.runtime.process.WorkItemHandler) AtomicLong(java.util.concurrent.atomic.AtomicLong) WorkItemHandlerNotFoundException(org.drools.core.WorkItemHandlerNotFoundException)

Example 2 with WorkItemHandler

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);
    }
}
Also used : WorkItemHandler(org.kie.api.runtime.process.WorkItemHandler) WorkItemHandlerModel(org.kie.api.builder.model.WorkItemHandlerModel)

Example 3 with WorkItemHandler

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]);
}
Also used : WorkItemHandler(org.kie.api.runtime.process.WorkItemHandler) DefaultWorkItemManager(org.drools.core.process.instance.impl.DefaultWorkItemManager) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) WorkItemImpl(org.drools.core.process.instance.impl.WorkItemImpl) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) WorkItem(org.drools.core.process.instance.WorkItem) WorkItemManager(org.kie.api.runtime.process.WorkItemManager) DefaultWorkItemManager(org.drools.core.process.instance.impl.DefaultWorkItemManager) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Example 4 with WorkItemHandler

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);
    }
}
Also used : WorkItemHandler(org.kie.api.runtime.process.WorkItemHandler) WorkItemImpl(org.drools.core.process.instance.impl.WorkItemImpl) Environment(org.kie.api.runtime.Environment) PersistenceContext(org.drools.persistence.api.PersistenceContext) WorkItemInfo(org.drools.persistence.info.WorkItemInfo)

Example 5 with WorkItemHandler

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);
    }
}
Also used : WorkItemHandler(org.kie.api.runtime.process.WorkItemHandler) WorkItemImpl(org.drools.core.process.instance.impl.WorkItemImpl) PersistenceContext(org.drools.persistence.api.PersistenceContext) WorkItemInfo(org.drools.persistence.info.WorkItemInfo)

Aggregations

WorkItemHandler (org.kie.api.runtime.process.WorkItemHandler)7 WorkItemImpl (org.drools.core.process.instance.impl.WorkItemImpl)3 WorkItemHandlerNotFoundException (org.drools.core.WorkItemHandlerNotFoundException)2 PersistenceContext (org.drools.persistence.api.PersistenceContext)2 WorkItemInfo (org.drools.persistence.info.WorkItemInfo)2 Test (org.junit.Test)2 WorkItemManager (org.kie.api.runtime.process.WorkItemManager)2 Properties (java.util.Properties)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 SessionConfiguration (org.drools.core.SessionConfiguration)1 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)1 WorkItem (org.drools.core.process.instance.WorkItem)1 DefaultWorkItemManager (org.drools.core.process.instance.impl.DefaultWorkItemManager)1 KieBase (org.kie.api.KieBase)1 WorkItemHandlerModel (org.kie.api.builder.model.WorkItemHandlerModel)1 Environment (org.kie.api.runtime.Environment)1 KieSession (org.kie.api.runtime.KieSession)1 KieSessionConfiguration (org.kie.api.runtime.KieSessionConfiguration)1 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)1 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)1