Search in sources :

Example 21 with CommandContext

use of org.kie.api.executor.CommandContext in project jbpm by kiegroup.

the class AsyncIntermediateCatchSignalTest method testCorrectProcessStateAfterExceptionSignalCommand.

@Test(timeout = 10000)
public void testCorrectProcessStateAfterExceptionSignalCommand() throws InterruptedException {
    latch = new CountDownLatch(1);
    RuntimeManager runtimeManager = createRuntimeManager(BPMN_AICS);
    KieSession ksession = getRuntimeEngine().getKieSession();
    ProcessInstance pi = ksession.startProcess(PROCESS_AICS, null);
    long pid = pi.getId();
    CommandContext ctx = new CommandContext();
    ctx.setData("DeploymentId", runtimeManager.getIdentifier());
    ctx.setData("ProcessInstanceId", pid);
    ctx.setData("Signal", "MySignal");
    ctx.setData("Event", null);
    executorService.scheduleRequest(AsyncSignalEventCommand.class.getName(), ctx);
    latch.await();
}
Also used : CommandContext(org.kie.api.executor.CommandContext) RuntimeManager(org.kie.api.runtime.manager.RuntimeManager) KieSession(org.kie.api.runtime.KieSession) ProcessInstance(org.kie.api.runtime.process.ProcessInstance) CountDownLatch(java.util.concurrent.CountDownLatch) AsyncSignalEventCommand(org.jbpm.process.core.async.AsyncSignalEventCommand) Test(org.junit.Test)

Example 22 with CommandContext

use of org.kie.api.executor.CommandContext in project jbpm by kiegroup.

the class AsyncEventNodeInstance method internalTrigger.

public void internalTrigger(final NodeInstance from, String type) {
    super.internalTrigger(from, type);
    ExecutorService executorService = (ExecutorService) getProcessInstance().getKnowledgeRuntime().getEnvironment().get("ExecutorService");
    if (executorService != null) {
        RuntimeManager runtimeManager = ((RuntimeManager) getProcessInstance().getKnowledgeRuntime().getEnvironment().get("RuntimeManager"));
        CommandContext ctx = new CommandContext();
        ctx.setData("deploymentId", runtimeManager.getIdentifier());
        ctx.setData("processInstanceId", getProcessInstance().getId());
        ctx.setData("Signal", getEventType());
        ctx.setData("Event", null);
        executorService.scheduleRequest(AsyncSignalEventCommand.class.getName(), ctx);
        Node node = getNode();
        if (node != null) {
            String uniqueId = (String) node.getMetaData().get("UniqueId");
            if (uniqueId == null) {
                uniqueId = ((NodeImpl) node).getUniqueId();
            }
            ((WorkflowProcessInstanceImpl) getProcessInstance()).getIterationLevels().remove(getNode().getMetaData().get("UniqueId"));
        }
    } else {
        logger.warn("No async executor service found continuing as sync operation...");
        // if there is no executor service available move as sync node
        triggerCompleted();
    }
}
Also used : CommandContext(org.kie.api.executor.CommandContext) AsyncEventNode(org.jbpm.workflow.core.node.AsyncEventNode) Node(org.kie.api.definition.process.Node) ExecutorService(org.kie.api.executor.ExecutorService) RuntimeManager(org.kie.api.runtime.manager.RuntimeManager) AsyncSignalEventCommand(org.jbpm.process.core.async.AsyncSignalEventCommand)

Example 23 with CommandContext

use of org.kie.api.executor.CommandContext in project jbpm by kiegroup.

the class BasicExecutorIntegrationTest method cancelRequestTest.

@Test
public void cancelRequestTest() throws InterruptedException {
    // The executor is on purpose not started to not fight against race condition
    // with the request cancelations.
    CommandContext ctxCMD = new CommandContext();
    String businessKey = UUID.randomUUID().toString();
    ctxCMD.setData("businessKey", businessKey);
    Date futureDate = new Date(System.currentTimeMillis() + 5000);
    Long requestId = executorService.scheduleRequest("org.jbpm.executor.commands.PrintOutCommand", futureDate, ctxCMD);
    List<RequestInfo> requests = executorService.getRequestsByBusinessKey(businessKey, new QueryContext());
    assertNotNull(requests);
    assertEquals(1, requests.size());
    assertEquals(requestId, requests.get(0).getId());
    // cancel the task immediately
    executorService.cancelRequest(requestId);
    List<RequestInfo> cancelledRequests = executorService.getCancelledRequests(new QueryContext());
    assertEquals(1, cancelledRequests.size());
}
Also used : CommandContext(org.kie.api.executor.CommandContext) AtomicLong(java.util.concurrent.atomic.AtomicLong) QueryContext(org.kie.api.runtime.query.QueryContext) RequestInfo(org.kie.api.executor.RequestInfo) Date(java.util.Date) Test(org.junit.Test)

