Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Info
titleStreamCache Affects your payload object

The StreamCache will affect your payload object as it will replace the Stream payload with a org.apache.camel.StreamCache object.
This StreamCache is capable of being re-readable and thus possible to better be routed within Camel using redelivery or Content Based Router or the likes.

However to not change the payload under the covers without the end user really knowing we changed the default in Camel 2.0 to disabled. So in Camel 2.0 you have to explicit enable it if you want to use it.

Tip

If using Camel 2.12 onwards then see about StreamCachingStrategy further below which is the recommended way to configure stream caching options.

Enabling stream caching

In Apache Camel, you can explicitly enable stream caching for a single route with the streamCaching DSL method:

...

Code Block
xml
xml
<camelContext xmlns="http://camel.apache.org/schema/blueprint">

  <!-- disable stream caching spool to disk -->
  <properties>
    <property key="CamelCachedOutputStreamThreshold" value="-1"/>
  </properties>

Using StreamCachingStrategy

Available as of Camel 2.12

Stream caching is from Camel 2.12 onwards intended to be configured using org.apache.camel.spi.StreamCachingStrategy.
The old kind of configuration using properties on the CamelContext has been marked as deprecated.

The strategy has the following options:

Option

Default

Description

temporaryDirectory

java.io.tmpdir

Base directory where temporary files for spooled streams should be stored.

spoolThreshold

128kb

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.

spoolChiper

null

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

bufferSize

4096

Size in bytes of the buffer used in the stream.

removeTemporaryDirectoryWhenStopping

true

Whether to remove the temporary directory when stopping CamelContext.

Using StreamCachingStrategy in Java

You can configure the StreamCachingStrategy in Java as shown below:

Code Block

context.getStreamCachingStrategy().setTemporaryDirectory"/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

Code Block

context.setStreamCaching(true);

Using StreamCachingStrategy in XML

And in XML you do

Code Block
xml
xml

<!-- define a bean of type StreamCachingStrategy which CamelContext will automatic use -->
<bean id="streamStrategy" class="org.apache.camel.impl.DefaultStreamCachingStrategy">
  <property name="temporaryDirectory" 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">

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.