Reading Time: 5 minutes

In this article, we will see how Mule can intercept messages on the TCP/IP socket for real-time communication. You will first receive messages on the TCP/IP socket and then transform the messages from byte to object, then from object to XML, and then, finally, from XML to JSON––all using out-of-the-box transformers.

The TCP transport allows users to send or receive messages over TCP connections. TCP is a layer above the IP. It is used to implement a number of other reliable protocols, such as HTTP and FTP. If you want to use the TCP transport directly, then you need a specific protocol to read 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 scenarios where transactions are in real-time.

Scenario

latest report
Learn why we are the Leaders in management and

In this blog post, we will first send the message from the Packet Sender application, and then Mule will receive the message on the TCP/IP socket. We will then transform the message from byte to object, then from object to XML, and then from XML to JSON––all using Anypoint Studio transformers.

Image title

1. First, configure TCP Transport as follows:

a. The exchange pattern will be one-way, because we are not sending any acknowledgment back. Then, we need to input the host and port information and define the connector configuration:

Image title
Image title

b. Set the MIME type to text/plain and set the response timeout to default. 

Image title

c. Check the “Do not use a Reconnection strategy” radiobox.

d. Now, choose the appropriate protocol according to your requirement and check the “Keep Alive” box as shown below.

Image title
Image title

2. Set the MIME type to “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. Set the MIME type to “text/JSON” in the XML to JSON transformer.

5. Configure all Logger components Message value to be  #[payload]  in order to log received payloads in the console.

6. Send the packet from the Packet Sender application on the TCP socket according to the configuration below. Set the packet payload to “MULE ESB TEST PACKET.”

Image title

7. Run the application in debug mode so that you can stop the application and easily see the in-flight conversion of data, while the data moves from one component to another. 

8. After passing the Byte Array to the Object component, you will receive the payload as a text/plain payload. 

Image title
Image title

9. After passing the Object to XML component, the payload will convert to XML format. 

10. After passing the XML to JSON component, the payload will convert from XML to JSON format. 

Image title
Image title

11. Finally, you can see all the data format changes in the console. 

Please refer to this page for more details and information.

This article was originally published on Dzone