Skip to content

Commit

Permalink
flowable#3692: Query jobs by process definition key
Browse files Browse the repository at this point in the history
  • Loading branch information
amporsim committed Nov 24, 2023
1 parent fef9471 commit c0a4253
Show file tree
Hide file tree
Showing 14 changed files with 229 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,23 @@ public void testQueryByProcessDefinitionId() {
assertThat(query.singleResult()).isNull();
}

@Test
@Deployment(resources = "org/flowable/engine/test/api/mgmt/ExternalWorkerJobQueryTest.bpmn20.xml")
public void testQueryByProcessDefinitionKey() {
ProcessInstance processInstance1 = runtimeService.startProcessInstanceByKey("externalWorkerJobQueryTest");
ProcessInstance processInstance2 = runtimeService.startProcessInstanceByKey("externalWorkerJobQueryTest");
ExternalWorkerJobQuery query = managementService.createExternalWorkerJobQuery().processDefinitionKey(processInstance1.getProcessDefinitionKey());
assertThat(query.count()).isEqualTo(4);
assertThat(query.list())
.extracting(ExternalWorkerJob::getProcessInstanceId)
.containsOnly(processInstance1.getId(), processInstance2.getId());

query = managementService.createExternalWorkerJobQuery().processDefinitionKey("invalid");
assertThat(query.count()).isZero();
assertThat(query.list()).isEmpty();
assertThat(query.singleResult()).isNull();
}

@Test
@Deployment(resources = "org/flowable/engine/test/api/mgmt/ExternalWorkerJobQueryTest.bpmn20.xml")
public void testQueryByExecutionId() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -773,6 +773,55 @@ public void testTimerJobQueryWithoutScopeType() {
assertThat(query.list().size()).isEqualTo(3);
}

@Test
@Deployment(resources = "org/flowable/engine/test/bpmn/oneTask.bpmn20.xml")
public void testDeadLetterJobQueryByProcessDefinitionKey() {
ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("startToEnd");

DeadLetterJobEntity deadLetterJob = managementService.executeCommand(commandContext -> {
JobServiceConfiguration jobServiceConfiguration = CommandContextUtil.getProcessEngineConfiguration(commandContext).getJobServiceConfiguration();
DeadLetterJobEntityManager deadLetterJobEntityManager = jobServiceConfiguration.getDeadLetterJobEntityManager();
DeadLetterJobEntity job = deadLetterJobEntityManager.create();
job.setJobType(Job.JOB_TYPE_MESSAGE);
job.setProcessInstanceId(processInstance.getId());
job.setProcessDefinitionId(processInstance.getProcessDefinitionId());
jobServiceConfiguration.getDeadLetterJobDataManager().insert(job);
return job;
});

DeadLetterJobEntity deadLetterJob2 = managementService.executeCommand(commandContext -> {
JobServiceConfiguration jobServiceConfiguration = CommandContextUtil.getProcessEngineConfiguration(commandContext).getJobServiceConfiguration();
DeadLetterJobEntityManager deadLetterJobEntityManager = jobServiceConfiguration.getDeadLetterJobEntityManager();
DeadLetterJobEntity job = deadLetterJobEntityManager.create();
job.setJobType(Job.JOB_TYPE_MESSAGE);
job.setScopeId("scope1");
jobServiceConfiguration.getDeadLetterJobDataManager().insert(job);
return job;
});

DeadLetterJobQuery query = managementService.createDeadLetterJobQuery().processDefinitionKey("startToEnd");
assertThat(query.count()).isEqualTo(1);
assertThat(query.list()).extracting(Job::getId).containsExactly(deadLetterJob.getId());
assertThat(query.singleResult().getId()).isEqualTo(deadLetterJob.getId());

query = managementService.createDeadLetterJobQuery().processDefinitionKey("invalid");
assertThat(query.count()).isZero();
assertThat(query.list()).isEmpty();
assertThat(query.singleResult()).isNull();

managementService.executeCommand((Command<Void>) commandContext -> {
JobServiceConfiguration jobServiceConfiguration = CommandContextUtil.getProcessEngineConfiguration(commandContext).getJobServiceConfiguration();
DeadLetterJobEntityManager deadLetterJobService = jobServiceConfiguration.getDeadLetterJobEntityManager();
List<Job> jobs = deadLetterJobService.findJobsByQueryCriteria(new DeadLetterJobQueryImpl(commandContext, jobServiceConfiguration));
for (Job job : jobs) {
deadLetterJobService.delete(job.getId());
}

return null;
});


}
@Test
@Deployment(resources = "org/flowable/engine/test/bpmn/oneTask.bpmn20.xml")
public void testDeadLetterJobQueryWithoutScopeType() {
Expand Down Expand Up @@ -820,6 +869,55 @@ public void testDeadLetterJobQueryWithoutScopeType() {
});
}

