1. Introduction
1.介绍
Mule is a Java-based product which provides an enterprise service bus (ESB) solutions. We can develop Mule applications using Anypoint Studio, an Eclipse plugin.
Mule是一个基于Java的产品,它提供了一个企业服务总线(ESB)解决方案。我们可以使用Anypoint Studio,一个Eclipse插件来开发Mule应用程序。
After a brief introduction to ESB’s and flows, we’ll talk about the different types of flows in Mule, and where we use each type.
在简单介绍了ESB和流之后,我们将讨论Mule中不同类型的流,以及我们在哪里使用每种类型。
2. Enterprise Service Bus (ESB)
2.企业服务总线(ESB)
An ESB is a mediating layer connecting independent pieces of software. These apps often use different protocols. Consequently, the ESB will take care of transforming and routing the data. This allows for the creation of decoupled services. Therefore, each service does not need to worry about how another service will consume its output.
ESB是一个连接独立软件片断的中介层。这些应用程序通常使用不同的协议。因此,ESB将负责数据的转换和路由。这允许创建解耦的服务。因此,每个服务不需要担心另一个服务将如何消费其输出。
It is the ESB’s job to make sure that the data is in the correct format. We can find more details about Mule ESB in our previous tutorial.
ESB的工作是确保数据的格式正确。我们可以在我们之前的教程中找到关于Mule ESB的更多细节。
Components are the building blocks of a Mule application. Thus, it’s these components that are responsible for transforming and routing data. We add components to a Mule application from the Mule Palette, on the right-hand side of Anypoint Studio:
组件是Mule应用程序的构建块。因此,正是这些组件负责转换和路由数据。我们从Anypoint Studio右侧的Mule Palette中向Mule应用程序添加组件。
Components are subsequently grouped into flows. A Mule application consists of one or more flows.
组件随后被分组为flow。一个Mule应用程序由一个或多个流组成。
3. What Is a Flow?
3.什么是流?
A flow is a connected collection of Mule components.
流是Mule组件的一个连接集合。
It usually consists of an inbound endpoint component (from where a message originates), and an outbound endpoint component. Therefore, the flow is responsible for the various processing stages in which the message may undergo.
它通常由一个入站端点组件(消息的来源地)和一个出站端点组件组成。因此,该流程负责消息可能经历的各种处理阶段。
Each flow may have a processing strategy as well as an exception handling strategy associated with it. A flow may also reference another flow using a flow reference component.
每个流可以有一个处理策略以及一个异常处理策略与之相关。一个流也可以使用流引用组件来引用另一个流。
There are three different types of flows in Mule:
在Mule中,有三种不同类型的流量。
- Subflows – a synchronous flow inheriting the processing and exception handling strategy of the parent flow
- Synchronous Flows – a synchronous flow with its processing and exception handling strategy
- Asynchronous Flows – an asynchronous flow with its processing and exception handling strategy
4. Subflows
4.子流程
We use subflows to group common logic. Subflows are processed synchronously; that is, execution of the calling flow halts until the subflow is complete.
我们使用子流程来分组共同的逻辑。子流程是同步处理的;也就是说,调用流程的执行会停止,直到子流程完成。
Specifically, we can add a subflow from the Mule Palette:
具体来说,我们可以从Mule Palette中添加一个子流程。
The subflow is called using a flow reference component:
子流程是使用流程参考组件调用的。
Additionally, subflows inherit the processing strategies and exception handling strategies of the calling flow. We can call a subflow from multiple different flows. Should we not wish to inherit these strategies, we could call a synchronous flow.
此外,子流程继承了调用流程的处理策略和异常处理策略。我们可以从多个不同的流中调用一个子流。如果我们不希望继承这些策略,我们可以调用一个同步流。
5. Synchronous Flows
5.同步流动
Like a subflow, a synchronous flow is also processed synchronously. This means that when we call a synchronous flow, it must complete before the parent flow resumes its execution.
与子流程一样,同步流程也是同步处理的。这意味着,当我们调用一个同步流时,它必须在父流恢复执行之前完成。
We add synchronous flows to our Mule application by adding regular flows; there is no “synchronous flow” component. We add a regular flow component:
我们通过添加常规流来为我们的Mule应用程序添加同步流;不存在 “同步流 “组件。我们添加一个常规的flow组件。
To call the synchronous flow, we again use a flow reference component:
为了调用同步流,我们再次使用一个流参考组件。
Unlike a subflow, it doesn’t inherit the processing and exception handling strategies of the calling flow. Consequently, the processing and exception handling strategies of the calling flow do not affect the behavior of this type of flow.
与子流程不同,它不继承调用流程的处理和异常处理策略。因此,调用流的处理和异常处理策略不影响这种类型的流的行为。
For these reasons, this type of flow is ideal for transactional processing since messages processed using synchronous flows execute on a single thread.
由于这些原因,这种类型的流是事务处理的理想选择,因为使用同步流处理的消息在单个线程上执行。
6. Asynchronous Flows
6.异步流
Asynchronous flows execute in parallel to the calling flow; i.e., they are processed asynchronously.
异步流与调用流并行执行;即,它们被异步处理。
We add asynchronous flows to the Mule application in the same way we add synchronous ones. So, what makes the flow asynchronous, is that we call it from within an asynchronous scope. We can do this by wrapping the flow reference component in an async component:
我们在Mule应用程序中添加异步流,与添加同步流的方式相同。因此,使流成为异步的,是我们从异步范围内调用它。我们可以通过在async组件中包装流引用组件来做到这一点。
Similarly to synchronous flows – they don’t inherit the processing and exception handling strategies of the calling flow.
与同步流类似 – 它们不继承调用流的处理和异常处理策略。
Additionally, messages processed using asynchronous flows execute on multiple threads, making this type of flow ideal for time-consuming tasks such as sending out emails or performing I/O operations.
此外,使用异步流处理的消息在多个线程上执行,使这种类型的流成为耗时任务的理想选择,如发送电子邮件或执行I/O操作。
7. Conclusion
7.结语
In this short tutorial, we discussed the characteristics of the different types of flows in Mule.
在这个简短的教程中,我们讨论了Mule中不同类型流量的特点。
For more information visit the official Mule website.
欲了解更多信息,请访问骡子的官方网站。