Stream cachingPage edited by Claus IbsenChanges (9)
Full ContentStream cachingWhile stream types (like StreamSource, InputStream and Reader) are commonly used in messaging for performance reasons, they also have an important drawback: they can only be read once. In order to be able to work with message content multiple times, the stream needs to be cached. Streams are caching in memory. In Camel 2.0, large stream messages (over 64 Kb in Camel 2.11 or older, and 128 kb from Camel 2.12 onwards) will be cached in a temporary file instead – Camel itself will handle deleting the temporary file once the cached stream is no longer necessary. In Camel 2.0 stream cache is default disabled out of the box.
Enabling stream cachingIn Apache Camel, you can explicitly enable stream caching for a single route with the streamCaching DSL method: from("jbi:service:http://foo.bar.org/MyService") .streamCaching() .to("jbi:service:http://foo.bar.org/MyOtherService");
A could of examples, to store in the java temp directory with a sub directory using the CamelContext name: context.getStreamCachingStrategy().setSpoolDirectory"${java.io.tmpdir}#name#/"); To store in KARAF_HOME/tmp/bundleId directory context.getStreamCachingStrategy().setSpoolDirectory"${env:KARAF_HOME}/tmp/bundle#bundleId#"); Using StreamCachingStrategy in JavaYou can configure the StreamCachingStrategy in Java as shown below: context.getStreamCachingStrategy().setSpoolDirectory"/tmp/cachedir"); context.getStreamCachingStrategy().setSpoolThreshold(64 * 1024); context.getStreamCachingStrategy().setBufferSize(16 * 1024); // to enable encryption using RC4 // context.getStreamCachingStrategy().setSpoolChiper("RC4"); And remember to enable Stream caching on the CamelContext or on routes context.setStreamCaching(true); Using StreamCachingStrategy in XMLAnd in XML you do: <camelContext streamCache="true" xmlns="http://camel.apache.org/schema/blueprint"> <streamCaching id="myCacheConfig" bufferSize="16384" spoolDirectory="/tmp/cachedir" spoolThreshold="65536"/> <route> <from uri="direct:c"/> <to uri="mock:c"/> </route> </camelContext> You can also define a <bean> instead of using the <streamCaching> tag: And in XML you do <!-- define a bean of type StreamCachingStrategy which CamelContext will automatic use --> <bean id="streamStrategy" class="org.apache.camel.impl.DefaultStreamCachingStrategy"> <property name="spoolDirectory" value="/tmp/cachedir"/> <property name="spoolThreshold" value="65536"/> <property name="bufferSize" value="16384"/> </bean> <!-- remember to enable stream caching --> <camelContext streamCaching="true" xmlns="http://camel.apache.org/schema/spring"> Using spoolUsedHeapMemoryThresholdBy default stream caching will spool only big payloads (128kb or bigger) to disk. However you can also set the spoolUsedHeapMemoryThreshold option which is a percentage of used heap memory. This can be used to also spool to disk when running low on memory. For example with: <streamCaching id="myCacheConfig" spoolDirectory="/tmp/cachedir" spoolUsedHeapMemoryThreshold="70"/> Then notice that as spoolThreshold is default enabled with 128kb, then we have both thresholds in use (spoolThreshold and spoolUsedHeapMemoryThreshold). And in this example then we only spool to disk if payload is > 128kb and that used heap memory is > 70%. The reason is that we have the option anySpoolRules as default false. That means both rules must be true (eg AND). If we want to spool to disk if either of the rules (eg OR), then we can do: <streamCaching id="myCacheConfig" spoolDirectory="/tmp/cachedir" spoolUsedHeapMemoryThreshold="70" anySpoolRules="true"/> If we only want to spool to disk if we run low on memory then we can set: <streamCaching id="myCacheConfig" spoolDirectory="/tmp/cachedir" spoolThreshold="-1" spoolUsedHeapMemoryThreshold="70"/> ... then we do not use the spoolThreshold rule, and only the heap memory based is in use. By default the upper limit of the used heap memory is based on the maximum heap size. Though you can also configure to use the committed heap size as the upper limit, this is done using the spoolUsedHeapMemoryThreshold option as shown below: <streamCaching id="myCacheConfig" spoolDirectory="/tmp/cachedir" spoolUsedHeapMemoryThreshold="70" spoolUsedHeapMemoryLimit="Committed"/> Using custom SpoolRule implementationsYou can implement your custom rules to determine if the stream should be spooled to disk. This can be done by implementing the interface org.apache.camel.spi.StreamCachingStrategy.SpoolRule which has a single method: boolean shouldSpoolCache(long length); The length is the length of the stream. To use the rule then add it to the StreamCachingStrategy as shown below: SpoolRule mySpoolRule = ... context.getStreamCachingStrategy().addSpoolRule(mySpoolRule); And from XML you need to define a <bean> with your custom rule <bean id="mySpoolRule" class="com.foo.MySpoolRule"/> <streamCaching id="myCacheConfig" spoolDirectory="/tmp/cachedir" spoolRules="mySpoolRule"/> Using the spoolRules attribute on <streamCaching>. if you have more rules, then separate them by comma. <streamCaching id="myCacheConfig" spoolDirectory="/tmp/cachedir" spoolRules="mySpoolRule,myOtherSpoolRule"/> How it works?In order to determine if a type requires caching, we leverage the type converter feature. Any type that requires stream caching can be converted into an org.apache.camel.StreamCache instance.
Stop watching space
|
Change email notification preferences
View Online
|
View Changes
|
Add Comment
|
- [CONF] Apache Camel > Stream caching confluence
- [CONF] Apache Camel > Stream caching confluence
- [CONF] Apache Camel > Stream caching Claus Ibsen (Confluence)
- [CONF] Apache Camel > Stream caching Claus Ibsen (Confluence)
- [CONF] Apache Camel > Stream caching Claus Ibsen (Confluence)
- [CONF] Apache Camel > Stream caching Claus Ibsen (Confluence)
- [CONF] Apache Camel > Stream caching Claus Ibsen (Confluence)
- [CONF] Apache Camel > Stream caching Claus Ibsen (Confluence)
- [CONF] Apache Camel > Stream caching Claus Ibsen (Confluence)
- [CONF] Apache Camel > Stream caching Claus Ibsen (Confluence)
- [CONF] Apache Camel > Stream caching Claus Ibsen (Confluence)