Search in sources :

Example 41 with CommandContext

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

the class BasicExecutorBaseTest method testUpdateRequestDataFromErrorState.

@Test(timeout = 10000)
public void testUpdateRequestDataFromErrorState() throws InterruptedException {
    CountDownAsyncJobListener countDownListener = configureListener(1);
    CommandContext ctxCMD = new CommandContext();
    ctxCMD.setData("businessKey", UUID.randomUUID().toString());
    ctxCMD.setData("retries", 0);
    Long requestId = executorService.scheduleRequest("org.jbpm.executor.test.MissingDataCommand", ctxCMD);
    countDownListener.waitTillCompleted();
    List<RequestInfo> inErrorRequests = executorService.getInErrorRequests(new QueryContext());
    assertEquals(1, inErrorRequests.size());
    Map<String, Object> fixedData = new HashMap<>();
    fixedData.put("amount", 200);
    executorService.updateRequestData(requestId, fixedData);
    countDownListener.reset(1);
    ((RequeueAware) executorService).requeueById(requestId);
    countDownListener.waitTillCompleted();
    List<RequestInfo> executedRequests = executorService.getCompletedRequests(new QueryContext());
    assertEquals(1, executedRequests.size());
}
Also used : CountDownAsyncJobListener(org.jbpm.executor.test.CountDownAsyncJobListener) CommandContext(org.kie.api.executor.CommandContext) HashMap(java.util.HashMap) AtomicLong(java.util.concurrent.atomic.AtomicLong) QueryContext(org.kie.api.runtime.query.QueryContext) RequestInfo(org.kie.api.executor.RequestInfo) Test(org.junit.Test)

Example 42 with CommandContext

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

the class ReconfiguredExecutorTest method simpleExcecutionTest.

