When sending information from one system to other system,
other system may require more information apart from the information send by
source system.
For example, there is an order managing system, which
send <orderId, customerId> details to the destination system. But
destination system may require further information like customer address, phone
number to deliver the order. But order management system pass only <orderId,
customeId>, to get more information, we should contact ‘customer management
system’ which can give the user address and phone number. Like this there are
many scenarios in real world application development.
Apache camel, provides two APIs to implement Enrich
pattern.
a.
pollEnrich
b.
enrich
pollEnrich
Merge data from another source using consumer
enrich
Merge data from another source using producer.
Let’s implement an application.
Orders folder contains order.json files.
Customer folder contains customer address.
Our task is to enrich the order message by merging the
customer data.
For the simplicity purpose, I assumed Orders contains
only one order file (order.json) and customer folder contains only one file
(customer.json).
Follow below step-by-step procedure to implement the
application.
Step 1: Create new
maven project ‘camelDataEnrich’.
Project structure looks like below.
Step 2: Update
pom.xml with 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>camelDataEnrich</groupId> <artifactId>camelDataEnrich</artifactId> <version>1</version> <dependencies> <!-- 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/com.google.code.gson/gson --> <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.8.5</version> </dependency> </dependencies> </project>
Step 3: Create new
package ‘com.sample.app.model’ and define the classes Order, Customer,
CustomerOrder.
Order.java
package com.sample.app.model; public class Order { private String orderId; private String prodId; private int noOfItems; private int customerId; public String getOrderId() { return orderId; } public void setOrderId(String orderId) { this.orderId = orderId; } public String getProdId() { return prodId; } public void setProdId(String prodId) { this.prodId = prodId; } public int getNoOfItems() { return noOfItems; } public void setNoOfItems(int noOfItems) { this.noOfItems = noOfItems; } public int getCustomerId() { return customerId; } public void setCustomerId(int customerId) { this.customerId = customerId; } }
Customer.java
package com.sample.app.model; public class Customer { private String name; private String city; private String country; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } public String getCountry() { return country; } public void setCountry(String country) { this.country = country; } }
CustomerOrder.java
package com.sample.app.model; public class CustomerOrder { private Order order; private Customer customer; public Order getOrder() { return order; } public void setOrder(Order order) { this.order = order; } public Customer getCustomer() { return customer; } public void setCustomer(Customer customer) { this.customer = customer; } }
Step 4: Create a
package ‘com.sample.app.routes’ and define the class FileCopyRoute.
FileCopyRoute.java
package com.sample.app.routes; import org.apache.camel.Exchange; import org.apache.camel.builder.RouteBuilder; import org.apache.camel.processor.aggregate.AggregationStrategy; import com.google.gson.Gson; import com.sample.app.model.Customer; import com.sample.app.model.CustomerOrder; import com.sample.app.model.Order; public class FileCopyRoute extends RouteBuilder { private static Gson gson = new Gson(); @Override public void configure() throws Exception { String customersFolder = "file:C:\\Users\\Public\\Customers?noop=true"; String ordersFolder = "file:C:\\Users\\Public\\Orders?noop=true"; String customerOrders = "file:C:\\Users\\Public\\CustomerOrders"; AggregationStrategy aggregationStrategy = new AggregationStrategy() { // oldExchnage points to message ordersFolder // newExchange points to message in customerFolder @Override public Exchange aggregate(Exchange oldExchange, Exchange newExchange) { String orderInfo = oldExchange.getIn().getBody(String.class); String customerInfo = newExchange.getIn().getBody(String.class); System.out.println("\nOrder Information" + orderInfo); System.out.println("\nCustomer Information : " + customerInfo); Order order = gson.fromJson(orderInfo, Order.class); Customer customer = gson.fromJson(customerInfo, Customer.class); CustomerOrder custOrder = new CustomerOrder(); custOrder.setOrder(order); custOrder.setCustomer(customer); String enrichedData = gson.toJson(custOrder); System.out.println("\nEnriched Information : " + enrichedData); oldExchange.getIn().setBody(enrichedData); return oldExchange; } }; from(ordersFolder).pollEnrich(customersFolder, aggregationStrategy).to(customerOrders); } }
Step 5: Define the
class Application in com.sample.app package.
Application.java
package com.sample.app; import java.util.concurrent.TimeUnit; import org.apache.camel.CamelContext; 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(); context.addRoutes(new FileCopyRoute()); context.start(); TimeUnit.MINUTES.sleep(1); context.stop(); } }
C:\\Users\\Public\\Customers contain customer.json
C:\\Users\\Public\\Orders contain order.json
order.json
{ "orderId" : "Ord11279327", "prodId" : "Prod342", "noOfItems" : 5, "customerId" : 549 }
customer.json
{ "name":"Krishna", "city":"Bangalore", "country":"India" }
Run Application.java. You can see below messages in
console.
Order Information{
"orderId"
: "Ord11279327",
"prodId"
: "Prod342",
"noOfItems"
: 5,
customerId
: 549
}
Customer Information : {
"name":"Krishna",
"city":"Bangalore",
"country":"India"
}
Enriched Information :
{"order":{"orderId":"Ord11279327","prodId":"Prod342","noOfItems":5,"customerId":549},"customer":{"name":"Krishna","city":"Bangalore","country":"India"}}
Project structure looks like below.
No comments:
Post a Comment