Search in sources :

Example 1 with MessageEntity

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

the class JobQueryTest method setUp.

/**
   * Setup will create
   *   - 3 process instances, each with one timer, each firing at t1/t2/t3 + 1 hour (see process)
   *   - 1 message
   */
protected void setUp() throws Exception {
    super.setUp();
    this.commandExecutor = processEngineConfiguration.getCommandExecutor();
    deploymentId = repositoryService.createDeployment().addClasspathResource("org/activiti/engine/test/api/mgmt/timerOnTask.bpmn20.xml").deploy().getId();
    // Create proc inst that has timer that will fire on t1 + 1 hour
    Calendar startTime = Calendar.getInstance();
    startTime.set(Calendar.MILLISECOND, 0);
    Date t1 = startTime.getTime();
    processEngineConfiguration.getClock().setCurrentTime(t1);
    processInstanceIdOne = runtimeService.startProcessInstanceByKey("timerOnTask").getId();
    testStartTime = t1;
    timerOneFireTime = new Date(t1.getTime() + ONE_HOUR);
    // Create proc inst that has timer that will fire on t2 + 1 hour
    startTime.add(Calendar.HOUR_OF_DAY, 1);
    // t2 = t1 + 1 hour
    Date t2 = startTime.getTime();
    processEngineConfiguration.getClock().setCurrentTime(t2);
    processInstanceIdTwo = runtimeService.startProcessInstanceByKey("timerOnTask").getId();
    timerTwoFireTime = new Date(t2.getTime() + ONE_HOUR);
    // Create proc inst that has timer that will fire on t3 + 1 hour
    startTime.add(Calendar.HOUR_OF_DAY, 1);
    // t3 = t2 + 1 hour
    Date t3 = startTime.getTime();
    processEngineConfiguration.getClock().setCurrentTime(t3);
    processInstanceIdThree = runtimeService.startProcessInstanceByKey("timerOnTask").getId();
    timerThreeFireTime = new Date(t3.getTime() + ONE_HOUR);
    // Create one message
    messageId = commandExecutor.execute(new Command<String>() {

        public String execute(CommandContext commandContext) {
            MessageEntity message = new MessageEntity();
            commandContext.getJobEntityManager().send(message);
            return message.getId();
        }
    });
}
Also used : MessageEntity(org.activiti.engine.impl.persistence.entity.MessageEntity) CommandContext(org.activiti.engine.impl.interceptor.CommandContext) Command(org.activiti.engine.impl.interceptor.Command) Calendar(java.util.Calendar) Date(java.util.Date)

Example 2 with MessageEntity

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

the class AcquireJobsCmd method execute.

public AcquiredJobs execute(CommandContext commandContext) {
    String lockOwner = jobExecutor.getLockOwner();
    int lockTimeInMillis = jobExecutor.getLockTimeInMillis();
    int maxNonExclusiveJobsPerAcquisition = jobExecutor.getMaxJobsPerAcquisition();
    AcquiredJobs acquiredJobs = new AcquiredJobs();
    List<JobEntity> jobs = commandContext.getJobEntityManager().findNextJobsToExecute(new Page(0, maxNonExclusiveJobsPerAcquisition));
    for (JobEntity job : jobs) {
        List<String> jobIds = new ArrayList<String>();
        if (job != null && !acquiredJobs.contains(job.getId())) {
            if (job instanceof MessageEntity && job.isExclusive() && job.getProcessInstanceId() != null) {
                // wait to get exclusive jobs within 100ms
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                }
                // acquire all exclusive jobs in the same process instance
                // (includes the current job)
                List<JobEntity> exclusiveJobs = commandContext.getJobEntityManager().findExclusiveJobsToExecute(job.getProcessInstanceId());
                for (JobEntity exclusiveJob : exclusiveJobs) {
                    if (exclusiveJob != null) {
                        lockJob(commandContext, exclusiveJob, lockOwner, lockTimeInMillis);
                        jobIds.add(exclusiveJob.getId());
                    }
                }
            } else {
                lockJob(commandContext, job, lockOwner, lockTimeInMillis);
                jobIds.add(job.getId());
            }
        }
        acquiredJobs.addJobIdBatch(jobIds);
    }
    return acquiredJobs;
}
Also used : JobEntity(org.activiti.engine.impl.persistence.entity.JobEntity) MessageEntity(org.activiti.engine.impl.persistence.entity.MessageEntity) ArrayList(java.util.ArrayList) AcquiredJobs(org.activiti.engine.impl.jobexecutor.AcquiredJobs) Page(org.activiti.engine.impl.Page)

Example 3 with MessageEntity

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

the class JobExecutorCmdExceptionTest method createTweetExceptionMessage.

protected MessageEntity createTweetExceptionMessage() {
    MessageEntity message = new MessageEntity();
    message.setJobHandlerType("tweet-exception");
    return message;
}
Also used : MessageEntity(org.activiti.engine.impl.persistence.entity.MessageEntity)

Example 4 with MessageEntity

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

the class JobExecutorTestCase method createTweetMessage.

protected MessageEntity createTweetMessage(String msg) {
    MessageEntity message = new MessageEntity();
    message.setJobHandlerType("tweet");
    message.setJobHandlerConfiguration(msg);
    return message;
}
Also used : MessageEntity(org.activiti.engine.impl.persistence.entity.MessageEntity)

Example 5 with MessageEntity

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

the class AsyncTaskTest method testFailingAsyncServiceTimer.

@Deployment
public void testFailingAsyncServiceTimer() {
    // start process 
    runtimeService.startProcessInstanceByKey("asyncService");
    // now there should be one job in the database, and it is a message
    assertEquals(1, managementService.createJobQuery().count());
    Job job = managementService.createJobQuery().singleResult();
    if (!(job instanceof MessageEntity)) {
        fail("the job must be a message");
    }
    try {
        managementService.executeJob(job.getId());
        fail();
    } catch (Exception e) {
    // exception expected
    }
    // the service failed: the execution is still sitting in the service task:
    Execution execution = runtimeService.createExecutionQuery().singleResult();
    assertNotNull(execution);
    assertEquals("service", runtimeService.getActiveActivityIds(execution.getId()).get(0));
    // there is still a single job because the timer was created in the same transaction as the 
    // service was executed (which rolled back)
    assertEquals(1, managementService.createJobQuery().count());
    runtimeService.deleteProcessInstance(execution.getId(), "dead");
}
Also used : MessageEntity(org.activiti.engine.impl.persistence.entity.MessageEntity) Execution(org.activiti.engine.runtime.Execution) Job(org.activiti.engine.runtime.Job) Deployment(org.activiti.engine.test.Deployment)

Aggregations

MessageEntity (org.activiti.engine.impl.persistence.entity.MessageEntity)8 CommandContext (org.activiti.engine.impl.interceptor.CommandContext)4 Job (org.activiti.engine.runtime.Job)4 ArrayList (java.util.ArrayList)1 Calendar (java.util.Calendar)1 Date (java.util.Date)1 Page (org.activiti.engine.impl.Page)1 Command (org.activiti.engine.impl.interceptor.Command)1 CommandExecutor (org.activiti.engine.impl.interceptor.CommandExecutor)1 AcquiredJobs (org.activiti.engine.impl.jobexecutor.AcquiredJobs)1 JobEntity (org.activiti.engine.impl.persistence.entity.JobEntity)1 Execution (org.activiti.engine.runtime.Execution)1 Deployment (org.activiti.engine.test.Deployment)1