Over a million developers have joined DZone.

Intercepting Messages on TCP/IP in Mule

DZone's Guide to

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.

· Integration Zone ·
Free Resource

How to Transform Your Business in the Digital Age: Learn how organizations are re-architecting their integration strategy with data-driven app integration for true digital transformation.

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.

Image title

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:Image title

b. The MIME type will be text/plain and response timeout will be default. Image title

c. The first radio box will be checked "Do not use a Reconnection strategy."Image title

d. Choose the protocol as per your requirement and keep the socket alive by checking the box as shown below.Image title

2. The MIME type will be "text/plain" in the Byte Array to Object transformer. 
Image title

3. The MIME type will be "text/xml" in the Object to XML transformer.Image title

4. The MIME type will be "text/JSON" in the XML to JSON transformer.Image title

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."Image title

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. Image title

9. After passing the Object to XML component, the payload is converted to XML format. Image title

10. After passing the XML to JSON component, the payload is converted from XML to JSON format. Image title

11. All the data format changes can be seen in the console, too. Image title

Please refer to this MuleSoft page for more details and information.

Build and deploy API integrations 7x faster. Try the Cloud Elements 100% RESTful platform for 30 days free. Access your trial here.

mule ,integration ,tcp ,anypoint studio

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}