Search in sources :

Example 11 with CommandExecutor

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

the class BaseSpringRestTestCase 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("  " + tableName + ": " + count + " 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();
        commandExecutor.execute(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 : CommandContext(org.activiti.engine.impl.interceptor.CommandContext) CommandExecutor(org.activiti.engine.impl.interceptor.CommandExecutor) DbSqlSession(org.activiti.engine.impl.db.DbSqlSession)

Example 12 with CommandExecutor

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

the class SimulationAcquireJobsRunnable method run.

public synchronized void run() {
    //		    if (log.isLoggable(Level.INFO)) {
    //		      log.info(jobExecutor.getName() + " starting to acquire jobs");
    //		    }
    final CommandExecutor commandExecutor = jobExecutor.getCommandExecutor();
    // while is not needed - repetition is done by event scheduling 
    //		    while (!isInterrupted) {
    isWaiting.set(false);
    int maxJobsPerAcquisition = jobExecutor.getMaxJobsPerAcquisition();
    try {
        AcquiredJobs acquiredJobs = commandExecutor.execute(jobExecutor.getAcquireJobsCmd());
        for (List<String> jobIds : acquiredJobs.getJobIdBatches()) {
            jobExecutor.executeJobs(jobIds);
        }
        // if all jobs were executed
        millisToWait = jobExecutor.getWaitTimeInMillis();
        int jobsAcquired = acquiredJobs.getJobIdBatches().size();
        if (jobsAcquired < maxJobsPerAcquisition) {
            isJobAdded = false;
            // check if the next timer should fire before the normal sleep time is over
            Date duedate = new Date(SimulationRunContext.getClock().getCurrentTime().getTime() + millisToWait);
            List<TimerEntity> nextTimers = commandExecutor.execute(new GetUnlockedTimersByDuedateCmd(duedate, new Page(0, 1)));
            if (!nextTimers.isEmpty()) {
                long millisTillNextTimer = nextTimers.get(0).getDuedate().getTime() - SimulationRunContext.getClock().getCurrentTime().getTime();
                if (millisTillNextTimer < millisToWait && millisTillNextTimer != 0) {
                    millisToWait = millisTillNextTimer;
                }
            }
        } else {
            millisToWait = 0;
        }
    } catch (ActivitiOptimisticLockingException optimisticLockingException) {
        // See https://activiti.atlassian.net/browse/ACT-1390
        log.trace("Optimistic locking exception during job acquisition. If you have multiple job executors running against the same database, " + "this exception means that this thread tried to acquire a job, which already was acquired by another job executor acquisition thread." + "This is expected behavior in a clustered environment. " + "You can ignore this message if you indeed have multiple job executor acquisition threads running against the same database. " + "Exception message: " + optimisticLockingException.getMessage());
    } catch (Exception e) {
        log.error("exception during job acquisition: " + e.getMessage(), e);
        millisToWait = jobExecutor.getWaitTimeInMillis();
    }
    if ((millisToWait > 0) && (!isJobAdded)) {
        try {
            log.trace("job acquisition thread sleeping for " + millisToWait + " millis");
            synchronized (MONITOR) {
                if (!isInterrupted) {
                    isWaiting.set(true);
                    SimulationEvent event = new SimulationEvent.Builder(SimulationEvent.TYPE_ACQUIRE_JOB_NOTIFICATION_EVENT).simulationTime(SimulationRunContext.getClock().getCurrentTime().getTime() + millisToWait).property(this).build();
                    SimulationRunContext.getEventCalendar().addEvent(event);
                // do not need to wait. - event scheduling is enough
                //MONITOR.wait(millisToWait);
                }
            }
            log.trace("job acquisition thread woke up");
        } finally {
        //		          isWaiting.set(false);
        }
    } else {
        // schedule run now
        SimulationEvent event = new SimulationEvent.Builder(SimulationEvent.TYPE_ACQUIRE_JOB_NOTIFICATION_EVENT).simulationTime(SimulationRunContext.getClock().getCurrentTime().getTime()).property(this).build();
        SimulationRunContext.getEventCalendar().addEvent(event);
    }
//		    }
//		    if (log.isLoggable(Level.INFO)) {
//		      log.info(jobExecutor.getName() + " stopped job acquisition");
//		    }
}
Also used : TimerEntity(org.activiti.engine.impl.persistence.entity.TimerEntity) CommandExecutor(org.activiti.engine.impl.interceptor.CommandExecutor) ActivitiOptimisticLockingException(org.activiti.engine.ActivitiOptimisticLockingException) Page(org.activiti.engine.impl.Page) Date(java.util.Date) ActivitiOptimisticLockingException(org.activiti.engine.ActivitiOptimisticLockingException) SimulationEvent(org.activiti.crystalball.simulator.SimulationEvent)

Example 13 with CommandExecutor

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

the class ProcessEngineConfigurationImpl method initIdGenerator.

// id generator /////////////////////////////////////////////////////////////
protected void initIdGenerator() {
    if (idGenerator == null) {
        CommandExecutor idGeneratorCommandExecutor = null;
        if (idGeneratorDataSource != null) {
            ProcessEngineConfigurationImpl processEngineConfiguration = new StandaloneProcessEngineConfiguration();
            processEngineConfiguration.setDataSource(idGeneratorDataSource);
            processEngineConfiguration.setDatabaseSchemaUpdate(DB_SCHEMA_UPDATE_FALSE);
            processEngineConfiguration.init();
            idGeneratorCommandExecutor = processEngineConfiguration.getCommandExecutor();
        } else if (idGeneratorDataSourceJndiName != null) {
            ProcessEngineConfigurationImpl processEngineConfiguration = new StandaloneProcessEngineConfiguration();
            processEngineConfiguration.setDataSourceJndiName(idGeneratorDataSourceJndiName);
            processEngineConfiguration.setDatabaseSchemaUpdate(DB_SCHEMA_UPDATE_FALSE);
            processEngineConfiguration.init();
            idGeneratorCommandExecutor = processEngineConfiguration.getCommandExecutor();
        } else {
            idGeneratorCommandExecutor = getCommandExecutor();
        }
        DbIdGenerator dbIdGenerator = new DbIdGenerator();
        dbIdGenerator.setIdBlockSize(idBlockSize);
        dbIdGenerator.setCommandExecutor(idGeneratorCommandExecutor);
        dbIdGenerator.setCommandConfig(getDefaultCommandConfig().transactionRequiresNew());
        idGenerator = dbIdGenerator;
    }
}
Also used : CommandExecutor(org.activiti.engine.impl.interceptor.CommandExecutor) DbIdGenerator(org.activiti.engine.impl.db.DbIdGenerator)

Example 14 with CommandExecutor

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

the class StandaloneMybatisTransactionContext method fireTransactionEvent.

/**
   * Fires the event for the provided {@link TransactionState}.
   * 
   * @param transactionState The {@link TransactionState} for which the listeners will be called.
   * @param executeInNewContext If true, the listeners will be called in a new command context.
   *                            This is needed for example when firing the {@link TransactionState#COMMITTED}
   *                            event: the transaction is already committed and executing logic in the same
   *                            context could lead to strange behaviour (for example doing a {@link SqlSession#update(String)}
   *                            would actually roll back the update (as the MyBatis context is already committed
   *                            and the internal flags have not been correctly set).
   */
protected void fireTransactionEvent(TransactionState transactionState, boolean executeInNewContext) {
    if (stateTransactionListeners == null) {
        return;
    }
    final List<TransactionListener> transactionListeners = stateTransactionListeners.get(transactionState);
    if (transactionListeners == null) {
        return;
    }
    if (executeInNewContext) {
        CommandExecutor commandExecutor = commandContext.getProcessEngineConfiguration().getCommandExecutor();
        CommandConfig commandConfig = new CommandConfig(false, TransactionPropagation.REQUIRES_NEW);
        commandExecutor.execute(commandConfig, new Command<Void>() {

            public Void execute(CommandContext commandContext) {
                executeTransactionListeners(transactionListeners, commandContext);
                return null;
            }
        });
    } else {
        executeTransactionListeners(transactionListeners, commandContext);
    }
}
Also used : TransactionListener(org.activiti.engine.impl.cfg.TransactionListener) CommandConfig(org.activiti.engine.impl.interceptor.CommandConfig) CommandContext(org.activiti.engine.impl.interceptor.CommandContext) CommandExecutor(org.activiti.engine.impl.interceptor.CommandExecutor)

Example 15 with CommandExecutor

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

the class DbSchemaDrop 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).dbSchemaDrop();
            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)

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