Java Code Examples for org.camunda.bpm.engine.runtime.JobQuery#singleResult()

The following examples show how to use org.camunda.bpm.engine.runtime.JobQuery#singleResult() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TaskListenerTest.java    From camunda-bpm-platform with Apache License 2.0 6 votes vote down vote up
@Test
@Deployment
public void testRecalculateTimeoutTaskListenerDuedateCreationDateBased() {
  // given
  ProcessInstance pi = runtimeService.startProcessInstanceByKey("process", Variables.putValue("duration", "PT1H"));

  JobQuery jobQuery = managementService.createJobQuery().processInstanceId(pi.getId());
  List<Job> jobs = jobQuery.list();
  assertEquals(1, jobs.size());
  Job job = jobs.get(0);
  Date oldDate = job.getDuedate();

  // when
  runtimeService.setVariable(pi.getId(), "duration", "PT15M");
  managementService.recalculateJobDuedate(job.getId(), true);

  // then
  Job jobUpdated = jobQuery.singleResult();
  assertEquals(job.getId(), jobUpdated.getId());
  assertNotEquals(oldDate, jobUpdated.getDuedate());
  assertTrue(oldDate.after(jobUpdated.getDuedate()));
  assertEquals(LocalDateTime.fromDateFields(jobUpdated.getCreateTime()).plusMinutes(15).toDate(), jobUpdated.getDuedate());
}
 
Example 2
Source File: FoxJobRetryCmdTest.java    From camunda-bpm-platform with Apache License 2.0 6 votes vote down vote up
protected void waitForExecutedJobWithRetriesLeft(int retriesLeft, String jobId) {
  JobQuery jobQuery = managementService.createJobQuery();

  if (jobId != null) {
    jobQuery.jobId(jobId);
  }

  Job job = jobQuery.singleResult();

  try {
    managementService.executeJob(job.getId());
  } catch (Exception e) {
  }

  // update job
  job = jobQuery.singleResult();

  if (job.getRetries() != retriesLeft) {
    waitForExecutedJobWithRetriesLeft(retriesLeft, jobId);
  }
}
 
Example 3
Source File: InterruptingEventSubProcessTest.java    From camunda-bpm-platform with Apache License 2.0 6 votes vote down vote up
@Deployment
public void testCancelTimer() {
  ProcessInstance pi = runtimeService.startProcessInstanceByKey("process");

  TaskQuery taskQuery = taskService.createTaskQuery();
  JobQuery jobQuery = managementService.createJobQuery().timers();

  Task task = taskQuery.singleResult();
  assertNotNull(task);
  assertEquals("taskBeforeInterruptingEventSuprocess", task.getTaskDefinitionKey());

  Job timer = jobQuery.singleResult();
  assertNotNull(timer);

  runtimeService.messageEventReceived("newMessage", pi.getId());

  task = taskQuery.singleResult();
  assertNotNull(task);
  assertEquals("taskAfterMessageStartEvent", task.getTaskDefinitionKey());

  assertEquals(0, jobQuery.count());

  taskService.complete(task.getId());

  assertProcessEnded(pi.getId());
}
 
Example 4
Source File: SpringRetryConfigurationTest.java    From camunda-bpm-platform with Apache License 2.0 6 votes vote down vote up
@Test
@OperateOnDeployment("clientDeployment")
public void testResolveRetryConfigBean() {
  // given
  ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("testRetry");

  JobQuery query = managementService
      .createJobQuery()
      .processInstanceId(processInstance.getId());

  Job job = query.singleResult();

  // when job fails
  try {
    managementService.executeJob(job.getId());
  } catch (Exception e) {
    // ignore
  }

  // then
  job = query.singleResult();
  Assert.assertEquals(6, job.getRetries());
}
 
