Search in sources :

Example 1 with AsyncGenericWorkItemHandler

use of com.salaboy.jbpm5.dev.guide.workitems.AsyncGenericWorkItemHandler in project jBPM5-Developer-Guide by Salaboy.

the class AsyncWorkItemDontWaitForCompletionTest method executorCheckInTestFinishes.

/**
 * Test executing a process with a single Task using the Executor Service
 * component to interact with a (mocked) external service. The process
 * is configured to tell the work item handler being used (AsyncGenericWorkItemHandler)
 * to not wait until the external system comes back to complete the Task.
 * The result will be a process that will be completed before the external
 * system is even invoked.
 * @throws InterruptedException
 */
@Test
public void executorCheckInTestFinishes() throws InterruptedException {
    HashMap<String, Object> input = new HashMap<String, Object>();
    String patientName = "John Doe";
    input.put("bedrequest_patientname", patientName);
    // Registers an instance of AsyncGenericWorkItemHandler as a handler for
    // all the 'Async Work' tasks in the processes.
    AsyncGenericWorkItemHandler asyncHandler = new AsyncGenericWorkItemHandler(executor, session.getId());
    session.getWorkItemManager().registerWorkItemHandler("Async Work", asyncHandler);
    WorkflowProcessInstance pI = (WorkflowProcessInstance) session.startProcess("PatientCheckIn", input);
    // Since we are not waiting for the external system to respond before
    // completing the workiten, the process is executed in just 1 step.
    // At this point the process is completed.
    assertEquals(ProcessInstance.STATE_COMPLETED, pI.getState());
    // At this point, the command shouldn't be executed yet.
    assertEquals(0, CheckInCommand.getCheckInCount());
    Thread.sleep(1000);
    // After 1 second, the command is not yet executed.
    assertEquals(0, CheckInCommand.getCheckInCount());
    Thread.sleep(executor.getInterval() * 1000);
    // After a reasonable time, the command must be executed.
    assertEquals(1, CheckInCommand.getCheckInCount());
}
Also used : AsyncGenericWorkItemHandler(com.salaboy.jbpm5.dev.guide.workitems.AsyncGenericWorkItemHandler) HashMap(java.util.HashMap) WorkflowProcessInstance(org.drools.runtime.process.WorkflowProcessInstance) Test(org.junit.Test)

Example 2 with AsyncGenericWorkItemHandler

use of com.salaboy.jbpm5.dev.guide.workitems.AsyncGenericWorkItemHandler in project jBPM5-Developer-Guide by Salaboy.

the class SlowWebServicesInteractionsTest method testSlowWebServicesWait.

/**
 * Invokes 3 web services that take some time to be executed. The process
 * will wait for each web service to finish before continuing to the next
 * task. The process -> web service interaction is performed using the
 * Execution Service component. The Command being used is {@link CXFWebServiceCommand}
 * and the web service implementation is {@link SlowServiceImpl}.
 * @throws InterruptedException
 */
@Test
public void testSlowWebServicesWait() throws InterruptedException {
    initializeSession("three-systems-interactions-wait.bpmn");
    SessionStoreUtil.sessionCache.put("sessionId=" + session.getId(), session);
    HashMap<String, Object> input = new HashMap<String, Object>();
    String patientName = "John Doe";
    input.put("bedrequest_patientname", patientName);
    AsyncGenericWorkItemHandler webServiceHandler = new AsyncGenericWorkItemHandler(executor, session.getId());
    session.getWorkItemManager().registerWorkItemHandler("Slow Web Service", webServiceHandler);
    WorkflowProcessInstance pI = (WorkflowProcessInstance) session.startProcess("ThreeSystemsInteractions", input);
    // No web service was invoked yet, so the process remains ACTIVE
    List<RequestInfo> resultList = executor.getExecutedRequests();
    assertEquals(0, resultList.size());
    assertEquals(ProcessInstance.STATE_ACTIVE, pI.getState());
    Thread.sleep(35000);
    // After 35 seconds we could see that the web services were invoked
    // correctly.
    resultList = executor.getExecutedRequests();
    assertEquals(3, resultList.size());
    assertEquals(ProcessInstance.STATE_COMPLETED, pI.getState());
    session.dispose();
}
Also used : AsyncGenericWorkItemHandler(com.salaboy.jbpm5.dev.guide.workitems.AsyncGenericWorkItemHandler) HashMap(java.util.HashMap) RequestInfo(org.jbpm.executor.entities.RequestInfo) WorkflowProcessInstance(org.drools.runtime.process.WorkflowProcessInstance) Test(org.junit.Test)