@Test
@Deployment(resources = "org/flowable/engine/test/bpmn/oneTask.bpmn20.xml")
public void testSuspendedJobQueryByProcessDefinitionKey() {
ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("startToEnd");
SuspendedJobEntity suspendedJob = managementService.executeCommand(commandContext -> {
JobServiceConfiguration jobServiceConfiguration = CommandContextUtil.getProcessEngineConfiguration(commandContext).getJobServiceConfiguration();
SuspendedJobEntityManager suspendedJobEntityManager = jobServiceConfiguration.getSuspendedJobEntityManager();
SuspendedJobEntity job = suspendedJobEntityManager.create();
job.setJobType(Job.JOB_TYPE_MESSAGE);
job.setProcessInstanceId(processInstance.getId());
job.setProcessDefinitionId(processInstance.getProcessDefinitionId());
jobServiceConfiguration.getSuspendedJobEntityManager().insert(job);

return job;
});

SuspendedJobEntity suspendedJob2 = managementService.executeCommand(commandContext -> {
JobServiceConfiguration jobServiceConfiguration = CommandContextUtil.getProcessEngineConfiguration(commandContext).getJobServiceConfiguration();
SuspendedJobEntityManager suspendedJobEntityManager = jobServiceConfiguration.getSuspendedJobEntityManager();
SuspendedJobEntity job = suspendedJobEntityManager.create();
job.setJobType(Job.JOB_TYPE_MESSAGE);
job.setScopeId("scope1");
jobServiceConfiguration.getSuspendedJobEntityManager().insert(job);

return job;
});

SuspendedJobQuery query = managementService.createSuspendedJobQuery().processDefinitionKey("startToEnd");
assertThat(query.count()).isEqualTo(1);
assertThat(query.list()).extracting(Job::getId).containsExactly(suspendedJob.getId());
assertThat(query.singleResult().getId()).isEqualTo(suspendedJob.getId());

query = managementService.createSuspendedJobQuery().processDefinitionKey("invalid");
assertThat(query.count()).isZero();
assertThat(query.list()).isEmpty();
assertThat(query.singleResult()).isNull();

managementService.executeCommand((Command<Void>) commandContext -> {
JobServiceConfiguration jobServiceConfiguration = CommandContextUtil.getProcessEngineConfiguration(commandContext).getJobServiceConfiguration();
SuspendedJobEntityManager suspendedJobEntityManager = jobServiceConfiguration.getSuspendedJobEntityManager();
List<Job> jobs = suspendedJobEntityManager.findJobsByQueryCriteria(new SuspendedJobQueryImpl(commandContext, jobServiceConfiguration));
for (Job job : jobs) {
suspendedJobEntityManager.delete(job.getId());
}

return null;
});
}