Example 5
Source File: IntermediateTimerEventTest.java    From camunda-bpm-platform with Apache License 2.0 5 votes vote down vote up
@Deployment
public void testExpressionRecalculateCurrentDateBased() throws Exception {
  // Set the clock fixed
  HashMap<String, Object> variables = new HashMap<String, Object>();
  variables.put("duration", "PT1H");

  // After process start, there should be timer created
  ProcessInstanceWithVariables pi1 = (ProcessInstanceWithVariables) runtimeService.startProcessInstanceByKey("intermediateTimerEventExample", variables);
  JobQuery jobQuery = managementService.createJobQuery().processInstanceId(pi1.getId());
  assertEquals(1, jobQuery.count());
  Job job = jobQuery.singleResult();
  Date firstDate = job.getDuedate();

  // After variable change and recalculation, there should still be one timer only, with a changed due date
  moveByMinutes(1);
  Date currentTime = ClockUtil.getCurrentTime();
  runtimeService.setVariable(pi1.getProcessInstanceId(), "duration", "PT15M");
  processEngine.getManagementService().recalculateJobDuedate(job.getId(), false);
  
  assertEquals(1, jobQuery.count());
  job = jobQuery.singleResult();
  assertNotEquals(firstDate, job.getDuedate());
  assertTrue(firstDate.after(job.getDuedate()));
  Date expectedDate = LocalDateTime.fromDateFields(currentTime).plusMinutes(15).toDate();
  assertThat(job.getDuedate()).isCloseTo(expectedDate, 1000l);
  
  // After waiting for sixteen minutes the timer should fire
  ClockUtil.setCurrentTime(new Date(firstDate.getTime() + TimeUnit.MINUTES.toMillis(16L)));
  waitForJobExecutorToProcessAllJobs(5000L);

  assertEquals(0, managementService.createJobQuery().processInstanceId(pi1.getId()).count());
  assertProcessEnded(pi1.getProcessInstanceId());
}
 
Example 6
Source File: StartTimerEventTest.java    From camunda-bpm-platform with Apache License 2.0 5 votes vote down vote up
@Deployment(resources = "org/camunda/bpm/engine/test/bpmn/event/timer/StartTimerEventTest.testRecalculateExpressionStartTimerEvent.bpmn20.xml")
public void testRecalculateUnchangedExpressionStartTimerEventCreationDateBased() throws Exception {
  // given
  JobQuery jobQuery = managementService.createJobQuery();
  ProcessInstanceQuery processInstanceQuery = runtimeService.createProcessInstanceQuery().processDefinitionKey("startTimerEventExample");
  assertEquals(1, jobQuery.count());
  assertEquals(0, processInstanceQuery.count());
  
  // when
  moveByMinutes(1);
  managementService.recalculateJobDuedate(jobQuery.singleResult().getId(), true);
  
  // then due date should be based on the creation time
  assertEquals(1, jobQuery.count());
  assertEquals(0, processInstanceQuery.count());
  
  Job jobUpdated = jobQuery.singleResult();
  Date expectedDate = LocalDateTime.fromDateFields(jobUpdated.getCreateTime()).plusHours(2).toDate();
  assertEquals(expectedDate, jobUpdated.getDuedate());

  // move the clock forward 2 hours and 1 minute
  moveByMinutes(121);
  executeAllJobs();

  List<ProcessInstance> pi = processInstanceQuery.list();
  assertEquals(1, pi.size());

  assertEquals(0, jobQuery.count());
}
 
