Search in sources :

Example 11 with JobEntity

use of org.activiti.engine.impl.persistence.entity.JobEntity 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 12 with JobEntity

use of org.activiti.engine.impl.persistence.entity.JobEntity in project Activiti by Activiti.

the class JobQueryTest method setRetries.

//helper ////////////////////////////////////////////////////////////
private void setRetries(final String processInstanceId, final int retries) {
    final Job job = managementService.createJobQuery().processInstanceId(processInstanceId).singleResult();
    commandExecutor.execute(new Command<Void>() {

        public Void execute(CommandContext commandContext) {
            JobEntity timer = commandContext.getDbSqlSession().selectById(JobEntity.class, job.getId());
            timer.setRetries(retries);
            return null;
        }
    });
}
Also used : JobEntity(org.activiti.engine.impl.persistence.entity.JobEntity) CommandContext(org.activiti.engine.impl.interceptor.CommandContext) Job(org.activiti.engine.runtime.Job)

Example 13 with JobEntity

use of org.activiti.engine.impl.persistence.entity.JobEntity in project Activiti by Activiti.

the class JobDetailPanel method addJobState.

protected void addJobState() {
    Label processDefinitionHeader = new Label(i18nManager.getMessage(Messages.JOB_HEADER_EXECUTION));
    processDefinitionHeader.addStyleName(ExplorerLayout.STYLE_H3);
    processDefinitionHeader.addStyleName(ExplorerLayout.STYLE_DETAIL_BLOCK);
    processDefinitionHeader.setWidth(100, UNITS_PERCENTAGE);
    addComponent(processDefinitionHeader);
    VerticalLayout layout = new VerticalLayout();
    layout.setSpacing(true);
    layout.setSizeFull();
    layout.setMargin(true, false, true, false);
    addDetailComponent(layout);
    setDetailExpandRatio(layout, 1.0f);
    // Exceptions
    if (job.getExceptionMessage() != null) {
        // Number of retries
        Label retrieslabel = new Label(getRetriesLabel(job));
        layout.addComponent(retrieslabel);
        // Exception
        Label exceptionMessageLabel = new Label(i18nManager.getMessage(Messages.JOB_ERROR) + ": " + job.getExceptionMessage());
        exceptionMessageLabel.addStyleName(ExplorerLayout.STYLE_JOB_EXCEPTION_MESSAGE);
        layout.addComponent(exceptionMessageLabel);
        // Add Exception stacktrace
        String stack = managementService.getJobExceptionStacktrace(job.getId());
        Label stackTraceLabel = new Label(stack);
        stackTraceLabel.setContentMode(Label.CONTENT_PREFORMATTED);
        stackTraceLabel.addStyleName(ExplorerLayout.STYLE_JOB_EXCEPTION_TRACE);
        stackTraceLabel.setSizeFull();
        Panel stackPanel = new Panel();
        stackPanel.setWidth(100, UNITS_PERCENTAGE);
        stackPanel.setSizeFull();
        stackPanel.setScrollable(true);
        stackPanel.addComponent(stackTraceLabel);
        layout.addComponent(stackPanel);
        layout.setExpandRatio(stackPanel, 1.0f);
    } else {
        if (job.getProcessDefinitionId() != null) {
            // This is a hack .. need to cleanify this in the engine
            JobEntity jobEntity = (JobEntity) job;
            if (jobEntity.getJobHandlerType().equals(TimerSuspendProcessDefinitionHandler.TYPE)) {
                addLinkToProcessDefinition(layout, i18nManager.getMessage(Messages.JOB_SUSPEND_PROCESSDEFINITION), false);
            } else if (jobEntity.getJobHandlerType().equals(TimerActivateProcessDefinitionHandler.TYPE)) {
                addLinkToProcessDefinition(layout, i18nManager.getMessage(Messages.JOB_ACTIVATE_PROCESSDEFINITION), true);
            } else {
                addNotYetExecutedLabel(layout);
            }
        } else {
            addNotYetExecutedLabel(layout);
        }
    }
}
Also used : JobEntity(org.activiti.engine.impl.persistence.entity.JobEntity) Panel(com.vaadin.ui.Panel) DetailPanel(org.activiti.explorer.ui.custom.DetailPanel) Label(com.vaadin.ui.Label) PrettyTimeLabel(org.activiti.explorer.ui.custom.PrettyTimeLabel) VerticalLayout(com.vaadin.ui.VerticalLayout)

Example 14 with JobEntity

use of org.activiti.engine.impl.persistence.entity.JobEntity in project Activiti by Activiti.

the class JobExecutorCmdHappyTest method testJobCommandsWithTimer.

