{"id":2076,"date":"2026-01-31T01:24:08","date_gmt":"2026-01-30T12:24:08","guid":{"rendered":"https:\/\/www.ronella.xyz\/?p=2076"},"modified":"2026-01-31T01:24:08","modified_gmt":"2026-01-30T12:24:08","slug":"apache-camel-bean-eip","status":"publish","type":"post","link":"https:\/\/www.ronella.xyz\/?p=2076","title":{"rendered":"Apache Camel Bean EIP"},"content":{"rendered":"<p>Apache Camel's Bean Enterprise Integration Pattern (EIP) lets you invoke POJO methods directly within routes, seamlessly integrating custom business logic without heavy frameworks. This article presents a fully self-contained, runnable Java example using Camel Main, featuring header manipulation and enhanced logging for real-world demonstration. The code requires only <code>camel-core<\/code> on the classpath.<\/p>\n<h2>Key Features Demonstrated<\/h2>\n<ul>\n<li>Timer-triggered message flow every 2 seconds.<\/li>\n<li>Bean method invocation with body and headers parameter binding.<\/li>\n<li>Custom header enrichment (processor metadata, timestamps).<\/li>\n<li>Split routes using <code>direct<\/code> for modularity.<\/li>\n<li>Infinite runtime via <code>Camel Main<\/code> (Ctrl+C to stop).<\/li>\n<\/ul>\n<p>The bean processes the message body, uppercases it, adds headers tracking processing details, and returns the transformed body.<\/p>\n<h2>Complete Runnable Code<\/h2>\n<p>Here's the entire application in a single file\u2014compile and run directly:<\/p>\n<pre><code class=\"language-java\">import org.apache.camel.Body;\nimport org.apache.camel.Headers;\nimport org.apache.camel.builder.RouteBuilder;\n\nimport java.util.Map;\n\n\/**\n * Apache Camel example demonstrating bean method invocation in a route.\n * Uses a timer-based route that processes messages through a custom bean.\n *\/\npublic class BeanExampleApp {\n\n    static void main(String[] args) throws Exception {\n        \/\/ Create a new Camel Main instance (using fully qualified name to avoid conflict)\n        org.apache.camel.main.Main main = new org.apache.camel.main.Main();\n\n        \/\/ Add routes with bean processing\n        main.configure().addRoutesBuilder(new RouteBuilder() {\n            @Override\n            public void configure() {\n                \/\/ Route 1: Timer that fires every 2 seconds\n                from(&quot;timer:beanTick?period=2000&quot;)\n                        .setBody().constant(&quot;hello from timer&quot;)\n                        .log(&quot;${body}&quot;)\n                        .to(&quot;direct:process&quot;)\n                        .log(&quot;${body}&quot;);\n\n                \/\/ Route 2: Process the message using a bean\n                from(&quot;direct:process&quot;)\n                        .bean(new MyProcessor(), &quot;process&quot;)  \/\/ Invoke bean method\n                        .log(&quot;After bean: ${body}&quot;)\n                        .log(&quot;Headers - ProcessedBy: ${header.ProcessedBy}, ProcessedAt: ${header.ProcessedAt}, OriginalBody: ${header.OriginalBody}&quot;);\n            }\n        });\n\n        \/\/ Run Camel (will run until Ctrl+C is pressed)\n        main.run(args);\n    }\n\n    \/**\n     * Custom processor bean that processes messages.\n     * Accepts body as String and headers as Map for manipulation.\n     *\/\n    public static class MyProcessor {\n        public String process(@Body String body, @Headers Map&lt;String, Object&gt; headers) {\n            \/\/ Add custom headers\n            headers.put(&quot;ProcessedBy&quot;, &quot;MyProcessor&quot;);\n            headers.put(&quot;ProcessedAt&quot;, System.currentTimeMillis());\n            headers.put(&quot;OriginalBody&quot;, body);\n\n            \/\/ Process and return the modified body\n            return body.toUpperCase() + &quot; - processed by MyProcessor&quot;;\n        }\n    }\n}<\/code><\/pre>\n<h2>Expected Output<\/h2>\n<p>Console logs repeat every 2 seconds:<\/p>\n<pre><code>hello from timer\nAfter bean: HELLO FROM TIMER - processed by MyProcessor\nHeaders - ProcessedBy: MyProcessor, ProcessedAt: 1769775068470, OriginalBody: hello from timer\nHELLO FROM TIMER - processed by MyProcessor<\/code><\/pre>\n<h2>Best Practices Highlighted<\/h2>\n<ul>\n<li>\n<p><strong>Parameter Binding<\/strong>: Use annotations like <code>@Body<\/code>, <code>@Headers<\/code>, <code>@Header(&quot;name&quot;)<\/code> for type-safe access; raw types work for simple cases.<\/p>\n<\/li>\n<li>\n<p><strong>Method Selection<\/strong>: Explicitly name <code>&quot;process&quot;<\/code> method to avoid ambiguity with overloads.<\/p>\n<\/li>\n<li>\n<p><strong>Inline Beans<\/strong>: Perfect for simple processors; use registry lookup for complex or shared beans.<\/p>\n<\/li>\n<li>\n<p><strong>Route Modularity<\/strong>: <code>direct:<\/code> endpoints enable clean separation of concerns.<\/p>\n<\/li>\n<\/ul>\n<p>This pattern excels for transformations, validations, and custom logic in enterprise integration routes.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Apache Camel&#8217;s Bean Enterprise Integration Pattern (EIP) lets you invoke POJO methods directly within routes, seamlessly integrating custom business logic without heavy frameworks. This article presents a fully self-contained, runnable Java example using Camel Main, featuring header manipulation and enhanced logging for real-world demonstration. The code requires only camel-core on the classpath. Key Features Demonstrated [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[92],"tags":[],"_links":{"self":[{"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=\/wp\/v2\/posts\/2076"}],"collection":[{"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2076"}],"version-history":[{"count":1,"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=\/wp\/v2\/posts\/2076\/revisions"}],"predecessor-version":[{"id":2077,"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=\/wp\/v2\/posts\/2076\/revisions\/2077"}],"wp:attachment":[{"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2076"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2076"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2076"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}