Search in sources :

Example 6 with ExecutionResults

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

the class QueryRunCommand method execute.

public ExecutionResults execute(CommandContext ctx) {
    BeanManager manager = CDIUtils.lookUpBeanManager(ctx);
    String clazz = QueryService.class.getName();
    try {
        QueryService cdiBean = (QueryService) CDIUtils.createBean(Class.forName(clazz), manager);
        logger.info("CDI bean created {}", cdiBean);
        String mapperClass = (String) ctx.getData("mapper");
        if (mapperClass == null) {
            mapperClass = "org.jbpm.kie.services.impl.query.mapper.ProcessInstanceQueryMapper";
        }
        Method m = Class.forName(mapperClass).getMethod("get", new Class[0]);
        QueryResultMapper<?> mapper = (QueryResultMapper<?>) m.invoke(null, new Object[0]);
        Object queryR = cdiBean.query((String) ctx.getData("query"), mapper, new QueryContext());
        logger.info("Result of the query is " + queryR);
    } catch (Exception e) {
        logger.error("Error while creating CDI bean from jbpm executor", e);
    }
    logger.info("Command executed on executor with data {}", ctx.getData());
    ExecutionResults executionResults = new ExecutionResults();
    return executionResults;
}
Also used : QueryResultMapper(org.jbpm.services.api.query.QueryResultMapper) QueryService(org.jbpm.services.api.query.QueryService) ExecutionResults(org.kie.api.executor.ExecutionResults) Method(java.lang.reflect.Method) QueryContext(org.kie.api.runtime.query.QueryContext) BeanManager(javax.enterprise.inject.spi.BeanManager)

Example 7 with ExecutionResults

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

the class ExecutionErrorCleanupCommand method execute.

@Override
public ExecutionResults execute(CommandContext ctx) throws Exception {
    SimpleDateFormat formatToUse = DATE_FORMAT;
    String dataFormat = (String) ctx.getData("DateFormat");
    if (dataFormat != null) {
        formatToUse = new SimpleDateFormat(dataFormat);
    }
    ExecutionResults executionResults = new ExecutionResults();
    String emfName = (String) ctx.getData("EmfName");
    if (emfName == null) {
        emfName = "org.jbpm.domain";
    }
    String singleRun = (String) ctx.getData("SingleRun");
    if ("true".equalsIgnoreCase(singleRun)) {
        // disable rescheduling
        this.nextScheduleTimeAdd = -1;
    }
    String nextRun = (String) ctx.getData("NextRun");
    if (nextRun != null) {
        nextScheduleTimeAdd = DateTimeUtils.parseDateAsDuration(nextRun);
    }
    // get hold of persistence and create instance of audit service
    EntityManagerFactory emf = EntityManagerFactoryManager.get().getOrCreate(emfName);
    // collect parameters
    String olderThan = (String) ctx.getData("OlderThan");
    String olderThanPeriod = (String) ctx.getData("OlderThanPeriod");
    String forProcess = (String) ctx.getData("ForProcess");
    String forProcessInstance = (String) ctx.getData("ForProcessInstance");
    String forDeployment = (String) ctx.getData("ForDeployment");
    if (olderThanPeriod != null) {
        long olderThanDuration = DateTimeUtils.parseDateAsDuration(olderThanPeriod);
        Date olderThanDate = new Date(System.currentTimeMillis() - olderThanDuration);
        olderThan = formatToUse.format(olderThanDate);
    }
    Map<String, Object> parameters = new HashMap<>();
    StringBuilder cleanUpErrorsQuery = new StringBuilder();
    cleanUpErrorsQuery.append("delete from ExecutionErrorInfo where processInstanceId in " + "(select processInstanceId from ProcessInstanceLog where status in (2,3))");
    if (olderThan != null && !olderThan.isEmpty()) {
        cleanUpErrorsQuery.append(" and errorDate < :olderThan");
        parameters.put("olderThan", formatToUse.parse(olderThan));
    }
    if (forProcess != null && !forProcess.isEmpty()) {
        cleanUpErrorsQuery.append(" and processId = :forProcess");
        parameters.put("forProcess", forProcess);
    }
    if (forProcessInstance != null && !forProcessInstance.isEmpty()) {
        cleanUpErrorsQuery.append(" and processInstanceId = :forProcessInstance");
        parameters.put("forProcessInstance", Long.parseLong(forProcessInstance));
    }
    if (forDeployment != null && !forDeployment.isEmpty()) {
        cleanUpErrorsQuery.append(" and deploymentId = :forDeployment");
        parameters.put("forDeployment", forDeployment);
    }
    TransactionalCommandService commandService = new TransactionalCommandService(emf);
    int deletedErrors = commandService.execute(new UpdateStringCommand(cleanUpErrorsQuery.toString(), parameters));
    logger.debug("Number of Execution errors deleted {}", deletedErrors);
    executionResults.setData("ErrorsDeleted", deletedErrors);
    return executionResults;
}
Also used : HashMap(java.util.HashMap) ExecutionResults(org.kie.api.executor.ExecutionResults) Date(java.util.Date) UpdateStringCommand(org.jbpm.shared.services.impl.commands.UpdateStringCommand) EntityManagerFactory(javax.persistence.EntityManagerFactory) TransactionalCommandService(org.jbpm.shared.services.impl.TransactionalCommandService) SimpleDateFormat(java.text.SimpleDateFormat)