Example 7
Source File: SuspendJobDefinitionTest.java    From camunda-bpm-platform with Apache License 2.0 5 votes vote down vote up
@Deployment(resources = {"org/camunda/bpm/engine/test/api/mgmt/SuspensionTest.testBase.bpmn"})
public void testSuspensionByIdAndSuspendJobsFlag_shouldSuspendJobs() {
  // given
  // a deployed process definition with asynchronous continuation

  // a running process instance with a failed job
  Map<String, Object> params = new HashMap<>();
  params.put("fail", Boolean.TRUE);
  runtimeService.startProcessInstanceByKey("suspensionProcess", params);

  // a job definition (which was created for the asynchronous continuation)
  JobDefinition jobDefinition = managementService.createJobDefinitionQuery().singleResult();

  // when
  // suspend the job definition
  managementService.suspendJobDefinitionById(jobDefinition.getId(), true);

  // then
  // there exists a suspended job definition...
  JobDefinitionQuery jobDefinitionQuery = managementService.createJobDefinitionQuery().suspended();

  assertEquals(1, jobDefinitionQuery.count());

  JobDefinition suspendedJobDefinition = jobDefinitionQuery.singleResult();

  assertEquals(jobDefinition.getId(), suspendedJobDefinition.getId());
  assertTrue(suspendedJobDefinition.isSuspended());

  // ...and a suspended job of the provided job definition
  JobQuery jobQuery = managementService.createJobQuery().suspended();

  assertEquals(1, jobQuery.count());

  Job suspendedJob = jobQuery.singleResult();
  assertEquals(jobDefinition.getId(), suspendedJob.getJobDefinitionId());
  assertTrue(suspendedJob.isSuspended());

}
 
Example 8
Source File: SuspendJobDefinitionTest.java    From camunda-bpm-platform with Apache License 2.0 5 votes vote down vote up
@Deployment(resources = {"org/camunda/bpm/engine/test/api/mgmt/SuspensionTest.testBase.bpmn"})
public void testSuspensionByProcessDefinitionIdAndSuspendJobsFlag_shouldSuspendJobs() {
  // given
  // a deployed process definition with asynchronous continuation
  ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().singleResult();

  // a running process instance with a failed job
  Map<String, Object> params = new HashMap<>();
  params.put("fail", Boolean.TRUE);
  runtimeService.startProcessInstanceByKey("suspensionProcess", params);

  // a job definition (which was created for the asynchronous continuation)
  JobDefinition jobDefinition = managementService.createJobDefinitionQuery().singleResult();

  // when
  // suspend the job definition
  managementService.suspendJobDefinitionByProcessDefinitionId(processDefinition.getId(), true);

  // then
  // there exists a suspended job definition...
  JobDefinitionQuery jobDefinitionQuery = managementService.createJobDefinitionQuery().suspended();

  assertEquals(1, jobDefinitionQuery.count());

  JobDefinition suspendedJobDefinition = jobDefinitionQuery.singleResult();

  assertEquals(jobDefinition.getId(), suspendedJobDefinition.getId());
  assertTrue(suspendedJobDefinition.isSuspended());

  // ...and a suspended job of the provided job definition
  JobQuery jobQuery = managementService.createJobQuery().suspended();

  assertEquals(1, jobQuery.count());

  Job suspendedJob = jobQuery.singleResult();
  assertEquals(jobDefinition.getId(), suspendedJob.getJobDefinitionId());
  assertTrue(suspendedJob.isSuspended());

}
 
Example 9
Source File: SuspendJobTest.java    From camunda-bpm-platform with Apache License 2.0 5 votes vote down vote up
@Deployment(resources = {"org/camunda/bpm/engine/test/api/mgmt/SuspensionTest.testBase.bpmn"})
public void testSuspensionByProcessDefinitionKey_shouldSuspendJob() {
  // given
  // a deployed process definition
  ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().singleResult();

  // a running process instance with a failed job
  Map<String, Object> params = new HashMap<String, Object>();
  params.put("fail", Boolean.TRUE);
  runtimeService.startProcessInstanceByKey("suspensionProcess", params);

  // the failed job
  JobQuery jobQuery = managementService.createJobQuery();
  Job job = jobQuery.singleResult();
  assertFalse(job.isSuspended());

  // when
  // the job will be suspended
  managementService.suspendJobByProcessDefinitionKey(processDefinition.getKey());

  // then
  // the job should be suspended
  assertEquals(0, jobQuery.active().count());
  assertEquals(1, jobQuery.suspended().count());

  Job suspendedJob = jobQuery.suspended().singleResult();

  assertEquals(job.getId(), suspendedJob.getId());
  assertTrue(suspendedJob.isSuspended());
}
 