public void testJobCommandsWithTimer() {
    // clock gets automatically reset in LogTestCase.runTest
    processEngineConfiguration.getClock().setCurrentTime(new Date(SOME_TIME));
    CommandExecutor commandExecutor = processEngineConfiguration.getCommandExecutor();
    String jobId = commandExecutor.execute(new Command<String>() {

        public String execute(CommandContext commandContext) {
            TimerEntity timer = createTweetTimer("i'm coding a test", new Date(SOME_TIME + (10 * SECOND)));
            commandContext.getJobEntityManager().schedule(timer);
            return timer.getId();
        }
    });
    AcquiredJobEntities acquiredJobs = commandExecutor.execute(new AcquireTimerJobsCmd("testLockOwner", 10000, 5));
    assertEquals(0, acquiredJobs.size());
    processEngineConfiguration.getClock().setCurrentTime(new Date(SOME_TIME + (20 * SECOND)));
    acquiredJobs = commandExecutor.execute(new AcquireTimerJobsCmd("testLockOwner", 10000, 5));
    assertEquals(1, acquiredJobs.size());
    JobEntity job = acquiredJobs.getJobs().iterator().next();
    assertEquals(jobId, job.getId());
    assertEquals(0, tweetHandler.getMessages().size());
    commandExecutor.execute(new ExecuteAsyncJobCmd(job));
    assertEquals("i'm coding a test", tweetHandler.getMessages().get(0));
    assertEquals(1, tweetHandler.getMessages().size());
}
Also used : AcquiredJobEntities(org.activiti.engine.impl.asyncexecutor.AcquiredJobEntities) JobEntity(org.activiti.engine.impl.persistence.entity.JobEntity) CommandContext(org.activiti.engine.impl.interceptor.CommandContext) TimerEntity(org.activiti.engine.impl.persistence.entity.TimerEntity) CommandExecutor(org.activiti.engine.impl.interceptor.CommandExecutor) ExecuteAsyncJobCmd(org.activiti.engine.impl.cmd.ExecuteAsyncJobCmd) Date(java.util.Date) AcquireTimerJobsCmd(org.activiti.engine.impl.cmd.AcquireTimerJobsCmd)

Example 15 with JobEntity

use of org.activiti.engine.impl.persistence.entity.JobEntity in project Activiti by Activiti.

the class HazelCastDistributedQueueBasedAsyncExecutor method initJobQueueListener.

protected void initJobQueueListener() {
    jobQueueListenerThread = new Thread(new Runnable() {

        public void run() {
            while (isActive) {
                JobEntity job = null;
                try {
                    // Blocking
                    job = jobQueue.take();
                } catch (InterruptedException e1) {
                    logger.info("jobQueueListenerThread interrupted. This is fine if the job executor is shutting down");
                // Do nothing, this can happen when shutting down
                } catch (HazelcastInstanceNotActiveException notActiveException) {
                    logger.info("Hazel cast not active exception caught. This is fine if the job executor is shutting down");
                }
                if (job != null) {
                    executorService.execute(new ExecuteAsyncRunnable(job, commandExecutor));
                }
            }
        }
    });
    jobQueueListenerThread.start();
}
Also used : JobEntity(org.activiti.engine.impl.persistence.entity.JobEntity) HazelcastInstanceNotActiveException(com.hazelcast.core.HazelcastInstanceNotActiveException) ExecuteAsyncRunnable(org.activiti.engine.impl.asyncexecutor.ExecuteAsyncRunnable) ExecuteAsyncRunnable(org.activiti.engine.impl.asyncexecutor.ExecuteAsyncRunnable)

Aggregations

JobEntity (org.activiti.engine.impl.persistence.entity.JobEntity)17 CommandExecutor (org.activiti.engine.impl.interceptor.CommandExecutor)5 Page (org.activiti.engine.impl.Page)3 AcquiredJobEntities (org.activiti.engine.impl.asyncexecutor.AcquiredJobEntities)3 CommandContext (org.activiti.engine.impl.interceptor.CommandContext)3 Job (org.activiti.engine.runtime.Job)3 Label (com.vaadin.ui.Label)2 ArrayList (java.util.ArrayList)2 Date (java.util.Date)2 ActivitiOptimisticLockingException (org.activiti.engine.ActivitiOptimisticLockingException)2 AcquireTimerJobsCmd (org.activiti.engine.impl.cmd.AcquireTimerJobsCmd)2 HazelcastInstanceNotActiveException (com.hazelcast.core.HazelcastInstanceNotActiveException)1 Panel (com.vaadin.ui.Panel)1 VerticalLayout (com.vaadin.ui.VerticalLayout)1 Calendar (java.util.Calendar)1 ActivitiException (org.activiti.engine.ActivitiException)1 ProcessEngineConfiguration (org.activiti.engine.ProcessEngineConfiguration)1 ActivitiEventDispatcher (org.activiti.engine.delegate.event.ActivitiEventDispatcher)1 ExecuteAsyncRunnable (org.activiti.engine.impl.asyncexecutor.ExecuteAsyncRunnable)1 DurationHelper (org.activiti.engine.impl.calendar.DurationHelper)1