You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2025/12/18 10:46:53 UTC
(camel) 08/14: CAMEL-22431: camel-once - A component for development to trigger only once
This is an automated email from the ASF dual-hosted git repository.
davsclaus pushed a commit to branch once
in repository https://gitbox.apache.org/repos/asf/camel.git
commit 20673d697a4f11d064a306965f18a3e7ccb37e71
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Thu Dec 18 09:59:30 2025 +0100
CAMEL-22431: camel-once - A component for development to trigger only once
---
.../camel-once/src/main/docs/once-component.adoc | 13 ++++++
.../component/once/OnceBodyAndHeaderFileTest.java | 47 ++++++++++++++++++++++
.../once/OnceBodyAndVariableFileTest.java | 47 ++++++++++++++++++++++
.../component/once/OnceBodyAndVariableTest.java | 45 +++++++++++++++++++++
.../camel/component/once/OnceBodyFileTest.java | 45 +++++++++++++++++++++
components/camel-once/src/test/resources/data.csv | 2 +
components/camel-once/src/test/resources/price.txt | 1 +
7 files changed, 200 insertions(+)
diff --git a/components/camel-once/src/main/docs/once-component.adoc b/components/camel-once/src/main/docs/once-component.adoc
index 8345691e036b..7ef15a74ad86 100644
--- a/components/camel-once/src/main/docs/once-component.adoc
+++ b/components/camel-once/src/main/docs/once-component.adoc
@@ -62,6 +62,19 @@ And the route in XML DSL:
</route>
-----
+=== Using headers and variables
+
+You can also specify headers and variables using _multivalue_, where each header is prefixed with `header.<key>`.
+In the sample below we set 2 headers as follows: `foo=abc` and `bar=123`.
+
+[source,java]
+----
+from("once:tick?body=world&header.foo=abc&header.bar=123")
+ .to("bean:myBean");
+----
+
+You can do the same for variables with `variable.<key>`.
+
=== Firing as soon as possible
By default, the component is fired after 1 seconds when Camel has been fully started.
diff --git a/components/camel-once/src/test/java/org/apache/camel/component/once/OnceBodyAndHeaderFileTest.java b/components/camel-once/src/test/java/org/apache/camel/component/once/OnceBodyAndHeaderFileTest.java
new file mode 100644
index 000000000000..6490ef1ea6f8
--- /dev/null
+++ b/components/camel-once/src/test/java/org/apache/camel/component/once/OnceBodyAndHeaderFileTest.java
@@ -0,0 +1,47 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.once;
+
+import org.apache.camel.RoutesBuilder;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.test.junit5.CamelTestSupport;
+import org.junit.jupiter.api.Test;
+
+public class OnceBodyAndHeaderFileTest extends CamelTestSupport {
+
+ @Test
+ public void testOnce() throws Exception {
+ getMockEndpoint("mock:result").expectedBodiesReceived("1,jack", "2,moe");
+ getMockEndpoint("mock:result").expectedHeaderReceived("foo", "abc");
+ getMockEndpoint("mock:result").expectedHeaderReceived("bar", "456");
+
+ MockEndpoint.assertIsSatisfied(context);
+ }
+
+ @Override
+ protected RoutesBuilder createRouteBuilder() throws Exception {
+ return new RouteBuilder() {
+ @Override
+ public void configure() throws Exception {
+ from("once:tick?delay=-1&body=file:src/test/resources/data.csv&header.foo=abc&header.bar=classpath:price.txt")
+ .split(body().tokenize("\n"))
+ .to("mock:result");
+ }
+ };
+ }
+}
diff --git a/components/camel-once/src/test/java/org/apache/camel/component/once/OnceBodyAndVariableFileTest.java b/components/camel-once/src/test/java/org/apache/camel/component/once/OnceBodyAndVariableFileTest.java
new file mode 100644
index 000000000000..7c1354a3e3b8
--- /dev/null
+++ b/components/camel-once/src/test/java/org/apache/camel/component/once/OnceBodyAndVariableFileTest.java
@@ -0,0 +1,47 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.once;
+
+import org.apache.camel.RoutesBuilder;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.test.junit5.CamelTestSupport;
+import org.junit.jupiter.api.Test;
+
+public class OnceBodyAndVariableFileTest extends CamelTestSupport {
+
+ @Test
+ public void testOnce() throws Exception {
+ getMockEndpoint("mock:result").expectedBodiesReceived("1,jack", "2,moe");
+ getMockEndpoint("mock:result").expectedHeaderReceived("foo", "abc");
+ getMockEndpoint("mock:result").expectedVariableReceived("bar", "456");
+
+ MockEndpoint.assertIsSatisfied(context);
+ }
+
+ @Override
+ protected RoutesBuilder createRouteBuilder() throws Exception {
+ return new RouteBuilder() {
+ @Override
+ public void configure() throws Exception {
+ from("once:tick?delay=-1&body=file:src/test/resources/data.csv&header.foo=abc&variable.bar=classpath:price.txt")
+ .split(body().tokenize("\n"))
+ .to("mock:result");
+ }
+ };
+ }
+}
diff --git a/components/camel-once/src/test/java/org/apache/camel/component/once/OnceBodyAndVariableTest.java b/components/camel-once/src/test/java/org/apache/camel/component/once/OnceBodyAndVariableTest.java
new file mode 100644
index 000000000000..366d5e2b40fe
--- /dev/null
+++ b/components/camel-once/src/test/java/org/apache/camel/component/once/OnceBodyAndVariableTest.java
@@ -0,0 +1,45 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.once;
+
+import org.apache.camel.RoutesBuilder;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.test.junit5.CamelTestSupport;
+import org.junit.jupiter.api.Test;
+
+public class OnceBodyAndVariableTest extends CamelTestSupport {
+
+ @Test
+ public void testOnce() throws Exception {
+ getMockEndpoint("mock:result").expectedBodiesReceived("world");
+ getMockEndpoint("mock:result").expectedHeaderReceived("foo", "abc");
+ getMockEndpoint("mock:result").expectedVariableReceived("bar", "123");
+
+ MockEndpoint.assertIsSatisfied(context);
+ }
+
+ @Override
+ protected RoutesBuilder createRouteBuilder() throws Exception {
+ return new RouteBuilder() {
+ @Override
+ public void configure() throws Exception {
+ from("once:tick?delay=-1&body=world&header.foo=abc&variable.bar=123").to("mock:result");
+ }
+ };
+ }
+}
diff --git a/components/camel-once/src/test/java/org/apache/camel/component/once/OnceBodyFileTest.java b/components/camel-once/src/test/java/org/apache/camel/component/once/OnceBodyFileTest.java
new file mode 100644
index 000000000000..f5751c102d0b
--- /dev/null
+++ b/components/camel-once/src/test/java/org/apache/camel/component/once/OnceBodyFileTest.java
@@ -0,0 +1,45 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.once;
+
+import org.apache.camel.RoutesBuilder;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.test.junit5.CamelTestSupport;
+import org.junit.jupiter.api.Test;
+
+public class OnceBodyFileTest extends CamelTestSupport {
+
+ @Test
+ public void testOnce() throws Exception {
+ getMockEndpoint("mock:result").expectedBodiesReceived("1,jack", "2,moe");
+
+ MockEndpoint.assertIsSatisfied(context);
+ }
+
+ @Override
+ protected RoutesBuilder createRouteBuilder() throws Exception {
+ return new RouteBuilder() {
+ @Override
+ public void configure() throws Exception {
+ from("once:tick?delay=-1&body=file:src/test/resources/data.csv")
+ .split(body().tokenize("\n"))
+ .to("mock:result");
+ }
+ };
+ }
+}
diff --git a/components/camel-once/src/test/resources/data.csv b/components/camel-once/src/test/resources/data.csv
new file mode 100644
index 000000000000..6554140f0c5d
--- /dev/null
+++ b/components/camel-once/src/test/resources/data.csv
@@ -0,0 +1,2 @@
+1,jack
+2,moe
\ No newline at end of file
diff --git a/components/camel-once/src/test/resources/price.txt b/components/camel-once/src/test/resources/price.txt
new file mode 100644
index 000000000000..ee2b8364542e
--- /dev/null
+++ b/components/camel-once/src/test/resources/price.txt
@@ -0,0 +1 @@
+456
\ No newline at end of file