Introducción a Mule ESB

1. Información general

Mule ESB es un Enterprise Service Bus ligero basado en Java. Permite a los desarrolladores conectar varias aplicaciones intercambiando datos en diferentes formatos. Transporta datos en forma de mensaje.

Los ESB ofrecen potentes capacidades al proporcionar una serie de servicios, como:

  • Creación y alojamiento de servicios
  • Mediación de servicios
  • Enrutamiento de mensajes
  • Transformación de datos

Encontraremos que los ESB son útiles si necesitamos integrar varias aplicaciones juntas, o si tenemos la idea de agregar más aplicaciones en el futuro.

ESB también se utiliza para tratar con más de un tipo de protocolo de comunicación y cuando se requieren capacidades de enrutamiento de mensajes.

Creemos un proyecto de muestra en la Sección 5 usando AnyPoint Studio que está disponible para descargar aquí.

2. Estructura del mensaje de mula

En pocas palabras, el propósito principal de un ESB es mediar entre servicios y enrutar mensajes a varios puntos finales. Por lo tanto, debe lidiar con diferentes tipos de contenido o carga útil.

La estructura del mensaje se divide en dos partes:

  • El encabezado, quecontiene metadatos del mensaje
  • La carga útil, que contiene datos específicos de la empresa.

El mensaje está incrustado dentro de un objeto de mensaje. Podemos recuperar el objeto de mensaje del contexto. Podemos cambiar sus propiedades y carga útil utilizando transformadores y componentes Java personalizados dentro de un flujo de Mule.

Cada aplicación consta de uno o más flujos.

En un flujo, podemos usar componentes para acceder, filtrar o alterar un mensaje y sus diferentes propiedades.

Por ejemplo, podemos obtener una instancia de un mensaje usando el componente Java. Este componente implementa la clase de un rescatable de interfaz de org.mule.api.lifecycle paquete:

public Object onCall(MuleEventContext eventContext) throws Exception { MuleMessage message = eventContext.getMessage(); message.setPayload("Message payload is changed here."); return message; }

3. Propiedades y variables

Los metadatos de mensajes constan de propiedades. Las variables representan datos sobre un mensaje. La forma en que se aplican las propiedades y variables a lo largo del ciclo de vida del mensaje se define por sus alcances. Las propiedades pueden ser de dos tipos, según su alcance: entrantes y salientes.

Las propiedades entrantes contienen metadatos que evitan que los mensajes se codifiquen mientras atraviesan flujos. Las propiedades entrantes son inmutables y el usuario no puede modificarlas. Están presentes solo durante la duración del flujo; una vez que el mensaje sale del flujo, las propiedades entrantes ya no están allí.

Mule puede establecer automáticamente las propiedades de salida o un usuario puede establecerlas a través de la configuración del flujo. Estas propiedades son mutables. Se convierten en propiedades entrantes cuando un mensaje ingresa a otro flujo después de cruzar barreras de transporte.

Podemos establecer y obtener propiedades de entrada y salida respectivamente llamando a los métodos setter y getter asociados en sus respectivos ámbitos:

message.setProperty( "outboundKey", "outboundpropertyvalue", PropertyScope.OUTBOUND); String inboundProp = (String) message.getInboundProperty("outboundKey");

Hay dos tipos de variables disponibles para declarar en aplicaciones.

Una es la variable de flujo que es local a un flujo de Mule y está disponible a través del flujo, subflujos y flujos privados.

Las variables de sesión, una vez declaradas, están disponibles en toda la aplicación.

4. Barreras de transporte y reflujo

Las barreras de transporte son conectores HTTP, VM, JMS o conectores similares que requieren rutas o puntos finales para enrutar los mensajes. Las variables de flujo no están disponibles a través de las barreras de transporte, pero las variables de sesión están disponibles en todo el proyecto en todos los flujos.

Cuando necesitamos crear un flujo secundario o privado, podemos hacer referencia al flujo de un flujo principal u otro flujo utilizando el componente flow-ref . Tanto las variables de flujo como las variables de sesión están disponibles en subflujos y flujos privados a los que se hace referencia con flow-ref .

5. Proyecto de ejemplo

Creemos una aplicación en Anypoint Studio que contenga múltiples flujos, que se comunican entre ellos a través de conectores entrantes y salientes.

Veamos el primer flujo:

Podemos configurar un oyente HTTP como:

Los componentes de flujo deben estar dentro de un etiqueta. Entonces, un ejemplo de flujo con múltiples componentes es:

Dentro del flujo, proporcionamos una referencia a un escucha HTTP configurado. Luego, mantenemos un registrador para registrar la carga útil que recibe el oyente HTTP a través del método POST.

Después de eso, se coloca una clase de transformador Java personalizada, que transforma la carga útil después de recibir el mensaje:

