Search in sources :

Example 21 with CommandExecutor

use of org.activiti.engine.impl.interceptor.CommandExecutor in project Activiti by Activiti.

the class ExecuteJobsRunnable method handleMultipleJobs.

protected void handleMultipleJobs() {
    final MultipleJobsExecutorContext jobExecutorContext = new MultipleJobsExecutorContext();
    final List<String> currentProcessorJobQueue = jobExecutorContext.getCurrentProcessorJobQueue();
    final CommandExecutor commandExecutor = jobExecutor.getCommandExecutor();
    currentProcessorJobQueue.addAll(jobIds);
    Context.setJobExecutorContext(jobExecutorContext);
    try {
        while (!currentProcessorJobQueue.isEmpty()) {
            String currentJobId = currentProcessorJobQueue.remove(0);
            try {
                commandExecutor.execute(new ExecuteJobsCmd(currentJobId));
            } catch (Throwable e) {
                log.error("exception during job execution: {}", e.getMessage(), e);
            } finally {
                jobExecutor.jobDone(currentJobId);
            }
        }
    } finally {
        Context.removeJobExecutorContext();
    }
}
Also used : CommandExecutor(org.activiti.engine.impl.interceptor.CommandExecutor) ExecuteJobsCmd(org.activiti.engine.impl.cmd.ExecuteJobsCmd)

Example 22 with CommandExecutor

use of org.activiti.engine.impl.interceptor.CommandExecutor in project Activiti by Activiti.

the class DbSchemaUpdate method main.

public static void main(String[] args) {
    ProcessEngineImpl processEngine = (ProcessEngineImpl) ProcessEngines.getDefaultProcessEngine();
    CommandExecutor commandExecutor = processEngine.getProcessEngineConfiguration().getCommandExecutor();
    CommandConfig config = new CommandConfig().transactionNotSupported();
    commandExecutor.execute(config, new Command<Object>() {

        public Object execute(CommandContext commandContext) {
            commandContext.getSession(DbSqlSession.class).dbSchemaUpdate();
            return null;
        }
    });
}
Also used : CommandConfig(org.activiti.engine.impl.interceptor.CommandConfig) CommandContext(org.activiti.engine.impl.interceptor.CommandContext) CommandExecutor(org.activiti.engine.impl.interceptor.CommandExecutor) ProcessEngineImpl(org.activiti.engine.impl.ProcessEngineImpl)

Example 23 with CommandExecutor

use of org.activiti.engine.impl.interceptor.CommandExecutor in project Activiti by Activiti.

the class AcquireTimerJobsRunnable method run.