Example 3 with AsyncGenericWorkItemHandler

use of com.salaboy.jbpm5.dev.guide.workitems.AsyncGenericWorkItemHandler in project jBPM5-Developer-Guide by Salaboy.

the class AsyncWorkItemWaitForCompletionTest method executorCheckInTestFinishesWithoutHandler.

/**
 * AsyncWorkItemWaitForCompletionTest: Test executing a process with a
 * single Task using the Executor Service component to interact with a (mocked)
 * external service. The process is configured to tell the work item handler being
 * used (AsyncGenericWorkItemHandler) to wait until the external system comes
 * back before completing the Task. The result will be a process that will is not
 * going to be completed until the external system comes back with a response.
 * @throws InterruptedException
 */
@Test
public void executorCheckInTestFinishesWithoutHandler() throws InterruptedException {
    HashMap<String, Object> input = new HashMap<String, Object>();
    String patientName = "John Doe";
    input.put("bedrequest_patientname", patientName);
    // Registers an instance of AsyncGenericWorkItemHandler as a handler for
    // all the 'Async Work' tasks in the processes.
    AsyncGenericWorkItemHandler asyncHandler = new AsyncGenericWorkItemHandler(executor, session.getId());
    session.getWorkItemManager().registerWorkItemHandler("Async Work", asyncHandler);
    WorkflowProcessInstance pI = (WorkflowProcessInstance) session.startProcess("PatientCheckIn", input);
    // No command was executed yet.
    assertEquals(0, CheckInCommand.getCheckInCount());
    // Since we are waiting for the execution of the external system to be done,
    // the process is still ACTIVE.
    assertEquals(ProcessInstance.STATE_ACTIVE, pI.getState());
    Thread.sleep(1000);
    // After 1 second, the command is not yet executed meaning that the process
    // is still ACTIVE.
    assertEquals(0, CheckInCommand.getCheckInCount());
    assertEquals(ProcessInstance.STATE_ACTIVE, pI.getState());
    Thread.sleep(executor.getInterval() * 1000);
    // After a reasonable time, the command must be executed. This should
    // have caused the execution of the callback and hence the completion
    // of the work item and thus the process.
    assertEquals(1, CheckInCommand.getCheckInCount());
    assertEquals(ProcessInstance.STATE_COMPLETED, pI.getState());
}
Also used : AsyncGenericWorkItemHandler(com.salaboy.jbpm5.dev.guide.workitems.AsyncGenericWorkItemHandler) HashMap(java.util.HashMap) WorkflowProcessInstance(org.drools.runtime.process.WorkflowProcessInstance) Test(org.junit.Test)

Example 4 with AsyncGenericWorkItemHandler

use of com.salaboy.jbpm5.dev.guide.workitems.AsyncGenericWorkItemHandler in project jBPM5-Developer-Guide by Salaboy.

the class HospitalInsuranceProcessExecutorTest method initializeSession.

