This is an automated email from the ASF dual-hosted git repository.
davsclaus 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 cda2391 CAMEL-22935: camel-core - Allow to add custom functions to
simple language
cda2391 is described below
commit cda2391b9a6123265269261a8166077818bd60aa
Author: Claus Ibsen <[email protected]>
AuthorDate: Wed Feb 4 21:11:59 2026 +0100
CAMEL-22935: camel-core - Allow to add custom functions to simple language
---
jbang/simple-custom-function/README.adoc | 55 +++++++++++++++++++++++++
jbang/simple-custom-function/StockFunction.java | 27 ++++++++++++
jbang/simple-custom-function/myapp.camel.yaml | 8 ++++
3 files changed, 90 insertions(+)
diff --git a/jbang/simple-custom-function/README.adoc
b/jbang/simple-custom-function/README.adoc
new file mode 100644
index 0000000..463a579
--- /dev/null
+++ b/jbang/simple-custom-function/README.adoc
@@ -0,0 +1,55 @@
+== YAML with custom simple function
+
+An example, of creating custom simple functions in Java code, which then can
be used in YAML DSL routes.
+
+=== 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 *
+----
+
+=== 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 * --dev
+----
+
+
+=== 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/simple-custom-function/StockFunction.java
b/jbang/simple-custom-function/StockFunction.java
new file mode 100644
index 0000000..c2e32a8
--- /dev/null
+++ b/jbang/simple-custom-function/StockFunction.java
@@ -0,0 +1,27 @@
+package com.mycompany;
+
+import org.apache.camel.BindToRegistry;
+import org.apache.camel.Exchange;
+import org.apache.camel.spi.SimpleFunction;
+
+import java.util.Random;
+
+@BindToRegistry("stock-function")
+public class StockFunction implements SimpleFunction {
+
+ @Override
+ public String getName() {
+ return "stock";
+ }
+
+ @Override
+ public boolean allowNull() {
+ return true;
+ }
+
+ @Override
+ public Object apply(Exchange exchange, Object input) throws Exception {
+ return 200 + new Random().nextInt(1000);
+ }
+
+}
\ No newline at end of file
diff --git a/jbang/simple-custom-function/myapp.camel.yaml
b/jbang/simple-custom-function/myapp.camel.yaml
new file mode 100644
index 0000000..2ced567
--- /dev/null
+++ b/jbang/simple-custom-function/myapp.camel.yaml
@@ -0,0 +1,8 @@
+- from:
+ uri: "timer:yaml"
+ parameters:
+ period: "5000"
+ steps:
+ - setBody:
+ simple: ${stock()}
+ - log: "The stock price is: ${body}"