Example 10
Source File: StartTimerEventTest.java    From camunda-bpm-platform with Apache License 2.0 5 votes vote down vote up
@Deployment
public void testPriorityInTimerCycleEvent() throws Exception {
  ClockUtil.setCurrentTime(new Date());

  // After process start, there should be timer created
  JobQuery jobQuery = managementService.createJobQuery();
  assertEquals(1, jobQuery.count());

  // ensure that the deployment Id is set on the new job
  Job job = jobQuery.singleResult();
  assertNotNull(job.getDeploymentId());
  assertEquals(9999, job.getPriority());

  final ProcessInstanceQuery piq = runtimeService.createProcessInstanceQuery()
    .processDefinitionKey("startTimerEventExampleCycle");

  assertEquals(0, piq.count());

  moveByMinutes(5);
  executeAllJobs();
  assertEquals(1, piq.count());
  assertEquals(1, jobQuery.count());

  // ensure that the deployment Id is set on the new job
  job = jobQuery.singleResult();
  assertNotNull(job.getDeploymentId());

  // second job should have the same priority
  assertEquals(9999, job.getPriority());
}
 
Example 11
Source File: SuspendJobTest.java    From camunda-bpm-platform with Apache License 2.0 5 votes vote down vote up
@Deployment(resources = {"org/camunda/bpm/engine/test/api/mgmt/SuspensionTest.testBase.bpmn"})
public void testSuspensionByJobDefinitionId_shouldSuspendJob() {
  // given

  // a running process instance with a failed job
  Map<String, Object> params = new HashMap<String, Object>();
  params.put("fail", Boolean.TRUE);
  runtimeService.startProcessInstanceByKey("suspensionProcess", params);

  // the job definition
  JobDefinition jobDefinition = managementService.createJobDefinitionQuery().singleResult();

  // the failed job
  JobQuery jobQuery = managementService.createJobQuery();
  Job job = jobQuery.singleResult();
  assertFalse(job.isSuspended());

  // when
  // the job will be suspended
  managementService.suspendJobByJobDefinitionId(jobDefinition.getId());

  // then
  // the job should be suspended
  assertEquals(0, jobQuery.active().count());
  assertEquals(1, jobQuery.suspended().count());

  Job suspendedJob = jobQuery.suspended().singleResult();

  assertEquals(job.getId(), suspendedJob.getId());
  assertEquals(jobDefinition.getId(), suspendedJob.getJobDefinitionId());
  assertTrue(suspendedJob.isSuspended());
}
 
Example 12
Source File: SuspendJobTest.java    From camunda-bpm-platform with Apache License 2.0 5 votes vote down vote up
@Deployment(resources = {"org/camunda/bpm/engine/test/api/mgmt/SuspensionTest.testBase.bpmn"})
public void testSuspensionById_shouldSuspendJob() {
  // given

  // a running process instance with a failed job
  Map<String, Object> params = new HashMap<String, Object>();
  params.put("fail", Boolean.TRUE);
  runtimeService.startProcessInstanceByKey("suspensionProcess", params);

  // the failed job
  JobQuery jobQuery = managementService.createJobQuery();
  Job job = jobQuery.singleResult();
  assertFalse(job.isSuspended());

  // when
  // the job will be suspended
  managementService.suspendJobById(job.getId());

  // then
  // the job should be suspended
  assertEquals(0, jobQuery.active().count());
  assertEquals(1, jobQuery.suspended().count());

  Job suspendedJob = jobQuery.suspended().singleResult();

  assertEquals(job.getId(), suspendedJob.getId());
  assertTrue(suspendedJob.isSuspended());
}
 
