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.git


The following commit(s) were added to refs/heads/main by this push:
     new 2192884d371 CAMEL-21551: added and documented stream caching processor 
(#17225)
2192884d371 is described below

commit 2192884d371377423dbada7f2870057cabdef8b4
Author: LostArtist <[email protected]>
AuthorDate: Fri Feb 21 11:09:06 2025 +0100

    CAMEL-21551: added and documented stream caching processor (#17225)
    
    * CAMEL-21551: added and documented stream caching processor
    
    * CAMEL-21551: fixed formatting, added javadoc to processor class
---
 .../camel/processor/StreamCachingProcessor.java    | 79 ++++++++++++++++++++++
 .../modules/ROOT/pages/stream-caching.adoc         | 10 +++
 2 files changed, 89 insertions(+)

diff --git 
a/core/camel-core-processor/src/main/java/org/apache/camel/processor/StreamCachingProcessor.java
 
b/core/camel-core-processor/src/main/java/org/apache/camel/processor/StreamCachingProcessor.java
new file mode 100644
index 00000000000..2c74dec1374
--- /dev/null
+++ 
b/core/camel-core-processor/src/main/java/org/apache/camel/processor/StreamCachingProcessor.java
@@ -0,0 +1,79 @@
+/*
+ * 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.processor;
+
+import org.apache.camel.AsyncCallback;
+import org.apache.camel.Exchange;
+import org.apache.camel.StreamCache;
+import org.apache.camel.Traceable;
+import org.apache.camel.spi.IdAware;
+import org.apache.camel.spi.RouteIdAware;
+import org.apache.camel.support.AsyncProcessorSupport;
+
+/**
+ * A processor which converts current message body to a stream cache.
+ */
+public class StreamCachingProcessor extends AsyncProcessorSupport implements 
Traceable, IdAware, RouteIdAware {
+
+    private String id;
+    private String routeId;
+
+    @Override
+    public boolean process(Exchange exchange, AsyncCallback callback) {
+        try {
+            Object body = exchange.getIn().getBody();
+
+            if (!(body instanceof StreamCache)) {
+                StreamCache streamCache = 
exchange.getContext().getTypeConverter()
+                        .convertTo(StreamCache.class, exchange, body);
+                if (streamCache != null) {
+                    exchange.getIn().setBody(streamCache);
+                }
+            }
+        } catch (Exception e) {
+            exchange.setException(e);
+        }
+
+        callback.done(true);
+        return true;
+    }
+
+    @Override
+    public String getTraceLabel() {
+        return "cache";
+    }
+
+    @Override
+    public void setId(String id) {
+        this.id = id;
+    }
+
+    @Override
+    public String getId() {
+        return id;
+    }
+
+    @Override
+    public String getRouteId() {
+        return routeId;
+    }
+
+    @Override
+    public void setRouteId(String routeId) {
+        this.routeId = routeId;
+    }
+}
diff --git a/docs/user-manual/modules/ROOT/pages/stream-caching.adoc 
b/docs/user-manual/modules/ROOT/pages/stream-caching.adoc
index 5cf51b89cb1..cca9b463ff5 100644
--- a/docs/user-manual/modules/ROOT/pages/stream-caching.adoc
+++ b/docs/user-manual/modules/ROOT/pages/stream-caching.adoc
@@ -221,3 +221,13 @@ Using the spoolRules attribute on `<streamCaching>`. if 
you have more rules, the
 <streamCaching id="myCacheConfig" spoolEnabled="true" 
spoolDirectory="/tmp/cachedir" spoolRules="mySpoolRule,myOtherSpoolRule"/>
 ----
 
+== Using StreamCachingProcessor
+
+Since Camel 4.11 this processor can be used to convert the current message 
body to a `StreamCache`. This allows the body to be re-read multiple times and 
can be placed at any point in a Camel route.
+
+[source, java]
+----
+from("direct:start")
+    .process(new StreamCachingProcessor())
+    .to("log:cached");
+----

Reply via email to