Intercepting Messages on TCP/IP in Mule
This tutorial will walk you through the steps of setting Mule to transform and intercept messages in real time with Anypoint Studio.
Join the DZone community and get the full member experience.Join For Free
In this article, we will see how Mule can intercept messages on the TCP/IP socket for real-time communication. Messages will be received on the TCP/IP socket and then will be transformed from byte to object, then object to XML, and then from XML to JSON using Mule out-of-box transformers.
The TCP transport allows sending or receiving messages over TCP connections. TCP is a layer over IP and is used to implement many other reliable protocols such as HTTP and FTP. If you want to use the TCP transport directly then you require a specific protocol for reading the message payload that is not supported by one of these higher level protocols. This is often the case when communicating with legacy or native system applications that don’t support web services. TCP transport is also used in business scenarios where the nature of transactions is in real time.
The message will be sent from the Packet Sender application and Mule will receive the message on TCP/IP socket and then it will be transformed from byte to object, then from object to XML, and then from XML to JSON using Anypoint Studio transformers.
1. TCP Transport will be configured as follows.
a. The exchange pattern will be one-way as we are not sending any acknowledgment back. Then the host and port need to be given. Then connector configuration needs to be defined:
b. The MIME type will be text/plain and response timeout will be default.
c. The first radio box will be checked "Do not use a Reconnection strategy."
d. Choose the protocol as per your requirement and keep the socket alive by checking the box as shown below.
2. The MIME type will be "text/plain" in the Byte Array to Object transformer.
3. The MIME type will be "text/xml" in the Object to XML transformer.
4. The MIME type will be "text/JSON" in the XML to JSON transformer.
5. All logger components will be configured as
#[payload] to log received payloads in the console.
6. Send the packet from the Packet Sender application on the TCP socket for the below configuration. Packet payload is "MULE ESB TEST PACKET."
7. Run the application in debug mode so that we can stop the application and can see the in-flight conversion of data while going from one component to another.
8. After passing the Byte Array to Object component, the payload is received as text/plain payload.
9. After passing the Object to XML component, the payload is converted to XML format.
10. After passing the XML to JSON component, the payload is converted from XML to JSON format.
11. All the data format changes can be seen in the console, too.
Please refer to this MuleSoft page for more details and information.
Opinions expressed by DZone contributors are their own.