Example 13
Source File: StartTimerEventTest.java    From camunda-bpm-platform with Apache License 2.0 5 votes vote down vote up
public void testRecalculateNonInterruptingWithChangedDurationExpressionInEventSubprocessCreationDateBased() throws Exception {
  // given
  ProcessBuilder processBuilder = Bpmn.createExecutableProcess("process");

  BpmnModelInstance modelInstance = processBuilder
    .startEvent()
      .userTask()
    .endEvent().done();

  processBuilder.eventSubProcess()
    .startEvent().interrupting(false).timerWithDuration("${duration}")
      .userTask("taskInSubprocess")
    .endEvent();

  deploymentId = repositoryService.createDeployment()
    .addModelInstance("process.bpmn", modelInstance).deploy()
    .getId();

  ProcessInstance pi = runtimeService.startProcessInstanceByKey("process", 
      Variables.createVariables().putValue("duration", "PT60S"));
  
  JobQuery jobQuery = managementService.createJobQuery();
  Job job = jobQuery.singleResult();
  String jobId = job.getId();
  Date oldDueDate = job.getDuedate();
  
  // when
  runtimeService.setVariable(pi.getId(), "duration", "PT2M");
  managementService.recalculateJobDuedate(jobId, true);

  // then
  assertEquals(1L, jobQuery.count());
  Date newDuedate = jobQuery.singleResult().getDuedate();
  Date expectedDate = LocalDateTime.fromDateFields(jobQuery.singleResult().getCreateTime()).plusMinutes(2).toDate();
  assertTrue(oldDueDate.before(newDuedate));
  assertTrue(expectedDate.equals(newDuedate));
  
  managementService.executeJob(jobId);
  assertEquals(1, taskService.createTaskQuery().taskName("taskInSubprocess").list().size());
}
 
Example 14
Source File: StartTimerEventTest.java    From camunda-bpm-platform with Apache License 2.0 5 votes vote down vote up
@Deployment
public void testCycleWithLimitStartTimerEvent() throws Exception {
  ClockUtil.setCurrentTime(new Date());

  // After process start, there should be timer created
  JobQuery jobQuery = managementService.createJobQuery();
  assertEquals(1, jobQuery.count());

  // ensure that the deployment Id is set on the new job
  Job job = jobQuery.singleResult();
  assertNotNull(job.getDeploymentId());

  final ProcessInstanceQuery piq = runtimeService.createProcessInstanceQuery().processDefinitionKey("startTimerEventExampleCycle");

  assertEquals(0, piq.count());

  moveByMinutes(5);
  executeAllJobs();
  assertEquals(1, piq.count());
  assertEquals(1, jobQuery.count());

  // ensure that the deployment Id is set on the new job
  job = jobQuery.singleResult();
  assertNotNull(job.getDeploymentId());

  moveByMinutes(5);
  executeAllJobs();
  assertEquals(2, piq.count());
  assertEquals(0, jobQuery.count());

}
 
Example 15
Source File: ActivateJobDefinitionTest.java    From camunda-bpm-platform with Apache License 2.0 4 votes vote down vote up
@Deployment(resources = {"org/camunda/bpm/engine/test/api/mgmt/SuspensionTest.testBase.bpmn"})
public void testActivationById_shouldExecuteImmediatelyAndRetainJobs() {
  // given
  // a deployed process definition with asynchronous continuation

  // a running process instance with a failed job
  Map<String, Object> params = new HashMap<>();
  params.put("fail", Boolean.TRUE);
  runtimeService.startProcessInstanceByKey("suspensionProcess", params);

  // a job definition (which was created for the asynchronous continuation)
  JobDefinition jobDefinition = managementService.createJobDefinitionQuery().singleResult();
  // ...which will be suspended with the corresponding jobs
  managementService.suspendJobDefinitionByProcessDefinitionKey("suspensionProcess", true);

  // when
  // activate the job definition
  managementService.activateJobDefinitionById(jobDefinition.getId(), false, null);

  // then
  // there exists an active job definition
  JobDefinitionQuery jobDefinitionQuery = managementService.createJobDefinitionQuery();

  assertEquals(0, jobDefinitionQuery.suspended().count());
  assertEquals(1, jobDefinitionQuery.active().count());

  JobDefinition activeJobDefinition = jobDefinitionQuery.active().singleResult();

  assertEquals(jobDefinition.getId(), activeJobDefinition.getId());
  assertFalse(activeJobDefinition.isSuspended());

  // the corresponding job is still suspended
  JobQuery jobQuery = managementService.createJobQuery();

  assertEquals(0, jobQuery.active().count());
  assertEquals(1, jobQuery.suspended().count());

  Job suspendedJob = jobQuery.singleResult();

  assertEquals(jobDefinition.getId(), suspendedJob.getJobDefinitionId());
  assertTrue(suspendedJob.isSuspended());

}
 
