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 b25b2613eb8f CAMEL-20199: Remove ThreadLocal Yaml caching in 
SnakeYAMLDataFormat
b25b2613eb8f is described below

commit b25b2613eb8f9ee58d25bf39ca5d58d89127d23b
Author: Guillaume Nodet <[email protected]>
AuthorDate: Mon Aug 3 07:41:24 2026 +0200

    CAMEL-20199: Remove ThreadLocal Yaml caching in SnakeYAMLDataFormat
    
    Remove ThreadLocal<WeakReference<Yaml>> caching that is ineffective with
    virtual threads (each VT is short-lived, so the cache never gets a hit).
    Create a fresh Yaml instance per marshal/unmarshal operation instead, and
    rename getYaml() to createYaml() to reflect the new semantics.
    
    Closes #25240
    
    Co-Authored-By: Claude Opus 4.6 <[email protected]>
---
 .../component/snakeyaml/SnakeYAMLDataFormat.java   | 44 +++++++---------------
 1 file changed, 14 insertions(+), 30 deletions(-)

diff --git 
a/components/camel-snakeyaml/src/main/java/org/apache/camel/component/snakeyaml/SnakeYAMLDataFormat.java
 
b/components/camel-snakeyaml/src/main/java/org/apache/camel/component/snakeyaml/SnakeYAMLDataFormat.java
index 21074bba843d..aca5f5b56164 100644
--- 
a/components/camel-snakeyaml/src/main/java/org/apache/camel/component/snakeyaml/SnakeYAMLDataFormat.java
+++ 
b/components/camel-snakeyaml/src/main/java/org/apache/camel/component/snakeyaml/SnakeYAMLDataFormat.java
@@ -21,7 +21,6 @@ import java.io.InputStreamReader;
 import java.io.OutputStream;
 import java.io.OutputStreamWriter;
 import java.io.Reader;
-import java.lang.ref.WeakReference;
 
 import org.apache.camel.CamelContext;
 import org.apache.camel.CamelContextAware;
@@ -50,7 +49,6 @@ import org.yaml.snakeyaml.resolver.Resolver;
 public final class SnakeYAMLDataFormat extends ServiceSupport implements 
DataFormat, DataFormatName, CamelContextAware {
 
     private CamelContext camelContext;
-    private final ThreadLocal<WeakReference<Yaml>> yamlCache;
     private BaseConstructor constructor;
     private Representer representer;
     private DumperOptions dumperOptions;
@@ -70,7 +68,6 @@ public final class SnakeYAMLDataFormat extends ServiceSupport 
implements DataFor
     }
 
     public SnakeYAMLDataFormat(Class<?> type) {
-        this.yamlCache = new ThreadLocal<>();
         this.unmarshalType = type;
     }
 
@@ -92,7 +89,7 @@ public final class SnakeYAMLDataFormat extends ServiceSupport 
implements DataFor
     @Override
     public void marshal(final Exchange exchange, final Object graph, final 
OutputStream stream) throws Exception {
         try (OutputStreamWriter osw = new OutputStreamWriter(stream, 
ExchangeHelper.getCharsetName(exchange))) {
-            getYaml().dump(graph, osw);
+            createYaml().dump(graph, osw);
         }
     }
 
@@ -106,14 +103,14 @@ public final class SnakeYAMLDataFormat extends 
ServiceSupport implements DataFor
         Class<?> unmarshalObjectType = unmarshalType != null ? unmarshalType : 
Object.class;
 
         if (body instanceof String s) {
-            return getYaml().loadAs(s, unmarshalObjectType);
+            return createYaml().loadAs(s, unmarshalObjectType);
         } else if (body instanceof Reader r) {
-            return getYaml().loadAs(r, unmarshalObjectType);
+            return createYaml().loadAs(r, unmarshalObjectType);
         } else {
             // fallback to InputStream
             InputStream is = 
exchange.getContext().getTypeConverter().mandatoryConvertTo(InputStream.class, 
exchange, body);
             Reader r = new InputStreamReader(is, 
ExchangeHelper.getCharsetName(exchange));
-            return getYaml().loadAs(r, unmarshalObjectType);
+            return createYaml().loadAs(r, unmarshalObjectType);
         }
     }
 
@@ -139,31 +136,18 @@ public final class SnakeYAMLDataFormat extends 
ServiceSupport implements DataFor
     @Override
     protected void doStop() throws Exception {
         super.doStop();
-        yamlCache.remove();
     }
 
-    private Yaml getYaml() {
-        Yaml yaml = null;
-        WeakReference<Yaml> ref = yamlCache.get();
-
-        if (ref != null) {
-            yaml = ref.get();
-        }
-
-        if (yaml == null) {
-            LoaderOptions options = new LoaderOptions();
-            options.setTagInspector(new TrustedTagInspector());
-            options.setAllowRecursiveKeys(allowRecursiveKeys);
-            options.setMaxAliasesForCollections(maxAliasesForCollections);
-            BaseConstructor c = constructor != null ? constructor : 
defaultConstructor(camelContext);
-            Representer r = representer != null ? representer : 
defaultRepresenter();
-            DumperOptions d = dumperOptions != null ? dumperOptions : 
defaultDumperOptions();
-            Resolver res = resolver != null ? resolver : defaultResolver();
-            yaml = new Yaml(c, r, d, options, res);
-            yamlCache.set(new WeakReference<>(yaml));
-        }
-
-        return yaml;
+    private Yaml createYaml() {
+        LoaderOptions options = new LoaderOptions();
+        options.setTagInspector(new TrustedTagInspector());
+        options.setAllowRecursiveKeys(allowRecursiveKeys);
+        options.setMaxAliasesForCollections(maxAliasesForCollections);
+        BaseConstructor c = constructor != null ? constructor : 
defaultConstructor(camelContext);
+        Representer r = representer != null ? representer : 
defaultRepresenter();
+        DumperOptions d = dumperOptions != null ? dumperOptions : 
defaultDumperOptions();
+        Resolver res = resolver != null ? resolver : defaultResolver();
+        return new Yaml(c, r, d, options, res);
     }
 
     public BaseConstructor getConstructor() {

Reply via email to