Apache Camel Example

Recently, I was digging a bit into Enterprise Integration Patterns and Apache Camel. The result was a handy example application with a simple camel route, which reads „incoming“ JSON files from a configurable inbox directory and passes them to my example REST customer service:

CamelExample

Not every component from Camel has a counterpart in the list of the enterprise integration patterns (EIP). However, I hope, the sloppy UML / EIP diagram mix that I used, helps a bit to clarify, what I did.

JSON files are read from an inbox directory via a camel file component and a Message object is constructed, which is essentially a wrapper around Java Object plus several header fields. Subsequently, the processing of each message is logged by the logging framework, the content type and http method headers are set and the message content is fired at the customer REST service in a PUT request. When the REST service has processed the request, the response object is passed to an exchangeable CustomerServiceResponseHandler. In my simple example, I implemented a handler, which again logs the URL of the created resource.

The MainApp class is responsible to bootstrap the whole thing: Create a CamelContext with help of the corresponding CamelContextFactory class, passing it the instance of the response handler implementation.

CustomerServiceResponseHandler responseHandler = new CustomerServiceResponseHandlerLoggingImpl();
 CamelContext camelContext = CamelContextFactory.createCamelContext(PROPS, responseHandler);

camelContext.start();
 Thread.sleep(10000);
 camelContext.stop();

Where is it and how to run?

Check it out on GitHub (unfortunately, I chose to put this project in a combined „fiddling“ repository):

https://github.com/abecker84/apache-camel-example

As a prerequisite, you also need my example REST service, also located on GitHub:

https://github.com/abecker84/spring-boot-restservice-example

When the REST service is run as a spring boot application with java -jar <jar-file>, you can run either the main application class MainApp, or you can give the integration test ResourceCreationIT a chance. This test actually uses the PUT method of the REST service to upload a JSON file by placing it in the inbox directory and bootstrapping the camel context. Subsequently, it uses the returned resource location in order to GET the newly created resource and assert that the ressource’s fields have the correct values.

In either case (running MainApp class of integration test), you will need to pass the following VM arguments to the JVM:

-DcamelExchangePath=/home/<username>/camel
-DcustomerRestEndpoint=http://localhost:8080/rest-example

Note that the actual paths could vary! The directory located at DcamelExchangePath must contain a directory called inbox, where you can place the incoming JSON files, as shown in the integration test!

REST service with Spring Boot

Recently, I fiddled around with Spring Boot’s means for setting up a simple REST-service to manage simplified customer data.

Implementation

  • Application class annotated with @SpringBootApplication
  • Controller class annotated with @RestController
  • Controller delegates calls to a business logic component CustomerManagement, which is injected via Spring
  • REST api provides http verbs GET, PUT (create and update) and DELETE
  • REST api is documented via a very rudimentary swagger setup

Resources

http://www.springboottutorial.com/creating-rest-service-with-spring-boot

Where is it and how to run?

Check it out on GitHub: https://github.com/abecker84/spring-boot-restservice-example

You should be able to checkout the project from GitHub, (optionally) import the project to your favourite IDE and build the JAR file with mvn package. When run with java -jar, a tomcat server starts up, running the app.

The swagger ui, which describes the REST api, is located at http://localhost:8080/swagger-ui.html

Comments appreciated!

Have fun!