@Test
@Deployment(resources = "org/flowable/engine/test/bpmn/oneTask.bpmn20.xml")
public void testSuspendedJobQueryWithoutScopeType() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,16 @@ public void testByExecutionId() {

@Test
public void testByProcessDefinitionId() {
String processDefinitionid = repositoryService.createProcessDefinitionQuery().singleResult().getId();
assertThat(managementService.createTimerJobQuery().processDefinitionId(processDefinitionid).count()).isEqualTo(3);
assertThat(managementService.createTimerJobQuery().processDefinitionId(processDefinitionid).list()).hasSize(3);
String processDefinitionId = repositoryService.createProcessDefinitionQuery().singleResult().getId();
assertThat(managementService.createTimerJobQuery().processDefinitionId(processDefinitionId).count()).isEqualTo(3);
assertThat(managementService.createTimerJobQuery().processDefinitionId(processDefinitionId).list()).hasSize(3);
}

@Test
public void testByProcessDefinitionKey() {
String processDefinitionKey = repositoryService.createProcessDefinitionQuery().singleResult().getKey();
assertThat(managementService.createTimerJobQuery().processDefinitionKey(processDefinitionKey).count()).isEqualTo(3);
assertThat(managementService.createTimerJobQuery().processDefinitionKey(processDefinitionKey).list()).hasSize(3);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ public interface BaseJobQuery<U extends BaseJobQuery<U, T>, T extends Job> exten
*/
U processDefinitionId(String processDefinitionId);

/**
* Only select jobs which exist for the given process definition key
*/
U processDefinitionKey(String processDefinitionKey);

/**
* Only select jobs which exist for the given category
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public class DeadLetterJobQueryImpl extends AbstractQuery<DeadLetterJobQuery, Jo
protected String handlerType;
protected Collection<String> handlerTypes;
protected String processDefinitionId;
protected String processDefinitionKey;
protected String category;
protected String categoryLike;
protected String elementId;
Expand Down Expand Up @@ -124,6 +125,14 @@ public DeadLetterJobQueryImpl processDefinitionId(String processDefinitionId) {
this.processDefinitionId = processDefinitionId;
return this;
}

public DeadLetterJobQueryImpl processDefinitionKey(String processDefinitionKey) {
if (processDefinitionKey == null) {
throw new FlowableIllegalArgumentException("Provided process definition key is null");
}
this.processDefinitionKey = processDefinitionKey;
return this;
}

@Override
public DeadLetterJobQueryImpl category(String category) {
Expand Down Expand Up @@ -482,6 +491,10 @@ public String getId() {
public String getProcessDefinitionId() {
return processDefinitionId;
}

public String getProcessDefinitionKey() {
return processDefinitionKey;
}

public String getCategory() {
return category;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public class ExternalWorkerJobQueryImpl extends AbstractQuery<ExternalWorkerJobQ
protected String handlerType;
protected Collection<String> handlerTypes;
protected String processDefinitionId;
protected String processDefinitionKey;
protected String category;
protected String categoryLike;
protected String elementId;
Expand Down Expand Up @@ -126,6 +127,15 @@ public ExternalWorkerJobQuery processDefinitionId(String processDefinitionId) {
return this;
}

@Override
public ExternalWorkerJobQuery processDefinitionKey(String processDefinitionKey) {
if (processDefinitionKey == null) {
throw new FlowableIllegalArgumentException("Provided process definition key is null");
}
this.processDefinitionKey = processDefinitionKey;
return this;
}

@Override
public ExternalWorkerJobQuery category(String category) {
if (category == null) {
Expand Down Expand Up @@ -470,6 +480,10 @@ public String getProcessDefinitionId() {
return processDefinitionId;
}

public String getProcessDefinitionKey() {
return processDefinitionKey;
}

public String getCategory() {
return category;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public class JobQueryImpl extends AbstractQuery<JobQuery, Job> implements JobQue
protected String handlerType;
protected Collection<String> handlerTypes;
protected String processDefinitionId;
protected String processDefinitionKey;
protected String category;
protected String categoryLike;
protected String elementId;
Expand Down Expand Up @@ -128,6 +129,15 @@ public JobQueryImpl processDefinitionId(String processDefinitionId) {
return this;
}

@Override
public JobQueryImpl processDefinitionKey(String processDefinitionKey) {
if (processDefinitionKey == null) {
throw new FlowableIllegalArgumentException("Provided process definition key is null");
}
this.processDefinitionKey = processDefinitionKey;
return this;
}

@Override
public JobQueryImpl category(String category) {
if (category == null) {
Expand Down Expand Up @@ -474,6 +484,10 @@ public String getProcessDefinitionId() {
return processDefinitionId;
}

public String getProcessDefinitionKey() {
return processDefinitionKey;
}

public String getCategory() {
return category;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public class SuspendedJobQueryImpl extends AbstractQuery<SuspendedJobQuery, Job>
protected String handlerType;
protected Collection<String> handlerTypes;
protected String processDefinitionId;
protected String processDefinitionKey;
protected String category;
protected String categoryLike;
protected String elementId;
Expand Down Expand Up @@ -127,6 +128,15 @@ public SuspendedJobQueryImpl processDefinitionId(String processDefinitionId) {
this.processDefinitionId = processDefinitionId;
return this;
}

@Override
public SuspendedJobQueryImpl processDefinitionKey(String processDefinitionKey) {
if (processDefinitionKey == null) {
throw new FlowableIllegalArgumentException("Provided process definition key is null");
}
this.processDefinitionKey = processDefinitionKey;
return this;
}

@Override
public SuspendedJobQueryImpl category(String category) {
Expand Down Expand Up @@ -524,6 +534,10 @@ public String getId() {
public String getProcessDefinitionId() {
return processDefinitionId;
}

public String getProcessDefinitionKey() {
return processDefinitionKey;
}

public String getCategory() {
return category;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public class TimerJobQueryImpl extends AbstractQuery<TimerJobQuery, Job> impleme
protected String handlerType;
protected Collection<String> handlerTypes;
protected String processDefinitionId;
protected String processDefinitionKey;
protected String category;
protected String categoryLike;
protected String elementId;
Expand Down Expand Up @@ -124,6 +125,15 @@ public TimerJobQueryImpl processDefinitionId(String processDefinitionId) {
return this;
}

@Override
public TimerJobQueryImpl processDefinitionKey(String processDefinitionKey) {
if (processDefinitionKey == null) {
throw new FlowableIllegalArgumentException("Provided process definition key is null");
}
this.processDefinitionKey = processDefinitionKey;
return this;
}

@Override
public TimerJobQueryImpl category(String category) {
if (category == null) {
Expand Down Expand Up @@ -458,6 +468,10 @@ public String getProcessDefinitionId() {
return processDefinitionId;
}

public String getProcessDefinitionKey() {
return processDefinitionKey;
}

public String getCategory() {
return category;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,9 @@

<sql id="selectDeadLetterJobByQueryCriteriaSql">
from ${prefix}ACT_RU_DEADLETTER_JOB RES
<if test="processDefinitionKey != null">
inner join ${prefix}ACT_RE_PROCDEF P on RES.PROC_DEF_ID_ = P.ID_
</if>
<where>
<if test="id != null">
RES.ID_ = #{id}
Expand Down Expand Up @@ -272,6 +275,9 @@
<if test="processDefinitionId != null">
and RES.PROC_DEF_ID_ = #{processDefinitionId}
</if>
<if test="processDefinitionKey != null">
and P.KEY_ = #{processDefinitionKey}
</if>
<if test="category != null">
and RES.CATEGORY_ = #{category}
</if>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,9 @@

<sql id="selectExternalWorkerJobByQueryCriteriaSql">
from ${prefix}ACT_RU_EXTERNAL_JOB RES
<if test="processDefinitionKey != null">
inner join ${prefix}ACT_RE_PROCDEF P on RES.PROC_DEF_ID_ = P.ID_
</if>
<where>
<if test="id != null">
RES.ID_ = #{id}
Expand Down Expand Up @@ -205,6 +208,9 @@
<if test="processDefinitionId != null">
and RES.PROC_DEF_ID_ = #{processDefinitionId}
</if>
<if test="processDefinitionKey != null">
and P.KEY_ = #{processDefinitionKey}
</if>
<if test="category != null">
and RES.CATEGORY_ = #{category}
</if>
Expand Down

0 comments on commit c0a4253

Please sign in to comment.