Example 16
Source File: ActivateJobDefinitionTest.java    From camunda-bpm-platform with Apache License 2.0 4 votes vote down vote up
@Deployment(resources = {"org/camunda/bpm/engine/test/api/mgmt/SuspensionTest.testBase.bpmn"})
public void testActivationByProcessDefinitionId_shouldExecuteImmediatelyAndSuspendJobs() {
  // given
  // a deployed process definition with asynchronous continuation
  ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().singleResult();

  // a running process instance with a failed job
  Map<String, Object> params = new HashMap<>();
  params.put("fail", Boolean.TRUE);
  runtimeService.startProcessInstanceByKey("suspensionProcess", params);

  // a job definition (which was created for the asynchronous continuation)
  JobDefinition jobDefinition = managementService.createJobDefinitionQuery().singleResult();
  // ...which will be suspended with the corresponding jobs
  managementService.suspendJobDefinitionByProcessDefinitionKey("suspensionProcess", true);

  // when
  // activate the job definition
  managementService.activateJobDefinitionByProcessDefinitionId(processDefinition.getId(), true, null);

  // then
  // there exists an active job definition...
  JobDefinitionQuery jobDefinitionQuery = managementService.createJobDefinitionQuery();

  assertEquals(0, jobDefinitionQuery.suspended().count());
  assertEquals(1, jobDefinitionQuery.active().count());

  JobDefinition activeJobDefinition = jobDefinitionQuery.active().singleResult();

  assertEquals(jobDefinition.getId(), activeJobDefinition.getId());
  assertFalse(activeJobDefinition.isSuspended());

  // ...and an active job of the provided job definition
  JobQuery jobQuery = managementService.createJobQuery();

  assertEquals(0, jobQuery.suspended().count());
  assertEquals(1, jobQuery.active().count());

  Job activeJob = jobQuery.singleResult();

  assertEquals(jobDefinition.getId(), activeJob.getJobDefinitionId());
  assertFalse(activeJob.isSuspended());

}
 
Example 17
Source File: SuspendJobDefinitionTest.java    From camunda-bpm-platform with Apache License 2.0 4 votes vote down vote up
@Deployment(resources = {"org/camunda/bpm/engine/test/api/mgmt/SuspensionTest.testBase.bpmn"})
public void testSuspensionByProcessDefinitionId_shouldExecuteDelayedAndRetainJobs() {
  // given
  // a deployed process definition with asynchronous continuation
  ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().singleResult();

  // a running process instance with a failed job
  Map<String, Object> params = new HashMap<>();
  params.put("fail", Boolean.TRUE);
  runtimeService.startProcessInstanceByKey("suspensionProcess", params);

  // a job definition (which was created for the asynchronous continuation)
  JobDefinition jobDefinition = managementService.createJobDefinitionQuery().singleResult();

  // when
  // suspend the job definition
  managementService.suspendJobDefinitionByProcessDefinitionId(processDefinition.getId(), false, oneWeekLater());

  // then
  // the job definition is still active
  JobDefinitionQuery jobDefinitionQuery = managementService.createJobDefinitionQuery();
  assertEquals(1, jobDefinitionQuery.active().count());
  assertEquals(0, jobDefinitionQuery.suspended().count());

  // there exists a job for the delayed suspension execution
  JobQuery jobQuery = managementService.createJobQuery();

  Job delayedSuspensionJob = jobQuery.timers().active().singleResult();
  assertNotNull(delayedSuspensionJob);
  assertThat(delayedSuspensionJob.getDeploymentId(), is(processDefinition.getDeploymentId()));

  // execute job
  managementService.executeJob(delayedSuspensionJob.getId());

  // the job definition should be suspended
  assertEquals(0, jobDefinitionQuery.active().count());
  assertEquals(1, jobDefinitionQuery.suspended().count());

  JobDefinition suspendedJobDefinition = jobDefinitionQuery.suspended().singleResult();

  assertEquals(jobDefinition.getId(), suspendedJobDefinition.getId());
  assertTrue(suspendedJobDefinition.isSuspended());

  // the corresponding job is still active
  jobQuery = managementService.createJobQuery().active();

  assertEquals(1, jobQuery.count());

  Job activeJob = jobQuery.singleResult();
  assertEquals(jobDefinition.getId(), activeJob.getJobDefinitionId());

  assertFalse(activeJob.isSuspended());

  jobQuery = managementService.createJobQuery().suspended();
  assertEquals(0, jobQuery.count());
}
 
