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:
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):
As a prerequisite, you also need my example REST service, also located on GitHub:
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:
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!