public Object transformMessage( MuleMessage message, String outputEncoding) throws TransformerException { message.setPayload("Payload is transferred here."); message.setProperty( "outboundKey", "outboundpropertyvalue", PropertyScope.OUTBOUND); return message; }

La clase de transformador debe extender AbstractMessageTransformer . También estamos configurando una propiedad de salida dentro de la clase.

Ahora, ya hemos convertido la carga útil dentro del objeto de mensaje y lo hemos registrado en la consola usando logger. Estamos configurando una variable de flujo y una variable de sesión.

Finalmente, estamos enviando nuestra carga útil a través del conector de VM saliente. La ruta en el conector de VM determina el punto final de recepción:

El mensaje transportado y transformado por el flujo inicial llega a Flow1 a través de un punto final de VM entrante.

El componente Java recupera las propiedades de salida establecidas por el primer flujo y devuelve el objeto que se convierte en la carga útil del mensaje.

El método transformMessage () para esta tarea:

public Object transformMessage( MuleMessage message, String outputEncoding) throws TransformerException { return (String) message.getInboundProperty("outboundKey"); }

Luego, las variables de flujo y sesión se establecen en el segundo flujo. Después de eso, tenemos una referencia a Flow2 usando el componente flow-ref .

En Flow2, transformamos el mensaje usando la clase de componente Java y lo registramos en la consola. También hemos establecido una variable de flujo F3 .

Después de llamar a Flow2 usando flow-ref, Flow1 esperará a que el mensaje se procese en Flow2 .

Cualquier variable de flujo configurada en Flow1 y Flow2 estará disponible en ambos flujos, ya que estos flujos no están separados por ninguna barrera de transporte.

Finalmente, el mensaje se envía de vuelta al solicitante HTTP a través de VM. Configuramos todas las VM como solicitud-respuesta.

Podemos invocar esta aplicación desde cualquier cliente REST publicando cualquier dato JSON en el cuerpo. La URL será localhost: 8081 según lo configurado en el escucha HTTP.

6. Maven Archetype

We can build a Mule ESB project using Mulesoft's Maven archetype.

In Maven's settings.xml file, we first need to add the org.mule.tools plugin group:

 org.mule.tools 

Then, we need to add a profile tag that says where Maven should look for Mulesoft artifacts:

 Mule Org  true    mulesoft-releases MuleSoft Repository //repository-master.mulesoft.org/releases/ default   

Finally, we can create the project using mule-project-archetype:create:

mvn mule-project-archetype:create -DartifactId=muleesb -DmuleVersion=3.9.0

After configuring our project, we can create a deployable archive using mvn package.

After that, we'd deploy the archive into the apps folder of any standalone Mule server.

7. A Standalone Mule Server via MuleSoft's Maven Repository

As just noted, the project we just created requires a standalone Mule server.

If we don't already have one, we can edit our pom.xml to pull one from MuleSoft's Maven repository:

 org.mule.tools.maven mule-maven-plugin 2.2.1  standalone 3.9.0    deploy deploy  deploy    

8. Conclusion

In this article, we've gone through different necessary concepts of building as ESB application in Mule. We've created a sample project illustrating all the described concepts.

We can now start creating ESB application using Anypoint Studio to meet our various needs.

As usual, the complete project can be found over on GitHub.

1. Overview

Mule ESB is a lightweight Java-based Enterprise Service Bus. It allows developers to connect multiple applications together by exchanging data in different formats. It carries data in the form of a message.

ESBs offer powerful capabilities by providing a number of services, such as:

  • Service creation and hosting
  • Service mediation
  • Message routing
  • Data transformation

We'll find ESBs useful if we need to integrate multiple applications together, or if we have the notion of adding more applications in the future.

ESB is also used for dealing with more than one type of communication protocol and when message routing capabilities are required.

Let's create a sample project in Section 5 using AnyPoint Studio which is available for download here.

2. Mule Message Structure

Simply put, the primary purpose of an ESB is to mediate between services and route messages to various endpoints. So it needs to deal with different types of content or payload.

The message structure is divided into two parts:

  • The header, whichcontains message metadata
  • The payload, which contains business-specific data

The message is embedded within a message object. We can retrieve the message object from the context. We can change its properties and payload using custom Java components and transformers inside a Mule flow.

Each application consists of one or more flows.

In a flow, we can use components to access, filter or alter a message and its different properties.

For example, we can obtain an instance of a message using Java component. This component class implements a Callable interface from org.mule.api.lifecycle package:

public Object onCall(MuleEventContext eventContext) throws Exception { MuleMessage message = eventContext.getMessage(); message.setPayload("Message payload is changed here."); return message; }

3. Properties and Variables

