You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by gg...@apache.org on 2023/06/30 12:18:55 UTC

[camel-kamelets-examples] branch main updated: Add example that uses Spring XML inside Camel XML DSL

This is an automated email from the ASF dual-hosted git repository.

ggrzybek pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel-kamelets-examples.git


The following commit(s) were added to refs/heads/main by this push:
     new a6f59c2  Add example that uses Spring XML inside Camel XML DSL
a6f59c2 is described below

commit a6f59c2616ebaf5cd683fd3b904ede23343fa658
Author: Grzegorz Grzybek <gr...@gmail.com>
AuthorDate: Fri Jun 30 14:18:45 2023 +0200

    Add example that uses Spring XML inside Camel XML DSL
---
 jbang/app-routes-spring-beans/README.adoc          | 87 ++++++++++++++++++++++
 .../app-routes-spring-beans/StandaloneGreeter.java | 21 ++++++
 jbang/app-routes-spring-beans/app.xml              | 34 +++++++++
 3 files changed, 142 insertions(+)

diff --git a/jbang/app-routes-spring-beans/README.adoc b/jbang/app-routes-spring-beans/README.adoc
new file mode 100644
index 0000000..9401d82
--- /dev/null
+++ b/jbang/app-routes-spring-beans/README.adoc
@@ -0,0 +1,87 @@
+== Hello Java
+
+This example shows how to configure beans using full Spring XML DSL inside `<camel>`.
+This allows to fully leverage Spring-style dependency injection and what's more, we can alter the behavior
+of Camel context itself by defining beans that are used to configure CamelContext object.
+
+Camel 4 is required.
+
+=== Install JBang
+
+First install JBang according to https://www.jbang.dev
+
+When JBang is installed then you should be able to run from a shell:
+
+[source,sh]
+----
+$ jbang --version
+----
+
+This will output the version of JBang.
+
+To run this example you can either install Camel on JBang via:
+
+[source,sh]
+----
+$ jbang app install camel@apache/camel
+----
+
+Which allows to run CamelJBang with `camel` as shown below.
+
+=== How to run
+
+Then you can run this example using:
+
+[source,sh]
+----
+$ camel run StandaloneGreeter.java beans.yaml app.xml
+----
+
+Or run with JBang using the longer command line (without installing camel as app in JBang):
+
+[source,sh]
+----
+$ jbang camel@apache/camel run StandaloneGreeter.java beans.yaml app.xml
+----
+
+=== Live reload
+
+You can run the example in dev mode which allows you to edit the example,
+and hot-reload when the file is saved.
+
+[source,sh]
+----
+$ camel run StandaloneGreeter.java beans.yaml app.xml --dev
+----
+
+=== Run directly from github
+
+The example can also be run directly by referring to the github URL as shown:
+
+[source,sh]
+----
+$ jbang camel@apache/camel run https://github.com/apache/camel-kamelets-examples/tree/main/jbang/app-routes-beans
+----
+
+=== Developer Web Console
+
+You can enable the developer console via `--console` flag as show:
+
+[source,sh]
+----
+$ camel run StandaloneGreeter.java beans.yaml app.xml --console
+----
+
+Then you can browse: http://localhost:8080/q/dev to introspect the running Camel applicaton.
+Under "beans" Camel should display `greeter1` and `greeter2` beans.
+
+
+=== Help and contributions
+
+If you hit any problem using Camel or have some feedback, then please
+https://camel.apache.org/community/support/[let us know].
+
+We also love contributors, so
+https://camel.apache.org/community/contributing/[get involved] :-)
+
+The Camel riders!
diff --git a/jbang/app-routes-spring-beans/StandaloneGreeter.java b/jbang/app-routes-spring-beans/StandaloneGreeter.java
new file mode 100644
index 0000000..2a43270
--- /dev/null
+++ b/jbang/app-routes-spring-beans/StandaloneGreeter.java
@@ -0,0 +1,21 @@
+package camel.example;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.Processor;
+import org.apache.camel.util.StringHelper;
+
+public class StandaloneGreeter implements Processor {
+
+    private String message;
+
+    public void setMessage(String message) {
+        this.message = message;
+    }
+
+    @Override
+    public void process(Exchange exchange) throws Exception {
+        String msg = exchange.getIn().getBody(String.class);
+        exchange.getIn().setBody(message + " " + StringHelper.after(msg, "I'm "));
+    }
+
+}
diff --git a/jbang/app-routes-spring-beans/app.xml b/jbang/app-routes-spring-beans/app.xml
new file mode 100644
index 0000000..a27b115
--- /dev/null
+++ b/jbang/app-routes-spring-beans/app.xml
@@ -0,0 +1,34 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<camel>
+
+    <!-- Using <beans> element we can declare beans in Spring XML way without actually using Spring's application context -->
+    <beans xmlns="http://www.springframework.org/schema/beans" xmlns:util="http://www.springframework.org/schema/util">
+
+        <!-- Spring beans and util namespaces can be used -->
+        <util:constant id="max" static-field="java.lang.Integer.MAX_VALUE" />
+
+        <!-- beans with identifiers can be referenced from routes. Spring-style dependency injection can be used. -->
+        <bean id="messageString" class="java.lang.String">
+            <constructor-arg index="0" value="Hello!"/>
+        </bean>
+
+        <bean id="greeter" class="camel.example.StandaloneGreeter">
+            <property name="message" ref="messageString"/>
+        </bean>
+
+        <!-- We can also define beans that will be used to alter the configuration of Camel context itself -->
+        <!-- <bean class="org.apache.camel.support.SimpleUuidGenerator"/> -->
+        <bean class="org.apache.camel.support.ClassicUuidGenerator"/>
+    </beans>
+
+    <route id="r1">
+        <from uri="timer:xml?period={{time:1000}}"/>
+        <setBody>
+            <simple>I'm Route ${routeId}</simple>
+        </setBody>
+        <bean ref="greeter" />
+        <log message="${body} (exchange id: ${exchangeId})"/>
+    </route>
+
+</camel>