@Test
public void simpleExcecutionTest() throws InterruptedException {
    CountDownAsyncJobListener countDownListener = configureListener(1);
    CommandContext ctxCMD = new CommandContext();
    ctxCMD.setData("businessKey", UUID.randomUUID().toString());
    executorService.scheduleRequest("org.jbpm.executor.commands.PrintOutCommand", ctxCMD);
    countDownListener.waitTillCompleted();
    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 : 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 43 with CommandContext

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

the class JmsAvaiableJobExecutorTest method testAsyncAuditProducerPrioritizedJobs.

@Test
public void testAsyncAuditProducerPrioritizedJobs() throws Exception {
    CountDownAsyncJobListener countDownListener = configureListener(2);
    final List<String> executedJobs = new ArrayList<String>();
    ((ExecutorServiceImpl) executorService).addAsyncJobListener(new AsynchronousJobListener() {

        @Override
        public void beforeJobScheduled(AsynchronousJobEvent event) {
        }

        @Override
        public void beforeJobExecuted(AsynchronousJobEvent event) {
        }

        @Override
        public void beforeJobCancelled(AsynchronousJobEvent event) {
        }

        @Override
        public void afterJobScheduled(AsynchronousJobEvent event) {
        }

        @Override
        public void afterJobExecuted(AsynchronousJobEvent event) {
            executedJobs.add(event.getJob().getKey());
        }

        @Override
        public void afterJobCancelled(AsynchronousJobEvent event) {
        }
    });
    CommandContext ctxCMD = new CommandContext();
    ctxCMD.setData("businessKey", "low priority");
    ctxCMD.setData("priority", 2);
    CommandContext ctxCMD2 = new CommandContext();
    ctxCMD2.setData("businessKey", "high priority");
    ctxCMD2.setData("priority", 8);
    UserTransaction ut = InitialContext.doLookup("java:comp/UserTransaction");
    ut.begin();
    executorService.scheduleRequest("org.jbpm.executor.commands.PrintOutCommand", ctxCMD);
    executorService.scheduleRequest("org.jbpm.executor.commands.PrintOutCommand", ctxCMD2);
    ut.commit();
    MessageReceiver receiver = new MessageReceiver();
    receiver.receiveAndProcess(queue, countDownListener);
    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(2, executedRequests.size());
    assertEquals(2, executedJobs.size());
    assertEquals("high priority", executedJobs.get(0));
    assertEquals("low priority", executedJobs.get(1));
}
Also used : UserTransaction(javax.transaction.UserTransaction) AsynchronousJobListener(org.jbpm.executor.AsynchronousJobListener) ExecutorServiceImpl(org.jbpm.executor.impl.ExecutorServiceImpl) CommandContext(org.kie.api.executor.CommandContext) ArrayList(java.util.ArrayList) QueryContext(org.kie.api.runtime.query.QueryContext) RequestInfo(org.kie.api.executor.RequestInfo) AsynchronousJobEvent(org.jbpm.executor.AsynchronousJobEvent) CountDownAsyncJobListener(org.jbpm.executor.test.CountDownAsyncJobListener) Test(org.junit.Test)

Example 44 with CommandContext

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

the class JmsAvaiableJobExecutorTest method testAsyncAuditProducer.

@Test
public void testAsyncAuditProducer() throws Exception {
    CountDownAsyncJobListener countDownListener = configureListener(1);
    CommandContext ctxCMD = new CommandContext();
    ctxCMD.setData("businessKey", UUID.randomUUID().toString());
    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);
    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 : 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 45 with CommandContext

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

the class ExecutorImpl method updateRequestData.

@Override
public void updateRequestData(Long requestId, Map<String, Object> data) {
    logger.debug("About to update request {} data with following {}", requestId, data);
    RequestInfo request = (RequestInfo) executorStoreService.findRequest(requestId);
    if (request.getStatus().equals(STATUS.CANCELLED) || request.getStatus().equals(STATUS.DONE) || request.getStatus().equals(STATUS.RUNNING)) {
        throw new IllegalStateException("Request data can't be updated when request is in status " + request.getStatus());
    }
    CommandContext ctx = null;
    ClassLoader cl = getClassLoader(request.getDeploymentId());
    try {
        logger.debug("Processing Request Id: {}, status {} command {}", request.getId(), request.getStatus(), request.getCommandName());
        byte[] reqData = request.getRequestData();
        if (reqData != null) {
            ObjectInputStream in = null;
            try {
                in = new ClassLoaderObjectInputStream(cl, new ByteArrayInputStream(reqData));
                ctx = (CommandContext) in.readObject();
            } catch (IOException e) {
                logger.warn("Exception while serializing context data", e);
            } finally {
                if (in != null) {
                    in.close();
                }
            }
        }
    } catch (Exception e) {
        logger.error("Unexpected error when reading request data", e);
        throw new RuntimeException(e);
    }
    if (ctx == null) {
        ctx = new CommandContext();
    }
    WorkItem workItem = (WorkItem) ctx.getData("workItem");
    if (workItem != null) {
        logger.debug("Updating work item {} parameters with data {}", workItem, data);
        for (Entry<String, Object> entry : data.entrySet()) {
            workItem.setParameter(entry.getKey(), entry.getValue());
        }
    } else {
        logger.debug("Updating request context with data {}", data);
        for (Entry<String, Object> entry : data.entrySet()) {
            ctx.setData(entry.getKey(), entry.getValue());
        }
    }
    try {
        ByteArrayOutputStream bout = new ByteArrayOutputStream();
        ObjectOutputStream oout = new ObjectOutputStream(bout);
        oout.writeObject(ctx);
        request.setRequestData(bout.toByteArray());
    } catch (IOException e) {
        throw new RuntimeException("Unable to save updated request data", e);
    }
    executorStoreService.updateRequest(request, null);
    logger.debug("Request {} data updated successfully", requestId);
}
Also used : CommandContext(org.kie.api.executor.CommandContext) ClassLoaderObjectInputStream(org.apache.commons.io.input.ClassLoaderObjectInputStream) IOException(java.io.IOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) RequestInfo(org.jbpm.executor.entities.RequestInfo) ObjectOutputStream(java.io.ObjectOutputStream) WorkItem(org.drools.core.process.instance.WorkItem) ExecutorNotStartedException(org.jbpm.executor.ExecutorNotStartedException) IOException(java.io.IOException) JMSException(javax.jms.JMSException) ByteArrayInputStream(java.io.ByteArrayInputStream) ObjectInputStream(java.io.ObjectInputStream) ClassLoaderObjectInputStream(org.apache.commons.io.input.ClassLoaderObjectInputStream)

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