Message metadata consists of properties. Variables represent data about a message. How properties and variables are applied across the message's life-cycle is defined by their scopes. Properties can be of two types, based on their scope: inbound and outbound.

Inbound properties contain metadata that prevents messages to become scrambled while traversing across flows. Inbound properties are immutable and cannot be altered by the user. They're present only for the duration of the flow – once the message exits the flow, inbound properties are no longer there.

Outbound properties can be set automatically by Mule, or a user can set them through flow configuration. These properties are mutable. They become inbound properties when a message enters another flow after crossing transport-barriers.

We can set and get outbound and inbound properties respectively by calling associated setter and getter methods in their respective scopes:

message.setProperty( "outboundKey", "outboundpropertyvalue", PropertyScope.OUTBOUND); String inboundProp = (String) message.getInboundProperty("outboundKey");

There are two types of variables available to declare in applications.

One is flow variable which is local to a Mule flow and available across the flow, sub-flows and private flows.

Session variables once declared become available across the entire application.

4. Transport Barriers and flow-ref

Transport barriers are HTTP-connectors, VMs, JMS or similar connectors that require paths or endpoints for messages to be routed. Flow variables aren't available across transport barriers, but session variables are available across the project in all flows.

When we need to create sub-flow or private flow, we can refer to the flow from a parent or another flow using flow-ref component. Both flow variables and session variables are available in sub-flows and private flows referred to using flow-ref.

5. Example Project

Let's create an application in Anypoint Studio that contains multiple flows, which communicate between themselves through inbound and outbound connectors.

Let's look at the first flow:

We can configure an HTTP listener as:

Flow components must be inside a tag. So, an example flow with multiple components is:

Inside the flow, we're providing a reference to a configured HTTP listener. Then we're keeping a logger to log the payload that HTTP listener is receiving through POST method.

After that, a custom Java transformer class is placed, that transforms the payload after receiving the message:

public Object transformMessage( MuleMessage message, String outputEncoding) throws TransformerException { message.setPayload("Payload is transferred here."); message.setProperty( "outboundKey", "outboundpropertyvalue", PropertyScope.OUTBOUND); return message; }

The transformer class must extend AbstractMessageTransformer. We're also setting an outbound property inside the class.

Now, we have already converted payload inside the message object, and have logged that in the console using logger. We're setting a flow variable and a session variable.

Finally, we are sending our payload through outbound VM connector. The path in VM connector determines the receiving endpoint:

The message carried and transformed by the initial flow reaches Flow1 through an inbound VM endpoint.

The Java component retrieves outbound properties set by the first flow and returns the object which becomes the message payload.

The transformMessage() method for this task:

public Object transformMessage( MuleMessage message, String outputEncoding) throws TransformerException { return (String) message.getInboundProperty("outboundKey"); }

Then, flow and session variables are set to the second flow. After that, we've got a reference to Flow2 using flow-ref component.

In Flow2, we've transformed the message using Java component class and logged it in the console. We've also set a flow variable F3.

After calling Flow2 using flow-ref, Flow1 will wait for the message to be processed in Flow2.

Any flow variable set in Flow1 and Flow2 will be available in both flows since these flows aren't separated by any transport barriers.

Finally, the message is sent back to the HTTP requester through VMs. We configured all VMs as request-response.

We can invoke this application from any REST client by posting any JSON data in the body. The URL will be localhost:8081 as configured in HTTP listener.

6. Maven Archetype

We can build a Mule ESB project using Mulesoft's Maven archetype.

In Maven's settings.xml file, we first need to add the org.mule.tools plugin group:

 org.mule.tools 

Then, we need to add a profile tag that says where Maven should look for Mulesoft artifacts:

 Mule Org  true    mulesoft-releases MuleSoft Repository //repository-master.mulesoft.org/releases/ default   

Finally, we can create the project using mule-project-archetype:create:

mvn mule-project-archetype:create -DartifactId=muleesb -DmuleVersion=3.9.0

After configuring our project, we can create a deployable archive using mvn package.

After that, we'd deploy the archive into the apps folder of any standalone Mule server.

7. A Standalone Mule Server via MuleSoft's Maven Repository

As just noted, the project we just created requires a standalone Mule server.

Si aún no tenemos uno, podemos editar nuestro pom.xml para extraer uno del repositorio Maven de MuleSoft:

 org.mule.tools.maven mule-maven-plugin 2.2.1  standalone 3.9.0    deploy deploy  deploy    

8. Conclusión

En este artículo, hemos analizado diferentes conceptos necesarios de construcción como aplicación ESB en Mule. Hemos creado un proyecto de muestra que ilustra todos los conceptos descritos.

Ahora podemos comenzar a crear una aplicación ESB usando Anypoint Studio para satisfacer nuestras diversas necesidades.

Como de costumbre, el proyecto completo se puede encontrar en GitHub.