public synchronized void run() {
    log.info("starting to acquire async jobs due");
    final CommandExecutor commandExecutor = asyncExecutor.getCommandExecutor();
    while (!isInterrupted) {
        try {
            AcquiredJobEntities acquiredJobs = commandExecutor.execute(new AcquireTimerJobsCmd(asyncExecutor.getLockOwner(), asyncExecutor.getTimerLockTimeInMillis(), asyncExecutor.getMaxTimerJobsPerAcquisition()));
            boolean allJobsSuccessfullyOffered = true;
            for (JobEntity job : acquiredJobs.getJobs()) {
                boolean jobSuccessFullyOffered = asyncExecutor.executeAsyncJob(job);
                if (!jobSuccessFullyOffered) {
                    allJobsSuccessfullyOffered = false;
                }
            }
            // if all jobs were executed
            millisToWait = asyncExecutor.getDefaultTimerJobAcquireWaitTimeInMillis();
            int jobsAcquired = acquiredJobs.size();
            if (jobsAcquired >= asyncExecutor.getMaxTimerJobsPerAcquisition()) {
                millisToWait = 0;
            }
            // If the queue was full, we wait too (even if we got enough jobs back), as not overload the queue
            if (millisToWait == 0 && !allJobsSuccessfullyOffered) {
                millisToWait = asyncExecutor.getDefaultQueueSizeFullWaitTimeInMillis();
            }
        } catch (ActivitiOptimisticLockingException optimisticLockingException) {
            if (log.isDebugEnabled()) {
                log.debug("Optimistic locking exception during timer job acquisition. If you have multiple timer executors running against the same database, " + "this exception means that this thread tried to acquire a timer job, which already was acquired by another timer executor acquisition thread." + "This is expected behavior in a clustered environment. " + "You can ignore this message if you indeed have multiple timer executor acquisition threads running against the same database. " + "Exception message: {}", optimisticLockingException.getMessage());
            }
        } catch (Throwable e) {
            log.error("exception during timer job acquisition: {}", e.getMessage(), e);
            millisToWait = asyncExecutor.getDefaultTimerJobAcquireWaitTimeInMillis();
        }
        if (millisToWait > 0) {
            try {
                if (log.isDebugEnabled()) {
                    log.debug("timer job acquisition thread sleeping for {} millis", millisToWait);
                }
                synchronized (MONITOR) {
                    if (!isInterrupted) {
                        isWaiting.set(true);
                        MONITOR.wait(millisToWait);
                    }
                }
                if (log.isDebugEnabled()) {
                    log.debug("timer job acquisition thread woke up");
                }
            } catch (InterruptedException e) {
                if (log.isDebugEnabled()) {
                    log.debug("timer job acquisition wait interrupted");
                }
            } finally {
                isWaiting.set(false);
            }
        }
    }
    log.info("stopped async job due acquisition");
}
Also used : JobEntity(org.activiti.engine.impl.persistence.entity.JobEntity) CommandExecutor(org.activiti.engine.impl.interceptor.CommandExecutor) ActivitiOptimisticLockingException(org.activiti.engine.ActivitiOptimisticLockingException) AcquireTimerJobsCmd(org.activiti.engine.impl.cmd.AcquireTimerJobsCmd)

Example 24 with CommandExecutor

use of org.activiti.engine.impl.interceptor.CommandExecutor in project Activiti by Activiti.

the class AbstractActivitiTestCase method assertAndEnsureCleanDb.

/** Each test is assumed to clean up all DB content it entered.
   * After a test method executed, this method scans all tables to see if the DB is completely clean. 
   * It throws AssertionFailed in case the DB is not clean.
   * If the DB is not clean, it is cleaned by performing a create a drop. */
protected void assertAndEnsureCleanDb() throws Throwable {
    log.debug("verifying that db is clean after test");
    Map<String, Long> tableCounts = managementService.getTableCount();
    StringBuilder outputMessage = new StringBuilder();
    for (String tableName : tableCounts.keySet()) {
        String tableNameWithoutPrefix = tableName.replace(processEngineConfiguration.getDatabaseTablePrefix(), "");
        if (!TABLENAMES_EXCLUDED_FROM_DB_CLEAN_CHECK.contains(tableNameWithoutPrefix)) {
            Long count = tableCounts.get(tableName);
            if (count != 0L) {
                outputMessage.append("  ").append(tableName).append(": ").append(count).append(" record(s) ");
            }
        }
    }
    if (outputMessage.length() > 0) {
        outputMessage.insert(0, "DB NOT CLEAN: \n");
        log.error(EMPTY_LINE);
        log.error(outputMessage.toString());
        log.info("dropping and recreating db");
        CommandExecutor commandExecutor = ((ProcessEngineImpl) processEngine).getProcessEngineConfiguration().getCommandExecutor();
        CommandConfig config = new CommandConfig().transactionNotSupported();
        commandExecutor.execute(config, new Command<Object>() {

            public Object execute(CommandContext commandContext) {
                DbSqlSession session = commandContext.getSession(DbSqlSession.class);
                session.dbSchemaDrop();
                session.dbSchemaCreate();
                return null;
            }
        });
        if (exception != null) {
            throw exception;
        } else {
            Assert.fail(outputMessage.toString());
        }
    } else {
        log.info("database was clean");
    }
}
Also used : CommandConfig(org.activiti.engine.impl.interceptor.CommandConfig) CommandContext(org.activiti.engine.impl.interceptor.CommandContext) CommandExecutor(org.activiti.engine.impl.interceptor.CommandExecutor) DbSqlSession(org.activiti.engine.impl.db.DbSqlSession)

