Tuesday 8 January 2019

Camel: wiretapping


wiretap pattern is used to inspect the messages that are flowing through a system.

How can we inspect messages using wiretapping?


Camel provide wiretap method, using this method you can transfer the message to wiretapping destination without affecting the actual route.

Example
from("file:C:\\Users\\Public\\demo?noop=true")
         .wireTap(wireTappingQ)
         .to(ordersQ);

Let’s implement an example using activeMq and camel.

Setup ActiveMQ
I would recommend you to go through my below post to setup the activeMQ.

Once you setup activeMQ, loginto the admin console by hitting below url.

Use below credentials to login to the admin console.
UserName: admin
Password: admin

You can see below kind of user interface.


Setup Eclipse Maven Project
Create new maven project 'camelWiretapping'. Project structure looks like below.



Open pom.xml and update maven dependencies.

pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>camelWiretapping</groupId>
	<artifactId>camelWiretapping</artifactId>
	<version>1</version>

	<dependencies>
		<!-- https://mvnrepository.com/artifact/org.apache.camel/camel-jms -->
		<dependency>
			<groupId>org.apache.camel</groupId>
			<artifactId>camel-jms</artifactId>
			<version>2.22.1</version>
		</dependency>

		<!-- https://mvnrepository.com/artifact/org.apache.camel/camel-core -->
		<dependency>
			<groupId>org.apache.camel</groupId>
			<artifactId>camel-core</artifactId>
			<version>2.22.1</version>
		</dependency>

		<!-- https://mvnrepository.com/artifact/org.apache.activemq/activemq-all -->
		<dependency>
			<groupId>org.apache.activemq</groupId>
			<artifactId>activemq-all</artifactId>
			<version>5.15.6</version>
		</dependency>

	</dependencies>
</project>

Create a package ‘com.sample.app.routes’ and define the class FileCopyRoute.java


FileCopyRoute.java
package com.sample.app.routes;

import org.apache.camel.builder.RouteBuilder;

public class FileCopyRoute extends RouteBuilder {

	@Override
	public void configure() throws Exception {
		String ordersQ = "demoJMS:queue:OrdersQ";
		String wireTappingQ = "demoJMS:queue:WireTappingQ";

		from("file:C:\\Users\\Public\\demo?noop=true").wireTap(wireTappingQ).to(ordersQ);
	}
}


Define the class Application.java in ‘com.sample’app’.

Application.java
package com.sample.app;

import java.util.concurrent.TimeUnit;

import javax.jms.ConnectionFactory;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.camel.CamelContext;
import org.apache.camel.component.jms.JmsComponent;
import org.apache.camel.impl.DefaultCamelContext;

import com.sample.app.routes.FileCopyRoute;

public class Application {
	public static void main(String args[]) throws Exception {
		CamelContext context = new DefaultCamelContext();

		ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://localhost:61616");

		context.addComponent("demoJMS", JmsComponent.jmsComponentAutoAcknowledge(connectionFactory));

		context.addRoutes(new FileCopyRoute());

		context.start();

		TimeUnit.MINUTES.sleep(1);

		context.stop();
	}
}

‘C:\Users\Public\demo’ directory has below documents.

ABC Quotation for interios.txt
ABC sales Q1.txt
Homio Orders.docx
XYZ Corp orders.xlsx


When I ran Application.java, I can see 4 messages are enqueued to both OrdersQ and WireTappingQ.





Previous                                                 Next                                                 Home<

No comments:

Post a Comment