Example 18
Source File: StartTimerEventTest.java    From camunda-bpm-platform with Apache License 2.0 4 votes vote down vote up
public void testRecalculateTimeCycleExpressionCreationDateBased() throws Exception {
  // given
  Mocks.register("cycle", "R/PT15M");

  ProcessBuilder processBuilder = Bpmn.createExecutableProcess("process");

  BpmnModelInstance modelInstance = processBuilder
    .startEvent().timerWithCycle("${cycle}")
      .userTask("aTaskName")
    .endEvent()
    .done();

  deploymentId = repositoryService.createDeployment()
    .addModelInstance("process.bpmn", modelInstance).deploy()
    .getId();
  
  JobQuery jobQuery = managementService.createJobQuery();
  assertEquals(1, jobQuery.count());

  Job job = jobQuery.singleResult();
  String jobId = job.getId();
  Date oldDuedate = job.getDuedate();

  // when
  moveByMinutes(1);
  managementService.recalculateJobDuedate(jobId, true);

  // then
  Job jobUpdated = jobQuery.singleResult();
  assertEquals(jobId, jobUpdated.getId());
  Date expectedDate = LocalDateTime.fromDateFields(jobUpdated.getCreateTime()).plusMinutes(15).toDate();
  assertEquals(expectedDate, jobUpdated.getDuedate());
  
  // when
  Mocks.register("cycle", "R/PT10M");
  managementService.recalculateJobDuedate(jobId, true);

  // then
  jobUpdated = jobQuery.singleResult();
  assertEquals(jobId, jobUpdated.getId());
  assertNotEquals(oldDuedate, jobUpdated.getDuedate());
  assertTrue(oldDuedate.after(jobUpdated.getDuedate()));
  expectedDate = LocalDateTime.fromDateFields(jobUpdated.getCreateTime()).plusMinutes(10).toDate();
  assertEquals(expectedDate, jobUpdated.getDuedate());
  
  Mocks.reset();
}
 
