March 2010: Hiding The Middleware from Your Domain Code With Camel

Hiding The Middleware from Your Domain Code With Camel

By James Carr, OCI Software Engineer

March 2010


Introduction

A very important aspect of writing software is trying to write a domain that focuses on the actual domain concepts as much as possible. That is, we want our domain logic to only deal in terms of the domain rather than dealing in fancy enterprise terms like SOAP, JMS, or REST. By keeping the middleware code hidden from the application code, the application is more flexible and easier to change, and Apache Camel helps achieve that without having to resort to using a full blown Enterprise Service Bus. Using Camel, I'll show how you can take two objects that collaborate in the same thread, move them to separate threads, and even separate them altogether behind web services or JMS and continue to work with the same API and not even care about the underlying transport.

The Domain

For the sake of simplicity the domain has purposefully been kept small, limited to just three objects to represent an system that takes an order of some sort (let's call it an OrderSite) which performs some basic business logic and creates a ShippingOrder to send to a Warehouse.

  1. // src/main/java/com/ociweb/example/domain/OrderSite.java
  2. public class OrderSite {
  3. private Warehouse warehouse;
  4.  
  5. public void process(Order order) {
  6. ShippingOrder shippingOrder =
  7. new ShippingOrder().forPurchase(order);
  8. shippingOrder.shipsOn(calculateShippingDate(shippingOrder));
  9. warehouse.shipProduct(shippingOrder);
  10. }
  11.  
  12. private DateMidnight calculateShippingDate
  13. (ShippingOrder shippingOrder) {
  14. return shippingOrder.getQuantity() < 10 ? oneDayFromNow()
  15. : threeDaysFromNow();
  16. }
  17.  
  18. private DateMidnight oneDayFromNow() {
  19. return new DateTime().plusDays(1).toDateMidnight();
  20. }
  21.  
  22. private DateMidnight threeDaysFromNow() {
  23. return new DateTime().plusDays(3).toDateMidnight();
  24. }
  25.  
  26. public void setWarehouse(Warehouse warehouse) {
  27. this.warehouse = warehouse;
  28. }
  29. }
  30.  
  1. // src/main/java/com/ociweb/example/domain/Warehouse.java
  2. public interface Warehouse {
  3. void shipProduct(ShippingOrder order);
  4. }
  1. // src/main/java/com/ociweb/example/domain/ShippingOrder.java
  2. import org.joda.time.DateMidnight;
  3.  
  4. public class ShippingOrder {
  5.  
  6. private int quantity;
  7. private String product;
  8. private DateMidnight date;
  9.  
  10. public Integer getQuantity() {
  11. return quantity;
  12. }
  13.  
  14. public String getProduct() {
  15. return product;
  16. }
  17.  
  18. public ShippingOrder forPurchase(Order order) {
  19. this.quantity= order.getQuantity();
  20. this.product = order.getProductName();
  21. return this;
  22. }
  23.  
  24. public DateMidnight getExpectedShipDate() {
  25. return date;
  26. }
  27.  
  28. public void shipsOn(DateMidnight date) {
  29. this.date = date;
  30. }
  31. }
  1. // src/main/java/com/ociweb/example/domain/Order.java
  2. public class Order {
  3. private final int quantity;
  4. private final String productName;
  5.  
  6. public Order(int quantity, String productName) {
  7. this.quantity = quantity;
  8. this.productName = productName;
  9. }
  10.  
  11. public int getQuantity() {
  12. return quantity;
  13. }
  14.  
  15. public String getProductName() {
  16. return productName;
  17. }
  18. }

We'll also include an implementation of Warehouse named SprocketWarehouse that simply prints the order received and puts the thread to sleep for 1 second to simulate a time consuming process.

  1. // src/main/java/com/ociweb/example/domain/SprocketWarehouse.java
  2. public class SprocketWarehouse implements Warehouse {
  3. private static final String MESSAGE =
  4. "Recieved Order for %d %s that will ship on %s.%n";
  5. public void shipProduct(ShippingOrder order) {
  6. System.out.printf(MESSAGE,
  7. order.getQuantity(),
  8. order.getProduct(),
  9. order.getExpectedShipDate().
  10. toString("MM/dd/yyyy"));
  11. try {
  12. Thread.sleep(1000);
  13. } catch (InterruptedException e) {}
  14. }
  15. }

These objects will remain largely unchanged throughout the article; the essence is to illustrate keeping your domain code free from any middleware specific transports.

Setting Up Camel

Luckily, Camel comes with a very rich set of configuration options ranging from using Spring, Guice, or just use plain old Java objects to configure itself. But before jumping in, let's first review some of the terminology I'll use:

Camel Context
     If you're familiar with Spring, you can picture the Camel context as being similar to the application context. Tt's the heart of Camel and is what you'll use to manage rules, routes, and endpoints that you'll register. You can also have multiple Camel context instances within the same VM.
Endpoint
     An endpoint is essentially a true-to-form implementation of MessageEndpoint from the book Enterprise Integration Patterns. In Camel, an endpoint represents a destination (e.g. a JMS queue, a file, a web service, or even an irc channel). Endpoints are defined in Camel using Components that work as factories for endpoints.
Route
    A Route defines a specialized set of routing rules to route a message within the Camel context. Camel allows users to define routes a number of ways, using a Java DSL, an xml DSL, and even a highly readable Scala DSL.

With all this in mind, let's set up a Spring based configuration that configures a Camel context to route a message from the OrderSite to the SprocketFactory using the direct component, which directly calls the object at the end of the endpoint.

  1. <!-- src/main/resources/com/ociweb/example/application-context.xml -->
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:camel="http://camel.apache.org/schema/spring"
  5. xsi:schemaLocation="
  6. http://www.springframework.org/schema/beans
  7. http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
  8. http://camel.apache.org/schema/spring
  9. http://camel.apache.org/schema/spring/camel-spring.xsd">
  10. <camel:camelContext id="context">
  11. <camel:proxy id="warehouseProxy"
  12. serviceUrl="warehouse"
  13. serviceInterface="com.ociweb.example.domain.Warehouse" ></camel:proxy>
  14. <camel:endpoint id="warehouse" uri="direct:warehouse"></camel:endpoint>
  15. <camel:route>
  16. <camel:from ref="warehouse"></camel:from>
  17. <camel:to uri="bean:warehouseImpl"></camel:to>
  18. </camel:route>
  19. </camel:camelContext>
  20.  
  21.  
  22. <bean id="warehouseImpl"
  23. class="com.ociweb.example.domain.SprocketWarehouse" ></bean>
  24.  
  25. <bean id="orderSite"
  26. class="com.ociweb.example.domain.OrderSite">
  27. <property name="warehouse" ref="warehouseProxy"></property>
  28. </bean>
  29. </beans>

This can look a little complicated with spring.xml, but it gets better. [Line 10] is the camelContext, which is where we'll store our routes, endpoints, and proxies. [Line 11] is a Camel proxy bean, which uses Spring Remoting under the hood to create a dynamic proxy for the Warehouse interface that we can inject into OrderSite to treat it like any other Java object. The serviceUrl attribute is a reference to the endpoint for this proxy, which in this case we name "warehouse", which references the id of the endpoint we've defined. [Line 14] specifies an endpoint element to specify a uri for the endpoint, set to "direct:warehouse". This means that any messages received will call a direct invocation on whatever consumers are attached to the endpoint. [Line 15] specifies a route that says from that endpoint we should call whatever is defined by to, which points to the bean definition for warehouse [Line 22]. And in [Line 25] we do the usual chore of injecting the proxy bean on OrderSite.

One interesting point to discuss is how the bean component works. You might be wondering how it knows which method to call. In this case SprocketWarehouse contained only one method, so Camel defaulted to calling that method. When there are multiple methods, it will try to call whichever method matches the type. Alternatively, you can also specify which method you want invoked by passing a method parameter to it, for example: bean:sprocketWarehouse?method=shipProduct.

Seeing It In Action

Now to see all this work, let's write a simple class to load up the context, lookup the OrderSite and submit a few orders:

  1. import org.springframework.context.support.ClassPathXmlApplicationContext;
  2.  
  3. import com.ociweb.example.domain.Order;
  4. import com.ociweb.example.domain.OrderSite;
  5.  
  6. public class PojoExampleWithSpring {
  7. public static void main(String... args){
  8. ClassPathXmlApplicationContext ctx =
  9. new ClassPathXmlApplicationContext(
  10. "classpath:com/ociweb/example/pojo-example.xml");
  11. OrderSite site = (OrderSite) ctx.getBean("orderSite");
  12.  
  13. site.process(new Order(9, "Sprockets"));
  14. site.process(new Order(10, "Sprockets"));
  15. site.process(new Order(13, "Sprockets"));
  16. site.process(new Order(225, "Sprockets"));
  17. }
  18. }

Now if we run this we'll get what we'd expect; each shipping order printed out as they are received, in the order they were sent, with a bit of a lag between each order being printed due to the thread sleeping for one second. Nothing fancy, and at this point you could even argue we overcomplicated having one object simply call another.

Placing The Dependency In Another Thread

We've decided it would be nice if we could multi-thread our order process. Rather than having to wait for each order to be processed sequentially, we instead want each Warehouse to handle the order in its own thread. Thankfully, this is an easy task to move towards using our existing setup. We just need to modify our camelContext a bit:

  1. <camel:camelContext id="context">
  2. <camel:proxy id="warehouseProxy" serviceUrl="warehouse"
  3. serviceInterface="com.ociweb.example.domain.Warehouse" ></camel:proxy>
  4. <camel:endpoint id="warehouse"
  5. uri="seda:warehouse?concurrentConsumers=4&waitForTaskToComplete=Never"></camel:endpoint>
  6. <camel:route>
  7. <camel:from ref="warehouse"></camel:from>
  8. <camel:to uri="bean:warehouseImpl"></camel:to>
  9. </camel:route>
  10. </camel:camelContext>

All that is done here is we change the uri to use the seda component which provides us with better asynchronous invocations as well as concurrency. We can pass options to the component in the form of uri parameters, setting concurrentConsumers to 4 and waitForTaskToComplete to Never, meaning it will call the method and just keep going. Re-running the example should now give us a result of all four warehouses printing their messages simultaneously and out of order as result of them all executing at once.

That's pretty exciting. We just made our app multi-threaded, and we didn't have to write one line of new code or mix a bunch threading logic into our domain. We can use the uri format of the component to configure it as we wish, limiting or expanding the maximum number of concurrent consumers we want executing at once as well as many other options.

For this article, I'm going to stop with different endpoints here because giving examples in each one could be worthy of an entire book! You can take a look at the Components Supported page on the Apache Camel Documentation site if you want to see all the components that Camel supports. Just a few interesting ones are cometd, CXFRS, FTP, and even some as far out there as GMail and IRC. Let's move on to some of the other features Camel supports.

Using The Plain Java DSL

Before we continue, let's re-write our route in plain Java. Conditionals and other concepts can start looking quite nasty as XML and I personally prefer writing them in Java, both for to increase readability and to make it easier to add dynamic functionality to your routing rules. All that needs to be done is create an implementation of RouteBuilder:

  1. // src/main/java/com/ociweb/example/routes/RouteBuilder.java
  2. public class WarehouseRoute extends RouteBuilder{
  3. @Override
  4. public void configure(){
  5. from("warehouse").to("bean:warehouseImpl");
  6. }
  7. }

Now we update the spring xml to reference the new RouteBuilder we just created:

  1. <camel:camelContext id="context">
  2. <camel:proxy id="warehouseProxy" serviceUrl="warehouse"
  3. serviceInterface="com.ociweb.example.domain.Warehouse" ></camel:proxy>
  4. <camel:routeBuilder ref="route"></camel:routeBuilder>
  5. <camel:endpoint id="warehouse"
  6. uri="seda:warehouse?concurrentConsumers=4&waitForTaskToComplete=Never"></camel:endpoint>
  7. </camel:camelContext>
  8.  
  9. <bean id="route" class="com.ociweb.example.routes.WarehouseRoute"></bean>

Notice the route is referenced as just a plain bean that we reference with a routeBuilder tag. We can now remove the old routing rules, as they now live within the Java class. Now lets add some enhanced functionality.

Implementing a Content Based Router

So far we've made our application multi-threaded, but now we've decided that we want to add a new warehouse that handles bulk orders, where bulk orders have been defined as ShippingOrders that exceed 200 items. How can we route a message to different endpoints based on the message contents? This is a good problem for which a Content Based Router from the book Enterprise Integration Patterns comes in handy. This type of router is specified to "examine the message content and route the message onto a different channel based on data contained in the message". Camel implements ContentBasedRouter as a choice which branches by evaluating various predicates.

To illustrate this, let's first create our new Warehouse implementation to handle bulk orders.

  1. public class BulkOrderWarehouse implements Warehouse {
  2. private static final String MESSAGE =
  3. "Received Bulk Order for %d %s that will ship on %s.%n";
  4. public void shipProduct(ShippingOrder order) {
  5. System.out.printf(MESSAGE,
  6. order.getQuantity(),
  7. order.getProduct(),
  8. order.getExpectedShipDate().toString("MM/dd/yyyy"));
  9.  
  10. for(int i =0; i < 1000000;i++);
  11. }
  12. }

In reality, this would probably have a bunch of logic to handle bulk orders and maybe even reside on a different server or maybe even in a different geographical location. But for the example, it's just a copy of the SprocketWarehouse with a different message. We also modify our Spring xml to register the new bean and name it and the SprocketWarehouse implementation to distinguish between them.

  1. public class WarehouseRoute extends RouteBuilder{
  2. @Override
  3. public void configure(){
  4. from("warehouse").
  5. choice().
  6. when(quantityIsGreaterThan(200)).
  7. to("bean:bulkOrderWarehouse").
  8. otherwise().
  9. to("bean:sprocketWarehouse");
  10. }
  11.  
  12. private Predicate quantityIsGreaterThan(final int bulkQuantity){
  13. return new Predicate(){
  14. public boolean matches(Exchange exchange) {
  15. return
  16. exchange.getIn().getBody(ShippingOrder.class).getQuantity()
  17. >= bulkQuantity;
  18. }
  19. };
  20. }
  21. }

The code is pretty readable — essentially it's making a choice on any messages coming from the warehouse endpoint, and when the quantity is greater than 200 it sends it to bulkOrderWarehouse, otherwise it will send it to the SprocketWarehouse. We use a Camel Predicate object to evaluate the exchange, getting the body as a ShippingOrder.class and performing the expected predicate logic on it.

If we rerun our example now, we'll see from the messages printed on the screen that all orders greater than or equal to 200 are sent to the bulk warehouse. This can also be done in pure xml in the Spring xml, but as mentioned previously it can get rather messy. In addition to creating a custom predicate, you can also use practically any JSR-233 compatible scripting languages such as Groovy, Javascript, or even PHP to add conditions for routing as well.

References

Hopefully all of this can give you a brief overview of the powerful routing capabilities of Camel and how you can utilize it to create highly decouple components that are unaware of the middleware and trasports you are using. This is really only the tip of the iceberg; I haven't even illustrated how Camel can handle an assortment of industry standard message formats or any of the twenty six different enterprise integration patterns it supports. In the end, it helps minimize the pain of dealing with the low level architecture leaving you free to focus on the more important aspect of your application: the domain.

All of the examples illustrated here are available for viewing yourself here. The examples are built using gradle, which you can install from http://gradle.org. Each example runs by simply typing gradle -q run from the command line.

 

The Software Engineering Tech Trends is a monthly newsletter featuring emerging trends in software engineering.

Subscribe

© Copyright Object Computing, Inc. 1993, 2016. All rights reserved

secret