Search in sources :

Example 16 with CommandContext

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

the class BasicExecutorBaseTest method executorExceptionTest.

@Test(timeout = 10000)
public void executorExceptionTest() throws InterruptedException {
    CountDownAsyncJobListener countDownListener = configureListener(1);
    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.SimpleIncrementCallback");
    commandContext.setData("retries", 0);
    executorService.scheduleRequest("org.jbpm.executor.ThrowExceptionCommand", commandContext);
    logger.info("{} Sleeping for 10 secs", System.currentTimeMillis());
    countDownListener.waitTillCompleted();
    List<RequestInfo> inErrorRequests = executorService.getInErrorRequests(new QueryContext());
    assertEquals(1, inErrorRequests.size());
    logger.info("Error: {}", inErrorRequests.get(0));
    assertEquals(1, inErrorRequests.get(0).getExecutions());
    List<ErrorInfo> errors = executorService.getAllErrors(new QueryContext());
    logger.info("Errors: {}", errors);
    assertEquals(1, errors.size());
}
Also used : CountDownAsyncJobListener(org.jbpm.executor.test.CountDownAsyncJobListener) AtomicLong(java.util.concurrent.atomic.AtomicLong) CommandContext(org.kie.api.executor.CommandContext) ErrorInfo(org.kie.api.executor.ErrorInfo) QueryContext(org.kie.api.runtime.query.QueryContext) RequestInfo(org.kie.api.executor.RequestInfo) Test(org.junit.Test)

Example 17 with CommandContext

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

the class BasicExecutorBaseTest method testCustomConstantRequestRetry.

@Test(timeout = 10000)
public void testCustomConstantRequestRetry() throws InterruptedException {
    CountDownAsyncJobListener countDownListener = configureListener(3);
    CommandContext ctxCMD = new CommandContext();
    ctxCMD.setData("businessKey", UUID.randomUUID().toString());
    ctxCMD.setData("retryDelay", "2s");
    ctxCMD.setData("retries", 2);
    executorService.scheduleRequest("org.jbpm.executor.ThrowExceptionCommand", ctxCMD);
    countDownListener.waitTillCompleted();
    List<RequestInfo> inErrorRequests = executorService.getInErrorRequests(new QueryContext());
    assertEquals(1, inErrorRequests.size());
    RequestInfo failedJob = inErrorRequests.get(0);
    assertEquals(3, failedJob.getExecutions());
    List<ErrorInfo> errors = executorService.getAllErrors(new QueryContext());
    // Three retries means 4 executions in total 1(regular) + 2(retries)
    assertEquals(3, errors.size());
    long firstError = errors.get(0).getTime().getTime();
    long secondError = errors.get(1).getTime().getTime();
    long thirdError = errors.get(2).getTime().getTime();
    // time difference between first and second should be at least 3 seconds
    long diff = secondError - firstError;
    assertTrue(diff > 2000);
    // time difference between second and third should be at least 6 seconds
    diff = thirdError - secondError;
    assertTrue(diff > 2000);
}
Also used : CountDownAsyncJobListener(org.jbpm.executor.test.CountDownAsyncJobListener) CommandContext(org.kie.api.executor.CommandContext) ErrorInfo(org.kie.api.executor.ErrorInfo) QueryContext(org.kie.api.runtime.query.QueryContext) RequestInfo(org.kie.api.executor.RequestInfo) Test(org.junit.Test)

Example 18 with CommandContext

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

the class BasicExecutorBaseTest method testCustomIncrementingRequestRetry.

@Test(timeout = 10000)
public void testCustomIncrementingRequestRetry() throws InterruptedException {
    CountDownAsyncJobListener countDownListener = configureListener(3);
    CommandContext ctxCMD = new CommandContext();
    ctxCMD.setData("businessKey", UUID.randomUUID().toString());
    ctxCMD.setData("retryDelay", "3s, 6s");
    ctxCMD.setData("retries", 2);
    executorService.scheduleRequest("org.jbpm.executor.ThrowExceptionCommand", ctxCMD);
    countDownListener.waitTillCompleted();
    List<RequestInfo> inErrorRequests = executorService.getInErrorRequests(new QueryContext());
    assertEquals(1, inErrorRequests.size());
    List<ErrorInfo> errors = executorService.getAllErrors(new QueryContext());
    // Three retries means 4 executions in total 1(regular) + 3(retries)
    assertEquals(3, errors.size());
    long firstError = errors.get(0).getTime().getTime();
    long secondError = errors.get(1).getTime().getTime();
    long thirdError = errors.get(2).getTime().getTime();
    // time difference between first and second should be at least 3 seconds
    long diff = secondError - firstError;
    assertTrue(diff > 3000);
    // time difference between second and third should be at least 6 seconds
    diff = thirdError - secondError;
    assertTrue(diff > 6000);
}
Also used : CountDownAsyncJobListener(org.jbpm.executor.test.CountDownAsyncJobListener) CommandContext(org.kie.api.executor.CommandContext) ErrorInfo(org.kie.api.executor.ErrorInfo) QueryContext(org.kie.api.runtime.query.QueryContext) RequestInfo(org.kie.api.executor.RequestInfo) Test(org.junit.Test)

