作者:blue燃烧的火柴 | 来源:互联网 | 2024-12-08 18:27
在 Java 开发中,特别是在使用 Activiti 工作流引擎时,org.activiti.engine.runtime.JobQuery.withException()
方法是一个非常实用的功能。本文将通过具体的代码示例来展示如何使用 JobQuery.withException()
方法查询因异常而失败的任务。这些示例主要来自于 GitHub、Stack Overflow 和 Maven 等平台上的精选项目,具有很高的参考价值。
withException 方法介绍
英文说明: Only select jobs that failed due to an exception.
中文说明: 仅选择由于异常而失败的作业。
代码示例
示例 1: 来源:org.finra.herd/herd-service
@Override
public List getJobsWithExceptionByProcessInstanceId(String processInstanceId) {
return activitiManagementService.createJobQuery().withException().processInstanceId(processInstanceId).list();
}
示例 2: 来源:org.finra.herd/herd-service
@Override
public long getJobsWithExceptionCountByProcessInstanceId(String processInstanceId) {
return activitiManagementService.createJobQuery().withException().processInstanceId(processInstanceId).count();
}
示例 3: 来源:FINRAOS/herd
@Override
public List getJobsWithExceptionByProcessInstanceId(String processInstanceId) {
return activitiManagementService.createJobQuery().withException().processInstanceId(processInstanceId).list();
}
示例 4: 来源:FINRAOS/herd
@Override
public long getJobsWithExceptionCountByProcessInstanceId(String processInstanceId) {
return activitiManagementService.createJobQuery().withException().processInstanceId(processInstanceId).count();
}
示例 5: 测试用例,来源:FINRAOS/herd
@Test
public void testGetJobsWithExceptionByProcessInstanceId() {
String processInstanceId = "processInstanceId";
JobQuery jobQuery = mock(JobQuery.class);
when(activitiManagementService.createJobQuery()).thenReturn(jobQuery);
when(jobQuery.withException()).thenReturn(jobQuery);
when(jobQuery.processInstanceId(processInstanceId)).thenReturn(jobQuery);
List expectedJobs = new ArrayList<>();
when(jobQuery.list()).thenReturn(expectedJobs);
List actualJobs = activitiService.getJobsWithExceptionByProcessInstanceId(processInstanceId);
assertSame(expectedJobs, actualJobs);
InOrder inOrder = inOrder(jobQuery);
inOrder.verify(jobQuery).withException();
inOrder.verify(jobQuery).processInstanceId(processInstanceId);
inOrder.verify(jobQuery).list();
inOrder.verifyNoMoreInteractions();
}
示例 6: 测试用例,来源:FINRAOS/herd
@Test
public void testGetJobsWithExceptionCountByProcessInstanceId() {
String processInstanceId = "processInstanceId";
JobQuery jobQuery = mock(JobQuery.class);
when(activitiManagementService.createJobQuery()).thenReturn(jobQuery);
when(jobQuery.withException()).thenReturn(jobQuery);
when(jobQuery.processInstanceId(processInstanceId)).thenReturn(jobQuery);
long expectedResult = 1234L;
when(jobQuery.count()).thenReturn(expectedResult);
long actualResult = activitiService.getJobsWithExceptionCountByProcessInstanceId(processInstanceId);
assertEquals(expectedResult, actualResult);
InOrder inOrder = inOrder(jobQuery);
inOrder.verify(jobQuery).withException();
inOrder.verify(jobQuery).processInstanceId(processInstanceId);
inOrder.verify(jobQuery).count();
inOrder.verifyNoMoreInteractions();
}
示例 7: 来源:org.activiti/activiti-rest
query.withException();
示例 8: 来源:org.apache.provisionr/provisionr-commands
@Override
protected Object doExecute() throws Exception {
if (businessKey == null || businessKey.isEmpty()) {
out.println("Please supply a business key");
} else {
// 反转列表以首先启动子进程(它们的 ID 较大)
List processInstanceList = processEngine.getRuntimeService()
.createProcessInstanceQuery().variableValueEquals(CoreProcessVariables.POOL_BUSINESS_KEY, businessKey)
.orderByProcessInstanceId().desc().list();
out.printf("Found %d processes with pool business key %s\n", processInstanceList.size(), businessKey);
int count = 0;
for (ProcessInstance instance : processInstanceList) {
List jobs = processEngine.getManagementService().createJobQuery()
.processInstanceId(instance.getProcessInstanceId()).withException().list();
for (Job job : jobs) {
count++;
processEngine.getManagementService().setJobRetries(job.getId(), JobEntity.DEFAULT_RETRIES);
}
}
out.printf("Number of retries reset for %s jobs\n", count);
}
return null;
}