This is Part 2 of an ongoing series of blogs explaining the EIP patterns (Enterprise Integration Patterns) in Apache camel. This blog is going to take you a step ahead and help you know more about the various EIP patterns such as WireTape, Multicast, and RecipientList EIP pattern
WireTap pattern
A wireTap pattern is used to filter messages based on content and read data from a message. WireTap allows routing the messages to a separate location while they are being routed to the ultimate destination. Additionally, WireTap is the fixed Recipient List with two output channels. It consumes messages off the input channel and publishes the unmodified messages to both output channels. The WireTap uses a thread collection to process the tapped messages. All messages are sent as Event Messages and run in parallel with the original message.
The wireTap sustains two different approaches to tapping an exchange:
- copy of the original exchange.
- new exchange instance to customize the tapped exchange.
1. Tap a copy of the original exchange
No filtering/modification is made in the original message
from("direct:start")
.to("log:foo")
.wireTap("direct:tap")
.to("mock:result");
2. Tap and customize the original exchange:
Basically, they are following two approaches:
- Processor approach
- Expression approach
– Processor approach: Using a processor gives you full power over how the exchange is populated as you can set properties, headers, and so on.
from("direct:start")
.wireTap("direct:foo", new Processor() {
public void new_process(Exchange ex) throws Exception {
ex.getIn().setHeader("foo", "bar");
}
}).to("mock:result");
from("direct:foo").to("mock:foo");
– Expression approach: The expression approach can be used to modify the In message body.
from("direct:start")
.wireTap("direct:foo", constant("Hello World"))
.to("mock:result");
from("direct:foo").to("mock:foo");
If you overuse wireTap to broadcast the output to multiple destinations. Subsequently, Multicasting or RecipientList is suitable for broadcasting.
Multicast pattern (Static RecipientList)
The multicast pattern sends the same message to a list of recipients. In addition, Regular or collection expressions define a recipient list. Below are the several ways of multicasting:
Multicasting Sequentially
To use multicasting, call multicast()
in the Java DSL and then pass the destinations to the to()
method. Below, we consume message from direct:start and it is multicast to three different destinations direct:x
, direct:y
, and direct:z
.
from("direct:start")
.multicast()
.to("direct:x", "direct:y", "direct:z");
or by multiple calls to to()
methods in sequence.
from("direct:start")
.multicast()
.to("direct:x")
.to("direct:y")
.to("direct:z");
Multicasting With Parallel Processing
A message is transmitted to multiple endpoints at the same time using parallel processing.
from("direct:start")
.multicast().parallelProcessing()
.to("direct:x", "direct:y", "direct:z");
Multicasting With Aggregation Strategy
An AggregationStrategy is for aggregating all the reply messages.
from("direct:start")
.multicast(new MyAggregationStrategy())
.parallelProcessing().to("direct:x", "direct:y", "direct:z")
.end()
.to("mock:result");
RecipientList (Dynamic RecipientList)
If you want to execute a task or set of tasks on a variable list of entities, the RecipientList pattern may be helpful. This pattern allows you to specify a list of recipients, and then run your workflow against all those entities in parallel. Further, To avoid sending confidential data to the wrong customer, one can use the recipient list pattern. Works with any type and amount of data. Basically, The recipients are specified in the message header and/or a property file (properties files) or as part of the body of the message.
Below, we consume messages from direct:start
and it is multicast to three different destinations direct:a
, direct:b, and direct:c
.
from("direct:start")
.multicast().to("direct:a", "direct:b", "direct:c");
Sending to multiple recipients in parallel
from("direct:start")
.recipientList(header("Header_Name")).parallelProcessing();
Stop continuing in case one recipient failed
from("direct:start")
.recipientList(header("Header_Name")).stopOnException();
Ignore invalid endpoints
from("direct:start")
.recipientList(header("Header_Name")).ignoreInvalidEndpoints();
Using custom AggregationStrategy
from("direct:start")
.recipientList(header("Header_Name"))
.aggregationStrategy(new UserDefinedAggStrategy())
.to("direct:last");
Conclusion
In conclusion, you get an idea about the EIP pattern in Apache Camel. Stay tuned for the next part related to more EIP patterns such as Content-Based Routing, Aggregation, Splitter, Message filter
Read Apache-Camel Manual for more knowledge.
Meanwhile, visit Knoldus Blogs to gain more information about different technologies.