Example 25 with CommandExecutor

use of org.activiti.engine.impl.interceptor.CommandExecutor in project Activiti by Activiti.

the class SpringTransactionContext method addTransactionListener.

public void addTransactionListener(final TransactionState transactionState, final TransactionListener transactionListener) {
    if (transactionState.equals(TransactionState.COMMITTING)) {
        TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronizationAdapter() {

            @Override
            public void beforeCommit(boolean readOnly) {
                transactionListener.execute(commandContext);
            }
        });
    } else if (transactionState.equals(TransactionState.COMMITTED)) {
        TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronizationAdapter() {

            @Override
            public void afterCommit() {
                CommandExecutor commandExecutor = commandContext.getProcessEngineConfiguration().getCommandExecutor();
                CommandConfig commandConfig = new CommandConfig(false, TransactionPropagation.REQUIRES_NEW);
                commandExecutor.execute(commandConfig, new Command<Void>() {

                    public Void execute(CommandContext commandContext) {
                        transactionListener.execute(commandContext);
                        return null;
                    }
                });
            }
        });
    } else if (transactionState.equals(TransactionState.ROLLINGBACK)) {
        TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronizationAdapter() {

            @Override
            public void beforeCompletion() {
                transactionListener.execute(commandContext);
            }
        });
    } else if (transactionState.equals(TransactionState.ROLLED_BACK)) {
        TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronizationAdapter() {

            @Override
            public void afterCompletion(int status) {
                if (TransactionSynchronization.STATUS_ROLLED_BACK == status) {
                    CommandExecutor commandExecutor = commandContext.getProcessEngineConfiguration().getCommandExecutor();
                    CommandConfig commandConfig = new CommandConfig(false, TransactionPropagation.REQUIRES_NEW);
                    commandExecutor.execute(commandConfig, new Command<Void>() {

                        public Void execute(CommandContext commandContext) {
                            transactionListener.execute(commandContext);
                            return null;
                        }
                    });
                }
            }
        });
    }
}
Also used : CommandConfig(org.activiti.engine.impl.interceptor.CommandConfig) CommandContext(org.activiti.engine.impl.interceptor.CommandContext) CommandExecutor(org.activiti.engine.impl.interceptor.CommandExecutor)

Aggregations

CommandExecutor (org.activiti.engine.impl.interceptor.CommandExecutor)36 CommandContext (org.activiti.engine.impl.interceptor.CommandContext)18 Deployment (org.activiti.engine.test.Deployment)11 ProcessInstance (org.activiti.engine.runtime.ProcessInstance)10 HistoricProcessInstance (org.activiti.engine.history.HistoricProcessInstance)8 SetProcessDefinitionVersionCmd (org.activiti.engine.impl.cmd.SetProcessDefinitionVersionCmd)8 CommandConfig (org.activiti.engine.impl.interceptor.CommandConfig)7 Date (java.util.Date)6 Execution (org.activiti.engine.runtime.Execution)6 DbSqlSession (org.activiti.engine.impl.db.DbSqlSession)5 JobEntity (org.activiti.engine.impl.persistence.entity.JobEntity)5 TimerEntity (org.activiti.engine.impl.persistence.entity.TimerEntity)5 ActivitiOptimisticLockingException (org.activiti.engine.ActivitiOptimisticLockingException)4 ProcessDefinition (org.activiti.engine.repository.ProcessDefinition)4 ActivitiException (org.activiti.engine.ActivitiException)3 ProcessEngineImpl (org.activiti.engine.impl.ProcessEngineImpl)3 AcquireTimerJobsCmd (org.activiti.engine.impl.cmd.AcquireTimerJobsCmd)3 JobEntityManager (org.activiti.engine.impl.persistence.entity.JobEntityManager)3 ProcessDefinitionEntity (org.activiti.engine.impl.persistence.entity.ProcessDefinitionEntity)3 ActivitiObjectNotFoundException (org.activiti.engine.ActivitiObjectNotFoundException)2