高效码农

Activiti7集成SpringBoot2官方实例(七):实现服务任务和监听器

Activiti 7中的服务任务和侦听器的实现方式与以前的版本不同。

实现服务任务Spring Bean

服务任务是我们流程定义中的最后一个活动。让我们实现它,以便我们可以完成流程实例。

我们需要做的是创建一个名为 serviceTask1Impl 的Spring Bean ,它将代表服务任务的实现。Spring bean需要是org.activiti.runtime.api.connector.Connector 类型接口 。这个新的 Connector 接口是Java Delegates的自然演变,Activiti 7 Core将尝试通过将它们包装在Connector实现中来重用Java Delegates。

在org/activiti/training/activiti7apibasicprocessusertaskservicetaskevents包中创建一个名为connectors 的新子包。然后在这个新包中添加一个名为ServiceTask1Connector 的新Spring bean ,如下所示:

package org.activiti.training.activiti7apibasicprocessusertaskservicetaskevents.connectors;

import org.activiti.api.process.model.IntegrationContext;
import org.activiti.api.process.runtime.connector.Connector;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

@Service(value = "serviceTask1Impl")
public class ServiceTask1Connector implements Connector {
 private Logger logger = LoggerFactory.getLogger(ServiceTask1Connector.class);

 public IntegrationContext execute(IntegrationContext integrationContext) {
 logger.info("Some service task logic... [processInstanceId=" + integrationContext.getProcessInstanceId() + "]");

 return integrationContext;
 }
}

连接器 使用Bean名称自动连接到ProcessRuntime ,在此示例中为“ serviceTask1Impl ”。这个bean名称是从 我们的流程定义中的serviceTask元素的implementation 属性中获取的 :

<bpmn2:serviceTask id="ServiceTask_1wg38me" name="Service Task 1" implementation="serviceTask1Impl">

连接器收到 IntegrationContext 与流程实例的信息和过程变量,并返回一个修改后 IntegrationContext 与需要被映射回过程变量的结果。

现在,按照前面的描述打包并运行应用程序。当我们运行内存数据库时,我们之前启动的流程实例将在重新启动后消失,创建一个新的 http://localhost:8080/start-process?processDefinitionKey=sampleproc-e9b76ff9-6f70-42c9-8dee-f6116c533a6d。以用户 testuser 身份登录,以便我们可以始终保持相同的用户。

然后键入以下URL: http://localhost:8080 my-tasks,然后您应该得到如下所示的响应:

[
 {
 id: "79ce7445-fc4b-11e8-95e6-acde48001122",
 name: "User Task 1",
 status: "ASSIGNED",
 assignee: "testuser",
 createdDate: "2018-12-10T07:16:13.109+0000",
 priority: 50,
 processDefinitionId: "723ee071-fc4b-11e8-95e6-acde48001122",
 processInstanceId: "79cb3ff2-fc4b-11e8-95e6-acde48001122"
 }
]

记下任务ID,然后在 http://localhost:8080/complete-task?taskId=79ce7445-fc4b-11e8-95e6-acde48001122 调用中使用它来完成任务。这将使流程实例转换到下一个活动,在我们的例子中是一个服务任务。您应该在日志中看到以下内容:

2018-12-10 07:17:05.090 INFO 39199 --- [nio-8080-exec-7] oatacServiceTask1Connector:一些服务任务逻辑... [processInstanceId = 79cb3ff2-fc4b-11e8-95e6-acde48001122]

现在检查流程实例是否已完成将是好事。我们可以使用之前开发的 http://localhost:8080/process-instances 调用来实现。它应该返回一个空列表。

实现进程监听器和任务监听器

进程监听器和任务监听器传统上是在Activiti中使用专有扩展实现的。这些扩展还意味着代码将与流程执行同步运行。在云部署中哪个不好。在Activiti 7中,流程引擎会以异步方式发出我们可以监听和订阅的事件。

通过实现org.activiti.api.process.runtime.events.listener.ProcessRuntimeEventListener 接口创建进程监听器 。

在org/activiti/training/activiti7apibasicprocessusertaskservicetaskevents包中创建一个名为listeners 的新子包。然后在这个新包中添加一个名为MyProcessEventListener 的新Spring bean ,如下所示:

package org.activiti.training.activiti7apibasicprocessusertaskservicetaskevents.listeners;

import org.activiti.api.model.shared.event.RuntimeEvent;
import org.activiti.api.model.shared.event.VariableCreatedEvent;
import org.activiti.api.process.model.events.SequenceFlowTakenEvent;
import org.activiti.api.process.runtime.events.*;
import org.activiti.api.process.runtime.events.listener.ProcessRuntimeEventListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

@Service
public class MyProcessEventListener implements ProcessRuntimeEventListener {
 private Logger logger = LoggerFactory.getLogger(MyProcessEventListener.class);

 @Override
 public void onEvent(RuntimeEvent runtimeEvent) {

 if (runtimeEvent instanceof ProcessStartedEvent)
 logger.info("Do something, process is started: " + runtimeEvent.toString());
 else if (runtimeEvent instanceof ProcessCompletedEvent)
 logger.info("Do something, process is completed: " + runtimeEvent.toString());
 else if (runtimeEvent instanceof ProcessCancelledEvent)
 logger.info("Do something, process is cancelled: " + runtimeEvent.toString());
 else if (runtimeEvent instanceof ProcessSuspendedEvent)
 logger.info("Do something, process is suspended: " + runtimeEvent.toString());
 else if (runtimeEvent instanceof ProcessResumedEvent)
 logger.info("Do something, process is resumed: " + runtimeEvent.toString());
 else if (runtimeEvent instanceof ProcessCreatedEvent)
 logger.info("Do something, process is created: " + runtimeEvent.toString());
 else if (runtimeEvent instanceof SequenceFlowTakenEvent)
 logger.info("Do something, sequence flow is taken: " + runtimeEvent.toString());
 else if (runtimeEvent instanceof VariableCreatedEvent)
 logger.info("Do something, variable was created: " + runtimeEvent.toString());
 else
 logger.info("Unknown event: " + runtimeEvent.toString());

 }
}