Example 19 with CommandContext

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

the class JmsAvaiableJobExecutorTest method testAsyncAuditProducerNotExistingDeployment.

@Test
public void testAsyncAuditProducerNotExistingDeployment() throws Exception {
    CountDownAsyncJobListener countDownListener = configureListener(1);
    CommandContext ctxCMD = new CommandContext();
    ctxCMD.setData("businessKey", UUID.randomUUID().toString());
    ctxCMD.setData("deploymentId", "not-existing");
    UserTransaction ut = InitialContext.doLookup("java:comp/UserTransaction");
    ut.begin();
    executorService.scheduleRequest("org.jbpm.executor.commands.PrintOutCommand", ctxCMD);
    ut.commit();
    MessageReceiver receiver = new MessageReceiver();
    receiver.receiveAndProcess(queue, countDownListener, 3000);
    List<RequestInfo> inErrorRequests = executorService.getInErrorRequests(new QueryContext());
    assertEquals(0, inErrorRequests.size());
    List<RequestInfo> queuedRequests = executorService.getQueuedRequests(new QueryContext());
    assertEquals(1, queuedRequests.size());
    List<RequestInfo> executedRequests = executorService.getCompletedRequests(new QueryContext());
    assertEquals(0, executedRequests.size());
}
Also used : UserTransaction(javax.transaction.UserTransaction) CountDownAsyncJobListener(org.jbpm.executor.test.CountDownAsyncJobListener) CommandContext(org.kie.api.executor.CommandContext) QueryContext(org.kie.api.runtime.query.QueryContext) RequestInfo(org.kie.api.executor.RequestInfo) Test(org.junit.Test)

Example 20 with CommandContext

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

the class JPASignalManager method signalEvent.

public void signalEvent(String type, Object event) {
    String actualSignalType = type.replaceFirst(ASYNC_SIGNAL_PREFIX, "");
    ProcessPersistenceContextManager contextManager = (ProcessPersistenceContextManager) getKnowledgeRuntime().getEnvironment().get(EnvironmentName.PERSISTENCE_CONTEXT_MANAGER);
    ProcessPersistenceContext context = contextManager.getProcessPersistenceContext();
    List<Long> processInstancesToSignalList = context.getProcessInstancesWaitingForEvent(actualSignalType);
    // handle signal asynchronously
    if (type.startsWith(ASYNC_SIGNAL_PREFIX)) {
        RuntimeManager runtimeManager = ((RuntimeManager) getKnowledgeRuntime().getEnvironment().get("RuntimeManager"));
        ExecutorService executorService = (ExecutorService) getKnowledgeRuntime().getEnvironment().get("ExecutorService");
        if (runtimeManager != null && executorService != null) {
            for (Long processInstanceId : processInstancesToSignalList) {
                CommandContext ctx = new CommandContext();
                ctx.setData("deploymentId", runtimeManager.getIdentifier());
                ctx.setData("processInstanceId", processInstanceId);
                ctx.setData("Signal", actualSignalType);
                ctx.setData("Event", event);
                executorService.scheduleRequest(AsyncSignalEventCommand.class.getName(), ctx);
            }
            return;
        } else {
            logger.warn("Signal should be sent asynchronously but there is no executor service available, continuing sync...");
        }
    }
    for (long id : processInstancesToSignalList) {
        try {
            getKnowledgeRuntime().getProcessInstance(id);
        } catch (IllegalStateException e) {
        // IllegalStateException can be thrown when using RuntimeManager
        // and invalid ksession was used for given context
        } catch (RuntimeException e) {
            logger.warn("Exception when loading process instance for signal '{}', instance with id {} will not be signaled", e.getMessage(), id);
        }
    }
    super.signalEvent(actualSignalType, event);
}
Also used : CommandContext(org.kie.api.executor.CommandContext) RuntimeManager(org.kie.api.runtime.manager.RuntimeManager) ExecutorService(org.kie.api.executor.ExecutorService) ProcessPersistenceContextManager(org.jbpm.persistence.api.ProcessPersistenceContextManager) AsyncSignalEventCommand(org.jbpm.process.core.async.AsyncSignalEventCommand) ProcessPersistenceContext(org.jbpm.persistence.api.ProcessPersistenceContext)

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