Example 19
Source File: SuspendJobDefinitionTest.java    From camunda-bpm-platform with Apache License 2.0 4 votes vote down vote up
@Deployment(resources = {"org/camunda/bpm/engine/test/api/mgmt/SuspensionTest.testBase.bpmn"})
public void testSuspensionById_shouldExecuteDelayedAndRetainJobs() {
  // given
  // a deployed process definition with asynchronous continuation

  // a running process instance with a failed job
  Map<String, Object> params = new HashMap<>();
  params.put("fail", Boolean.TRUE);
  runtimeService.startProcessInstanceByKey("suspensionProcess", params);

  // a job definition (which was created for the asynchronous continuation)
  JobDefinition jobDefinition = managementService.createJobDefinitionQuery().singleResult();

  // when
  // suspend the job definition
  managementService.suspendJobDefinitionById(jobDefinition.getId(), false, oneWeekLater());

  // then
  // the job definition is still active
  JobDefinitionQuery jobDefinitionQuery = managementService.createJobDefinitionQuery();
  assertEquals(1, jobDefinitionQuery.active().count());
  assertEquals(0, jobDefinitionQuery.suspended().count());

  // there exists a job for the delayed suspension execution
  JobQuery jobQuery = managementService.createJobQuery();

  Job delayedSuspensionJob = jobQuery.timers().active().singleResult();
  assertNotNull(delayedSuspensionJob);
  String deploymentId = repositoryService.createProcessDefinitionQuery()
      .processDefinitionId(jobDefinition.getProcessDefinitionId()).singleResult().getDeploymentId();
  assertThat(delayedSuspensionJob.getDeploymentId(), is(deploymentId));

  // execute job
  managementService.executeJob(delayedSuspensionJob.getId());

  // the job definition should be suspended
  assertEquals(0, jobDefinitionQuery.active().count());
  assertEquals(1, jobDefinitionQuery.suspended().count());

  JobDefinition suspendedJobDefinition = jobDefinitionQuery.suspended().singleResult();

  assertEquals(jobDefinition.getId(), suspendedJobDefinition.getId());
  assertTrue(suspendedJobDefinition.isSuspended());

  // the corresponding job is still active
  jobQuery = managementService.createJobQuery().active();

  assertEquals(1, jobQuery.count());

  Job activeJob = jobQuery.singleResult();
  assertEquals(jobDefinition.getId(), activeJob.getJobDefinitionId());

  assertFalse(activeJob.isSuspended());

  jobQuery = managementService.createJobQuery().suspended();
  assertEquals(0, jobQuery.count());
}
 
Example 20
Source File: ActivateJobDefinitionTest.java    From camunda-bpm-platform with Apache License 2.0 4 votes vote down vote up
@Deployment(resources = {"org/camunda/bpm/engine/test/api/mgmt/SuspensionTest.testBase.bpmn"})
public void testActivationById_shouldExecuteDelayedAndRetainJobs() {
  // given
  // a deployed process definition with asynchronous continuation

  // a running process instance with a failed job
  Map<String, Object> params = new HashMap<>();
  params.put("fail", Boolean.TRUE);
  runtimeService.startProcessInstanceByKey("suspensionProcess", params);

  // a job definition (which was created for the asynchronous continuation)
  JobDefinition jobDefinition = managementService.createJobDefinitionQuery().singleResult();
  // ...which will be suspended with the corresponding jobs
  managementService.suspendJobDefinitionByProcessDefinitionKey("suspensionProcess", true);

  // when
  // activate the job definition
  managementService.activateJobDefinitionById(jobDefinition.getId(), false, oneWeekLater());

  // then
  // the job definition is still suspended
  JobDefinitionQuery jobDefinitionQuery = managementService.createJobDefinitionQuery();
  assertEquals(0, jobDefinitionQuery.active().count());
  assertEquals(1, jobDefinitionQuery.suspended().count());

  // there exists a job for the delayed activation execution
  JobQuery jobQuery = managementService.createJobQuery();

  Job delayedActivationJob = jobQuery.timers().active().singleResult();
  assertNotNull(delayedActivationJob);
  String deploymentId = repositoryService.createProcessDefinitionQuery()
      .processDefinitionId(jobDefinition.getProcessDefinitionId()).singleResult().getDeploymentId();
  assertThat(delayedActivationJob.getDeploymentId(), is(deploymentId));

  // execute job
  managementService.executeJob(delayedActivationJob.getId());

  // the job definition should be suspended
  assertEquals(1, jobDefinitionQuery.active().count());
  assertEquals(0, jobDefinitionQuery.suspended().count());

  JobDefinition activeJobDefinition = jobDefinitionQuery.active().singleResult();

  assertEquals(jobDefinition.getId(), activeJobDefinition.getId());
  assertFalse(activeJobDefinition.isSuspended());

  // the corresponding job is still suspended
  jobQuery = managementService.createJobQuery();

  assertEquals(0, jobQuery.active().count());
  assertEquals(1, jobQuery.suspended().count());

  Job suspendedJob = jobQuery.singleResult();

  assertEquals(jobDefinition.getId(), suspendedJob.getJobDefinitionId());
  assertTrue(suspendedJob.isSuspended());

}