通过实现ProcessRuntimeEventListener 接口,进程侦听器自动连接到ProcessRuntime 。监听器收到一个RuntimeEvent,其中包含有关该事件的所有信息。我们可以查看子类来确定事件的内容,例如ProcessCompletedEvent 。

以同样的方式,我们可以通过实现 org.activiti.api.task.runtime.events.listener.TaskRuntimeEventListener 接口来创建任务侦听器。

在这个新包中添加一个名为MyTaskEventListener 的新Spring bean ,如下所示:

package org.activiti.training.activiti7apibasicprocessusertaskservicetaskevents.listeners;

import org.activiti.api.model.shared.event.RuntimeEvent;
import org.activiti.api.task.model.Task;
import org.activiti.api.task.runtime.events.*;
import org.activiti.api.task.runtime.events.listener.TaskRuntimeEventListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

@Service
public class MyTaskEventListener implements TaskRuntimeEventListener {
 private Logger logger = LoggerFactory.getLogger(MyTaskEventListener.class);

 @Override
 public void onEvent(RuntimeEvent runtimeEvent) {

 if (runtimeEvent instanceof TaskActivatedEvent)
 logger.info("Do something, task is activated: " + runtimeEvent.toString());
 else if (runtimeEvent instanceof TaskAssignedEvent) {
 TaskAssignedEvent taskEvent = (TaskAssignedEvent)runtimeEvent;
 Task task = taskEvent.getEntity();
 logger.info("Do something, task is assigned: " + task.toString());
 } else if (runtimeEvent instanceof TaskCancelledEvent)
 logger.info("Do something, task is cancelled: " + runtimeEvent.toString());
 else if (runtimeEvent instanceof TaskCompletedEvent)
 logger.info("Do something, task is completed: " + runtimeEvent.toString());
 else if (runtimeEvent instanceof TaskCreatedEvent)
 logger.info("Do something, task is created: " + runtimeEvent.toString());
 else if (runtimeEvent instanceof TaskSuspendedEvent)
 logger.info("Do something, task is suspended: " + runtimeEvent.toString());
 else
 logger.info("Unknown event: " + runtimeEvent.toString());

 }
}

现在,按照前面的描述打包并运行应用程序。使用 http://localhost:8080/start-process?processDefinitionKey=sampleproc-e9b76ff9-6f70-42c9-8dee-f6116c533a6d创建一个新的流程实例。

在这一点上,我们应该看到很多事件记录:

2018-12-10 07:22:24.319  INFO 39199 --- [nio-8080-exec-4] o.a.t.a.l.MyProcessEventListener         : Do something, process is created: org.activiti.runtime.api.event.impl.ProcessCreatedEventImpl@3b555ed0

2018-12-10 07:22:24.321  INFO 39199 --- [nio-8080-exec-4] o.a.t.a.l.MyProcessEventListener         : Do something, process is started: org.activiti.runtime.api.event.impl.ProcessStartedEventImpl@45c34e86

2018-12-10 07:22:24.322  INFO 39199 --- [nio-8080-exec-4] o.a.t.a.l.MyProcessEventListener         : Unknown event: org.activiti.api.runtime.event.impl.BPMNActivityStartedEventImpl@70e78827

2018-12-10 07:22:24.323  INFO 39199 --- [nio-8080-exec-4] o.a.t.a.l.MyProcessEventListener         : Unknown event: org.activiti.api.runtime.event.impl.BPMNActivityCompletedEventImpl@2ea9f9f7

2018-12-10 07:22:24.325  INFO 39199 --- [nio-8080-exec-4] o.a.t.a.l.MyProcessEventListener         : Do something, sequence flow is taken: org.activiti.api.runtime.event.impl.SequenceFlowTakenImpl@3f1b812c

2018-12-10 07:22:24.325  INFO 39199 --- [nio-8080-exec-4] o.a.t.a.l.MyProcessEventListener         : Unknown event: org.activiti.api.runtime.event.impl.BPMNActivityStartedEventImpl@60734f37

2018-12-10 07:22:24.328  INFO 39199 --- [nio-8080-exec-4] o.a.t.a.listeners.MyTaskEventListener    : Do something, task is created: org.activiti.runtime.api.event.impl.TaskCreatedEventImpl@2c9945ae

2018-12-10 07:22:24.329  INFO 39199 --- [nio-8080-exec-4] o.a.t.a.listeners.MyTaskEventListener    : Do something, task is assigned: TaskImpl{id='5711101b-fc4c-11e8-95e6-acde48001122', owner='null', assignee='testuser', name='User Task 1', description='null', createdDate=Mon Dec 10 07:22:24 GMT 2018, claimedDate=null, dueDate=null, priority=50, processDefinitionId='723ee071-fc4b-11e8-95e6-acde48001122', processInstanceId='570ffea8-fc4c-11e8-95e6-acde48001122', parentTaskId='null', formKey='null', status=ASSIGNED}

当前页面是本站的「Google AMP」版。查看和发表评论请点击:完整版 »