...
Option | Default | Description | ||
---|---|---|---|---|
| | Base directory where temporary files for spooled streams should be stored. This option supports naming patterns as documented below. | ||
| | If set, the temporary files are encrypted using the specified cipher transformation (i.e., a valid stream or 8-bit cipher name such as "RC4", "AES/CTR/NoPadding". An empty name "" is treated as null). | ||
| | Size in bytes when the stream should be spooled to disk instead of keeping in memory. Use a value of 0 or negative to disable it all together so streams is always kept in memory regardless of their size. | ||
| | A percentage (1 to 99) of current used heap memory to use as threshold for spooling streams to disk. The upper bounds is based on heap committed (guaranteed memory the JVM can claim). This can be used to spool to disk when running low on memory. | ||
| | Whether any or all | | If set, the temporary files are encrypted using the specified cipher transformation (i.e., a valid stream or 8-bit cipher name such as "RC4", "AES/CTR/NoPadding". An empty name "" is treated as null). |
| | Initial size if in-memory created stream buffers. | ||
| | Whether to remove the spool directory when stopping CamelContext. | ||
| | Whether utilization statistics is enabled. By enabling this you can see these statics for example with JMX. |
...
Code Block | ||||
---|---|---|---|---|
| ||||
<!-- 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 spoolHeapMemoryWatermarkThreshold
By default stream caching will spool only big payloads (128kb or bigger) to disk. However you can also set the spoolHeapMemoryWatermarkThreshold 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:
Code Block |
---|
<streamCaching id="myCacheConfig" spoolDirectory="/tmp/cachedir" spoolHeapMemoryWatermarkThreshold="70"/> |
Then notice that as spoolThreshold is default enabled with 128kb, then we have both thresholds in use (spoolThreads and spoolHeapMemoryWatermarkThreshold). 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:
Code Block |
---|
<streamCaching id="myCacheConfig" spoolDirectory="/tmp/cachedir" spoolHeapMemoryWatermarkThreshold="70" anySpoolRules="true"/>
|
If we only want to spool to disk if we run low on memory then we can set:
Code Block |
---|
<streamCaching id="myCacheConfig" spoolDirectory="/tmp/cachedir" spoolThreshold="-1" spoolHeapMemoryWatermarkThreshold="70"/>
|
... then we do not use the spoolThreshold rule, and only the heap memory based is in use.
Using custom SpoolRule implementations
You 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:
Code Block |
---|
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:
Code Block |
---|
SpoolRule mySpoolRule = ...
context.getStreamCachingStrategy().addSpoolRule(mySpoolRule);
|
And from XML you need to define a <bean> with your custom rule
Code Block | ||||
---|---|---|---|---|
| ||||
<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.
Code Block | ||||
---|---|---|---|---|
| ||||
<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.