Example 8 with ExecutionResults

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

the class ReoccurringPrintOutCommand method execute.

public ExecutionResults execute(CommandContext ctx) {
    logger.info("Command executed on executor with data {} at {}", ctx.getData(), new Date());
    ExecutionResults executionResults = new ExecutionResults();
    return executionResults;
}
Also used : ExecutionResults(org.kie.api.executor.ExecutionResults) Date(java.util.Date)

Example 9 with ExecutionResults

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

the class RequeueRunningJobsCommand method execute.

public ExecutionResults execute(CommandContext ctx) {
    Long olderThan = (Long) ctx.getData("MaxRunningTime");
    Long requestId = (Long) ctx.getData("RequestId");
    try {
        ExecutorService executorService = ExecutorServiceFactory.newExecutorService(null);
        if (executorService instanceof RequeueAware) {
            if (requestId != null) {
                logger.info("Requeue jobs by id {}", requestId);
                ((RequeueAware) executorService).requeueById(requestId);
            } else {
                logger.info("Requeue jobs older than {}", olderThan);
                ((RequeueAware) executorService).requeue(olderThan);
            }
        } else {
            logger.info("Executor Service is not capable of jobs requeue");
        }
    } catch (Exception e) {
        logger.error("Error while creating CDI bean from jbpm executor", e);
    }
    logger.info("Command executed on executor with data {}", ctx.getData());
    ExecutionResults executionResults = new ExecutionResults();
    return executionResults;
}
Also used : ExecutionResults(org.kie.api.executor.ExecutionResults) RequeueAware(org.jbpm.executor.RequeueAware) ExecutorService(org.kie.api.executor.ExecutorService)

Example 10 with ExecutionResults

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

the class LongRunningCommand method execute.

public ExecutionResults execute(CommandContext ctx) {
    System.out.println(Thread.currentThread().getName() + " [INFO] Command executed on executor with data " + ctx.getData());
    long time = 8 * 1000;
    System.out.println(Thread.currentThread().getName() + " [INFO] Simulating long running task, sleep time " + time + "ms");
    try {
        Thread.sleep(time);
    } catch (Exception ex) {
    }
    System.out.println(Thread.currentThread().getName() + " [INFO] Long running task finished after " + time + "ms");
    ExecutionResults executionResults = new ExecutionResults();
    return executionResults;
}
Also used : ExecutionResults(org.kie.api.executor.ExecutionResults)

Aggregations

ExecutionResults (org.kie.api.executor.ExecutionResults)23 Date (java.util.Date)5 EntityManagerFactory (javax.persistence.EntityManagerFactory)4 Test (org.junit.Test)4 WorkItem (org.kie.api.runtime.process.WorkItem)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 IOException (java.io.IOException)3 ObjectInputStream (java.io.ObjectInputStream)3 HashMap (java.util.HashMap)3 CommandContext (org.kie.api.executor.CommandContext)3 RuntimeEngine (org.kie.api.runtime.manager.RuntimeEngine)3 RuntimeManager (org.kie.api.runtime.manager.RuntimeManager)3 QueryContext (org.kie.api.runtime.query.QueryContext)3 SimpleDateFormat (java.text.SimpleDateFormat)2 Map (java.util.Map)2 AtomicLong (java.util.concurrent.atomic.AtomicLong)2 BeanManager (javax.enterprise.inject.spi.BeanManager)2 WorkItemImpl (org.drools.core.process.instance.impl.WorkItemImpl)2 TransactionalCommandService (org.jbpm.shared.services.impl.TransactionalCommandService)2 ExecutorService (org.kie.api.executor.ExecutorService)2