{"id":399,"date":"2010-08-26T22:23:52","date_gmt":"2010-08-26T20:23:52","guid":{"rendered":"http:\/\/mcl.jogger.pl\/2010\/08\/26\/complex-flows-with-apache-camel\/"},"modified":"2022-08-02T10:29:10","modified_gmt":"2022-08-02T08:29:10","slug":"complex-flows-with-apache-camel","status":"publish","type":"post","link":"https:\/\/touk.pl\/blog\/2010\/08\/26\/complex-flows-with-apache-camel\/","title":{"rendered":"<!--:en-->Complex flows with Apache Camel<!--:-->"},"content":{"rendered":"<p><!--:en-->At work, we&#8217;re mainly integrating services and systems, and since we&#8217;re on a constant lookout for new, better technologies, ways to do things easier, make them more sustainable, we&#8217;re trying to Usually we use<\/p>\n<p><a href=\"http:\/\/camel.apache.org\">Apache Camel<\/a> for this task, which is a Swiss-knife for integration engineer. What&#8217;s more, this tools corresponds well with our approach to integration solutions:<br \/>\n* try to operate on XML messages, so you get the advantage of XPaths, XSL and other benefits,<br \/>\n* don&#8217;t convert XML into Java classes back and forth and be worried with problems like XML conversion,<br \/>\n* try to get a simple flow of the process. However, at first sight Apache Camel seems to have some drawbacks mainly in the area of practical solutions ;-). It&#8217;s very handy tool if you need to use it as a pipeline with some marginal processing of the data that passes through it. It gets a lot harder to wrap your head around if you consider some branching and intermediate calls to external services. This may be tricky to write properly in Camel&#8217;s DSL. Here is a simple pipeline example:<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/blog.innovative-labs.com\/blog\/simple_flow.png\" alt=\"\" width=\"600\" \/> And here the exact scenario we&#8217;re discussing: <img decoding=\"async\" src=\"http:\/\/blog.innovative-labs.com\/blog\/branched_flow.png\" alt=\"\" width=\"600\" \/> What I&#8217;d like to show is the solution to this problem. Well, if you&#8217;re using a recent version of Camel this may be easier, a little different, but should still more-or-less work this way. This code is written for Apache Camel 1.4 &#8211; a rather antic version, but that&#8217;s what we&#8217;re forced to use. Oh, well. Ok, enough whining! So, I create a test class to illustrate the case. The route defined in <strong>TestRouter<\/strong> class is responsible for:<br \/>\n1. receiving input<br \/>\n2. setting exchange property to a given xpath, which effectively is the name of the first XML element in the input stream<br \/>\n3. than, the input data is sent to three different external services, each of them replies with some fictional data &#8211; notice routes <strong>a<\/strong>, <strong>b<\/strong> and <strong>c<\/strong>. The <strong>SimpleContentSetter<\/strong> processor is just for responding with a given text.<br \/>\n4. the response from all three services is somehow processed by <strong>RequestEnricher<\/strong> bean, which is described below<br \/>\n5. eventually the exchange is logged in specified category Here is some code for this:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\">public class SimpleTest {\r\n    public void setUp() throws Exception {\r\n        TestRouter tr = new TestRouter();\r\n        ctx.addRoutes(tr);\r\n    }\r\n\r\n    @Test\r\n    public void shouldCheck() throws Exception {\r\n        ctx.createProducerTemplate().send(\"direct:in\", getInOut(\"\"));\r\n    }\r\n\r\n    class TestRouter extends RouteBuilder {\r\n\r\n        public void configure() throws Exception {\r\n\r\n            ((ProcessorType) from(\"direct:in\")\r\n                .setProperty(\"operation\").xpath(\"local-name(\/*)\", String.class)\r\n                .multicast(new MergeAggregationStrategy())\r\n                .to(\"direct:a\", \"direct:b\", \"direct:c\")\r\n                .end()\r\n                .setBody().simple(\"${in.body}\"))\r\n            .bean(RequestEnricher.class, \"enrich\")\r\n                .to(\"log:pl.touk.debug\");\r\n\r\n            from(\"direct:a\").process(new SimpleContentSetter(\"\"));\r\n            from(\"direct:b\").process(new SimpleContentSetter(\"\"));\r\n            from(\"direct:c\").process(new SimpleContentSetter(\"\"));\r\n        }\r\n    }\r\n}<\/pre>\n<p>What&#8217;s unusual in this code is the fact, that what normally Camel does when you write a piece of DSL like:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">.to(\"direct:a\", \"direct:b\", \"direct:c\")<\/pre>\n<p>is pass input to service<\/p>\n<p><strong>a<\/strong>, than <strong>a<\/strong>&#8216;s output gets passed to <strong>b<\/strong>, becomes it&#8217;s input, than <strong>b<\/strong>&#8216;s output becomes <strong>c<\/strong>&#8216;s input. The problem being, you loose the output from <strong>a<\/strong> and <strong>b<\/strong>, not mentioning that you might want to send the same input to all three services. That&#8217;s where a little tool called <em>multicast()<\/em> comes in handy. It offers you the ability to aggregate the outputs of those services. You may even create an <strong>AggregationStrategy<\/strong> that will do it the way you like. Below class, <strong>MergeAggregationStrategy<\/strong> does exactly that kind of work &#8211; it joins outputs from all three services. A lot of info about proper use of <strong>AggregationStrategy<\/strong>-ies can be found in <a href=\"http:\/\/tmielke.blogspot.com\/2009\/01\/using-camel-aggregator-correctly.html\">this post by Torsten Mielke.<\/a><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">public class MergeAggregationStrategy implements AggregationStrategy {\r\n\r\n    public Exchange aggregate(Exchange oldExchange, Exchange newExchange) {\r\n        if (oldExchange.isFailed()) {\r\n            return oldExchange;\r\n        }\r\n        transformMessage(oldExchange.getIn(), newExchange.getIn());\r\n        transformMessage(oldExchange.getOut(), newExchange.getOut());\r\n        return newExchange;\r\n    }\r\n\r\n    private void transformMessage(Message oldM, Message newM) {\r\n        String oldBody = oldM.getBody(String.class);\r\n        String newBody = newM.getBody(String.class);\r\n        newM.setBody(oldBody + newBody);\r\n    }\r\n\r\n}<\/pre>\n<p>However nice this may look (or not), what you&#8217;re left with is a mix of multiple XMLs. Normally this won&#8217;t do you much good. Better thing to do is to parse this output in some way. What we&#8217;re using for this is a Groovy :). Which is great for the task of parsing XML. A lot less verbose than ordinary Java. Let&#8217;s assume a scenario, that the aggregated output, currently looking like this:<\/p>\n<pre class=\"brush: xml\"><\/pre>\n<p>is to be processed with the following steps in mind:<\/p>\n<ul>\n<li>use ** as the result element<\/li>\n<li>use attributes <em>param1<\/em>, <em>param2<\/em>, <em>param3<\/em> from element ** and add it to result element **<\/li>\n<\/ul>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">public class RequestEnricher {\r\n\r\n    public String enrich(@Property(name = \"operation\") String operation, Exchange ex) {\r\n\r\n        use(DOMCategory) {\r\n            def dhl = new groovy.xml.Namespace(\"http:\/\/example.com\/common\/dhl\/schema\", 'dhl')\r\n            def pc = new groovy.xml.Namespace(\"http:\/\/example.com\/pc\/types\", 'pc')\r\n            def doc = new XmlParser().parseText(ex.in.body)\r\n\r\n            def pcRequest = doc.\r\n            \"aaaa\" [0]\r\n\r\n            [\"param1\", \"param2\", \"param3\"].each() {\r\n                def node = doc.\r\n                '**' [(\"\" + it)][0]\r\n                if (node)\r\n                    pcRequest['@' + it] = node.text()\r\n            }\r\n\r\n            gNodeListToString([pcRequest])\r\n        }\r\n\r\n    }\r\n\r\n    String gNodeListToString(list) {\r\n        StringBuilder sb = new StringBuilder();\r\n        list.each {\r\n            listItem -&gt;\r\n                StringWriter sw = new StringWriter();\r\n            new XmlNodePrinter(new PrintWriter(sw)).print(listItem)\r\n            sb.append(sw.toString());\r\n        }\r\n        return sb.toString();\r\n    }\r\n\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<p>What we&#8217;re doing here, especially the last line of<\/p>\n<p><strong>enrich<\/strong> method is the conversion to String. There are some problems for Camel if we spit out Groovy objects. The rest is just some Groovy specific ways of manipulating XML. But looking into <strong>enrich<\/strong> method&#8217;s parameters, there is <em>@Property<\/em> annotation used, which binds the property assigned earlier in a router code to one of the arguments. That is really cool feature and there are more such annotations:<br \/>\n* <em>@XPath<\/em><br \/>\n* <em>@Header<\/em><br \/>\n* <em>@Headers<\/em> and <em>@Properties<\/em> &#8211; gives whole maps of properties or headers This pretty much concludes the subject :) Have fun, and if in doubt, leave a comment with your question!<\/p>\n<p><!--:--><\/p>\n","protected":false},"excerpt":{"rendered":"At work, we&#8217;re mainly integrating services and systems, and since we&#8217;re on a constant lookout for new, better technologies, ways to do things easier, make them more sustainable, we&#8217;re trying to\nUsually we use Apache Camel for this task, which is a Swis&#8230;At work, we&#8217;re mainly integrating services and systems, and since we&#8217;re on a constant lookout for new, better technologies, ways to do things easier, make them more sustainable, we&#8217;re trying to\nUsually we use Apache Camel for this task, which is a Swis&#8230;\n","protected":false},"author":11,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[68],"class_list":{"0":"post-399","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-development-design","7":"tag-java"},"_links":{"self":[{"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/posts\/399","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/users\/11"}],"replies":[{"embeddable":true,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/comments?post=399"}],"version-history":[{"count":10,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/posts\/399\/revisions"}],"predecessor-version":[{"id":14802,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/posts\/399\/revisions\/14802"}],"wp:attachment":[{"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/media?parent=399"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/categories?post=399"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/tags?post=399"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}