Search in sources :

Example 6 with ActivitiOptimisticLockingException

use of org.activiti.engine.ActivitiOptimisticLockingException 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 7 with ActivitiOptimisticLockingException

use of org.activiti.engine.ActivitiOptimisticLockingException 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 8 with ActivitiOptimisticLockingException

use of org.activiti.engine.ActivitiOptimisticLockingException in project Activiti by Activiti.

the class StandaloneTaskTest method testOptimisticLockingThrownOnMultipleUpdates.

public void testOptimisticLockingThrownOnMultipleUpdates() {
    Task task = taskService.newTask();
    taskService.saveTask(task);
    String taskId = task.getId();
    // first modification
    Task task1 = taskService.createTaskQuery().taskId(taskId).singleResult();
    Task task2 = taskService.createTaskQuery().taskId(taskId).singleResult();
    task1.setDescription("first modification");
    taskService.saveTask(task1);
    // second modification on the initial instance
    task2.setDescription("second modification");
    try {
        taskService.saveTask(task2);
        fail("should get an exception here as the task was modified by someone else.");
    } catch (ActivitiOptimisticLockingException expected) {
    //  exception was thrown as expected
    }
    taskService.deleteTask(taskId, true);
}
Also used : Task(org.activiti.engine.task.Task) ActivitiOptimisticLockingException(org.activiti.engine.ActivitiOptimisticLockingException)

Example 9 with ActivitiOptimisticLockingException

use of org.activiti.engine.ActivitiOptimisticLockingException in project Activiti by Activiti.

the class IdentityServiceTest method testGroupOptimisticLockingException.

public void testGroupOptimisticLockingException() {
    Group group = identityService.newGroup("group");
    identityService.saveGroup(group);
    Group group1 = identityService.createGroupQuery().singleResult();
    Group group2 = identityService.createGroupQuery().singleResult();
    group1.setName("name one");
    identityService.saveGroup(group1);
    try {
        group2.setName("name two");
        identityService.saveGroup(group2);
        fail("Expected an exception");
    } catch (ActivitiOptimisticLockingException e) {
    // Expected an exception
    }
    identityService.deleteGroup(group.getId());
}
Also used : Group(org.activiti.engine.identity.Group) ActivitiOptimisticLockingException(org.activiti.engine.ActivitiOptimisticLockingException)

Example 10 with ActivitiOptimisticLockingException

use of org.activiti.engine.ActivitiOptimisticLockingException in project Activiti by Activiti.

the class TaskServiceTest method testUserTaskOptimisticLocking.

@Deployment(resources = { "org/activiti/engine/test/api/oneTaskProcess.bpmn20.xml" })
public void testUserTaskOptimisticLocking() {
    runtimeService.startProcessInstanceByKey("oneTaskProcess");
    Task task1 = taskService.createTaskQuery().singleResult();
    Task task2 = taskService.createTaskQuery().singleResult();
    task1.setDescription("test description one");
    taskService.saveTask(task1);
    try {
        task2.setDescription("test description two");
        taskService.saveTask(task2);
        fail("Expecting exception");
    } catch (ActivitiOptimisticLockingException e) {
    // Expected exception
    }
}
Also used : Task(org.activiti.engine.task.Task) ActivitiOptimisticLockingException(org.activiti.engine.ActivitiOptimisticLockingException) Deployment(org.activiti.engine.test.Deployment)

Aggregations

ActivitiOptimisticLockingException (org.activiti.engine.ActivitiOptimisticLockingException)10 CommandExecutor (org.activiti.engine.impl.interceptor.CommandExecutor)4 Task (org.activiti.engine.task.Task)3 Date (java.util.Date)2 JobEntity (org.activiti.engine.impl.persistence.entity.JobEntity)2 ArrayList (java.util.ArrayList)1 GregorianCalendar (java.util.GregorianCalendar)1 HashMap (java.util.HashMap)1 BrokenBarrierException (java.util.concurrent.BrokenBarrierException)1 CyclicBarrier (java.util.concurrent.CyclicBarrier)1 SimulationEvent (org.activiti.crystalball.simulator.SimulationEvent)1 Group (org.activiti.engine.identity.Group)1 User (org.activiti.engine.identity.User)1 Page (org.activiti.engine.impl.Page)1 AcquireAsyncJobsDueCmd (org.activiti.engine.impl.cmd.AcquireAsyncJobsDueCmd)1 AcquireTimerJobsCmd (org.activiti.engine.impl.cmd.AcquireTimerJobsCmd)1 CommandContext (org.activiti.engine.impl.interceptor.CommandContext)1 TimerEntity (org.activiti.engine.impl.persistence.entity.TimerEntity)1 ProcessInstance (org.activiti.engine.runtime.ProcessInstance)1 Deployment (org.activiti.engine.test.Deployment)1