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();
}
}
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;
}
});
}
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");
}
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");
}
}
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;
}
});
}
}
});
}
}
Aggregations