private void initializeSession() {
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(new ClassPathResource("InsuranceProcessV2.bpmn"), ResourceType.BPMN2);
    if (kbuilder.hasErrors()) {
        KnowledgeBuilderErrors errors = kbuilder.getErrors();
        for (KnowledgeBuilderError error : errors) {
            System.out.println(">>> Error:" + error.getMessage());
        }
        throw new IllegalStateException(">>> Knowledge couldn't be parsed! ");
    }
    KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
    session = kbase.newStatefulKnowledgeSession();
    KnowledgeRuntimeLoggerFactory.newConsoleLogger(session);
    session.addEventListener(new DefaultAgendaEventListener() {

        @Override
        public void afterRuleFlowGroupActivated(org.drools.event.rule.RuleFlowGroupActivatedEvent event) {
            session.fireAllRules();
        }
    });
    // Registers the same generic handler for all the tasks. The handler will
    // schedule commands into the Executor Service. Each task in the process
    // is configured to use a different command.
    AsyncGenericWorkItemHandler genericHandler = new AsyncGenericWorkItemHandler(executor, session.getId());
    session.getWorkItemManager().registerWorkItemHandler("Gather Patient Data", genericHandler);
    session.getWorkItemManager().registerWorkItemHandler("Insurance Service", genericHandler);
    session.getWorkItemManager().registerWorkItemHandler("External Insurance Company Service", genericHandler);
    session.getWorkItemManager().registerWorkItemHandler("Rates Service", genericHandler);
    session.getWorkItemManager().registerWorkItemHandler("Invoice Service", genericHandler);
}
Also used : KnowledgeBuilderError(org.drools.builder.KnowledgeBuilderError) KnowledgeBuilder(org.drools.builder.KnowledgeBuilder) AsyncGenericWorkItemHandler(com.salaboy.jbpm5.dev.guide.workitems.AsyncGenericWorkItemHandler) KnowledgeBase(org.drools.KnowledgeBase) KnowledgeBuilderErrors(org.drools.builder.KnowledgeBuilderErrors) DefaultAgendaEventListener(org.drools.event.rule.DefaultAgendaEventListener) ClassPathResource(org.drools.io.impl.ClassPathResource)

Example 5 with AsyncGenericWorkItemHandler

use of com.salaboy.jbpm5.dev.guide.workitems.AsyncGenericWorkItemHandler in project jBPM5-Developer-Guide by Salaboy.

the class SlowWebServicesInteractionsTest method testSlowWebServicesNoWait.

/**
 * Invokes 3 web services that take some time to be executed. The process
 * will continue with its execution without waiting for each web service to
 * finish. The process -> web service interaction is performed using the
 * Execution Service component. The Command being used is {@link CXFWebServiceCommand}
 * and the web service implementation is {@link SlowServiceImpl}.
 * @throws InterruptedException
 */
@Test
public void testSlowWebServicesNoWait() throws InterruptedException {
    initializeSession("three-systems-interactions-nowait.bpmn");
    HashMap<String, Object> input = new HashMap<String, Object>();
    String patientName = "John Doe";
    input.put("bedrequest_patientname", patientName);
    AsyncGenericWorkItemHandler webServiceHandler = new AsyncGenericWorkItemHandler(executor, session.getId());
    session.getWorkItemManager().registerWorkItemHandler("Slow Web Service", webServiceHandler);
    WorkflowProcessInstance pI = (WorkflowProcessInstance) session.startProcess("ThreeSystemsInteractions", input);
    // Even if the requests are not executed yet, the process is completed.
    List<RequestInfo> resultList = executor.getExecutedRequests();
    assertEquals(0, resultList.size());
    assertEquals(ProcessInstance.STATE_COMPLETED, pI.getState());
    Thread.sleep(35000);
    // After 35 seconds we could see that the web services were invoked
    // correctly.
    resultList = executor.getExecutedRequests();
    assertEquals(3, resultList.size());
    session.dispose();
}
Also used : AsyncGenericWorkItemHandler(com.salaboy.jbpm5.dev.guide.workitems.AsyncGenericWorkItemHandler) HashMap(java.util.HashMap) RequestInfo(org.jbpm.executor.entities.RequestInfo) WorkflowProcessInstance(org.drools.runtime.process.WorkflowProcessInstance) Test(org.junit.Test)

Aggregations

AsyncGenericWorkItemHandler (com.salaboy.jbpm5.dev.guide.workitems.AsyncGenericWorkItemHandler)5 HashMap (java.util.HashMap)4 WorkflowProcessInstance (org.drools.runtime.process.WorkflowProcessInstance)4 Test (org.junit.Test)4 RequestInfo (org.jbpm.executor.entities.RequestInfo)2 KnowledgeBase (org.drools.KnowledgeBase)1 KnowledgeBuilder (org.drools.builder.KnowledgeBuilder)1 KnowledgeBuilderError (org.drools.builder.KnowledgeBuilderError)1 KnowledgeBuilderErrors (org.drools.builder.KnowledgeBuilderErrors)1 DefaultAgendaEventListener (org.drools.event.rule.DefaultAgendaEventListener)1 ClassPathResource (org.drools.io.impl.ClassPathResource)1