JAVA Developer Guide to begin with Wire-Tap Enterprise Integration Pattern

Ritvik Singh Chauhan
Javarevisited
Published in
4 min readMay 21, 2022

Wire-tapping is the term that is used by surveillance agencies for monitoring phone calls. You are likely aware of this term if you have seen phone-tapping or signal-intercepting activities in most hacking or sci-fi movies/series. A similar concept is also used by Software Enterprises to monitor or intercept data flowing between two services. The Wire-Tap pattern helps in solving the problem of intercepting the data without breaking the flow between services.

What is the Wire-Tap Pattern?

Wire-Tap is one of the design patterns from the book “Enterprise Integration Patterns: Designing, Building, and Deploying Messaging Solutions”, which describes the simple router (also known as Tee) duplicating the message from single input to two output channels.

In most cases within software systems, there is a need to monitor or copy the data flowing through. It is usually achieved by intercepting the data and redirecting it to a different location like monitoring dashboards, consoles, filesystems, or the database. Such functionality mustn’t change the original data and interrupt the flow of the services.

Wire-Tap Pattern JAVA Example

Suppose we are building a Message Eavesdropping System, which intercepts data from Switch. In this example, I am consuming data from a source queue and pushing it to a destination queue using Switch. Added a wiretapping service to intercept the data at the message processor switch.

Message Definition:

For Message model, I have id, text and messageDate fields. Overridden toString method to return JSON similar string for output.

Sender Service:

In the sender service thread, generating Message objects (using getMessage method) and pushing those to queue in the run method. Here messageId field is being used to generate a unique key for each generated Message object. The random field is being used to generate random text for the Message objects.

Receiver Service:

In receiver service thread, consuming Message objects from queue in run method.

Message Processor Switch:

In message processor thread, pulling Message objects from source queue and pushing those to destination queue, but calling wiretapService object’s wiretap method in between to intercept the message.

Wiretap Service:

In the wiretapping service thread, whenever the wiretap method is called with the Message object, creates a deep-cloned Message using the MessageCloneUtil utility class and pushes it to its queue to further processing.

Message Cloning Utility:

As we have immutable fields id as long and text as String in Message objects, so while deep cloning, I am using them as it is. But creating a new Date object with the same value as the original input.

Wiretap Pattern in Action:

In the demo code below, created two queues for creating services. Used both of them to create a MessageProcessorSwitch thread, only the source queue to create a SenderService thread, and only the destination queue to create a ReceiverService thread. Whenever the SenderService thread is pushing a Message to the source queue, both threads (ReceiverService and WiretapService) are getting data.

Source Code Available at: https://github.com/s3c-d43m0n/Desing-Patterns-in-JAVA/tree/main/Integration/Wiretap

Key Points in the Implementation

  • Message object needs to be deep-cloned within the wiretap method or else if there is a change in the same Message object in downstream systems, then original content might get lost.
  • The existing flow between two services must not be interrupted while implementing the pattern. Here WiretapService is defined as a private field in MessageProcessorSwitch class, as we don't want to expose it to other resources.
  • Here I have used BlockingQueue objects as thread-safe queues as I am having multiple threads pulling/pushing Message objects. We can change based on the requirements.
  • Here I have used QUEUES for the Consumer/Producer scenario. Most of the high-end software systems are using queueing solutions such as JMS or KAFKA when dealing specially with Microservices.

What’s your favorite design pattern or if you’d like to understand better any other pattern or topic, feel free to reach out to me on LinkedIn or Google Form and I’ll try to cover it next time!

Please share this with all your Medium friends and hit that👏 button below to spread it around even more. Please follow me for future updates. Thanks for reading.

--

--

Ritvik Singh Chauhan
Javarevisited

Tech Enthusiast Software Developer focused on Backend Engineering. Get notified when I publish: https://medium.com/subscribe/@ritvik.singh.chauhan