Example 24 with CommandContext

use of org.kie.api.executor.CommandContext in project jbpm by kiegroup.

the class BasicExecutorIntegrationTest method multipleCallbackTest.

@Test
public void multipleCallbackTest() throws InterruptedException {
    CommandContext commandContext = new CommandContext();
    commandContext.setData("businessKey", UUID.randomUUID().toString());
    cachedEntities.put((String) commandContext.getData("businessKey"), new AtomicLong(1));
    commandContext.setData("callbacks", "org.jbpm.executor.ejb.impl.test.SimpleIncrementCallback, org.jbpm.executor.ejb.impl.test.CustomCallback");
    executorService.scheduleRequest("org.jbpm.executor.commands.PrintOutCommand", commandContext);
    Thread.sleep(10000);
    List<RequestInfo> inErrorRequests = executorService.getInErrorRequests(new QueryContext());
    assertEquals(0, inErrorRequests.size());
    List<RequestInfo> queuedRequests = executorService.getQueuedRequests(new QueryContext());
    assertEquals(0, queuedRequests.size());
    List<RequestInfo> executedRequests = executorService.getCompletedRequests(new QueryContext());
    assertEquals(1, executedRequests.size());
    assertEquals(2, ((AtomicLong) cachedEntities.get((String) commandContext.getData("businessKey"))).longValue());
    ExecutionResults results = null;
    byte[] responseData = executedRequests.get(0).getResponseData();
    ObjectInputStream in = null;
    try {
        in = new ObjectInputStream(new ByteArrayInputStream(responseData));
        results = (ExecutionResults) in.readObject();
    } catch (Exception e) {
        logger.warn("Exception while serializing context data", e);
        return;
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    String result = (String) results.getData("custom");
    assertNotNull(result);
    assertEquals("custom callback invoked", result);
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) CommandContext(org.kie.api.executor.CommandContext) ByteArrayInputStream(java.io.ByteArrayInputStream) ExecutionResults(org.kie.api.executor.ExecutionResults) QueryContext(org.kie.api.runtime.query.QueryContext) IOException(java.io.IOException) RequestInfo(org.kie.api.executor.RequestInfo) IOException(java.io.IOException) ObjectInputStream(java.io.ObjectInputStream) Test(org.junit.Test)

Example 25 with CommandContext

use of org.kie.api.executor.CommandContext in project jbpm by kiegroup.

the class BasicExecutorIntegrationTest method simpleExcecutionTest.

@Test
public void simpleExcecutionTest() throws InterruptedException {
    CommandContext ctxCMD = new CommandContext();
    ctxCMD.setData("businessKey", UUID.randomUUID().toString());
    executorService.scheduleRequest("org.jbpm.executor.commands.PrintOutCommand", ctxCMD);
    Thread.sleep(10000);
    List<RequestInfo> inErrorRequests = executorService.getInErrorRequests(new QueryContext());
    assertEquals(0, inErrorRequests.size());
    List<RequestInfo> queuedRequests = executorService.getQueuedRequests(new QueryContext());
    assertEquals(0, queuedRequests.size());
    List<RequestInfo> executedRequests = executorService.getCompletedRequests(new QueryContext());
    assertEquals(1, executedRequests.size());
}
Also used : CommandContext(org.kie.api.executor.CommandContext) QueryContext(org.kie.api.runtime.query.QueryContext) RequestInfo(org.kie.api.executor.RequestInfo) Test(org.junit.Test)

Aggregations

CommandContext (org.kie.api.executor.CommandContext)52 Test (org.junit.Test)43 RequestInfo (org.kie.api.executor.RequestInfo)39 QueryContext (org.kie.api.runtime.query.QueryContext)39 CountDownAsyncJobListener (org.jbpm.executor.test.CountDownAsyncJobListener)25 AtomicLong (java.util.concurrent.atomic.AtomicLong)22 Date (java.util.Date)10 ErrorInfo (org.kie.api.executor.ErrorInfo)9 KieSession (org.kie.api.runtime.KieSession)6 HashMap (java.util.HashMap)5 RuntimeManager (org.kie.api.runtime.manager.RuntimeManager)5 ProcessInstance (org.kie.api.runtime.process.ProcessInstance)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4 IOException (java.io.IOException)4 ObjectInputStream (java.io.ObjectInputStream)4 ExecutorServiceImpl (org.jbpm.executor.impl.ExecutorServiceImpl)4 AsyncSignalEventCommand (org.jbpm.process.core.async.AsyncSignalEventCommand)4 AbstractRuntimeManager (org.jbpm.runtime.manager.impl.AbstractRuntimeManager)4 RuntimeEngine (org.kie.api.runtime.manager.RuntimeEngine)4 UserTransaction (javax.transaction.UserTransaction)3