mayurbm opened a new pull request, #25871:
URL: https://github.com/apache/camel/pull/25871

   ## Summary
   
   Fixes [CAMEL-24553](https://issues.apache.org/jira/browse/CAMEL-24553).
   
   Adds a pre-flight size guard in `MessageSupport.getMandatoryBody(Class<T>)` 
that refuses to convert bodies whose known length exceeds the in-memory 
conversion limit to bulk types (`String`, `byte[]`, `CharSequence`). This 
prevents `OutOfMemoryError` when the Splitter/Bean path calls 
`getMandatoryBody(String.class)` on a multi-GB payload.
   
   ## Stack trace (real-world OOM)
   
   ```
   Caused by: java.lang.OutOfMemoryError: Required array length 2147483638 + 
737144626 is too large
       at 
java.base/java.lang.AbstractStringBuilder.append(AbstractStringBuilder.java:582)
       at 
org.apache.camel.InvalidPayloadException.<init>(InvalidPayloadException.java:39)
       at 
org.apache.camel.support.MessageSupport.getMandatoryBody(MessageSupport.java:125)
       at 
org.apache.camel.support.builder.ExpressionBuilder$33.evaluate(ExpressionBuilder.java:1028)
       at 
org.apache.camel.component.bean.MethodInfo$ParameterExpression.evaluate(MethodInfo.java:592)
       at 
org.apache.camel.processor.Splitter.createProcessorExchangePairs(Splitter.java:165)
       at 
org.apache.camel.processor.MulticastProcessor.doProcess(MulticastProcessor.java:347)
   ```
   
   ## Root cause
   
   `getMandatoryBody(Class<T>)` delegates to the type converter without a size 
guard. When converting a multi-GB body to `String`, the type converter tries to 
allocate a `StringBuilder` larger than `Integer.MAX_VALUE`, killing the JVM.
   
   ## Fix
   
   Add `isOversizedInMemoryConversion()` check before the type converter call. 
Uses already-known body length (`CharSequence.length()`, `byte[].length`, 
`StreamCache.length()`) — zero allocation overhead for normal payloads.
   
   **Before:**
   ```
   java.lang.OutOfMemoryError: Required array length 2147483638 + 737144626 is 
too large
   ```
   
   **After (heap survives):**
   ```
   InvalidPayloadException: No body available of type: java.lang.String ...
   Caused by: IllegalStateException: Refusing to convert body of type 
java.lang.String (size=2884627964)
   to java.lang.String because it exceeds the in-memory conversion size limit.
   Use streaming split/tokenize or process body as InputStream/StreamCache.
   ```
   
   Default cap: **256 MiB** — overridable via system property 
`camel.message.max-in-memory-body` (bytes).
   
   ## Changes
   
   - 
`core/camel-support/src/main/java/org/apache/camel/support/MessageSupport.java` 
— OOM guard in `getMandatoryBody(Class<T>)`
   - 
`core/camel-core/src/test/java/org/apache/camel/impl/MessageSupportOversizedBodyTest.java`
 (new) — 6 tests
   
   ## Test Results
   
   ```
   Tests run: 6, Failures: 0, Errors: 0, Skipped: 0  [JDK 21 / Maven 3.9]
   Full camel-support + camel-core suites: BUILD SUCCESS
   ```
   
   ## Note
   
   This fix prevents JVM death and provides an actionable error message. The 
root operational fix is to use `streaming="true"` on the Splitter and process 
large payloads as `InputStream`/`StreamCache` rather than binding the whole 
body as a `String` bean argument.
   
   ## AI Attribution
   
   This contribution was developed with AI assistance using [Claude 
Code](https://github.com/anthropics/claude-code).
   
   ```
   Co-authored-by: Claude Sonnet 4.5 <[email protected]>
   ```


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to