Interface StreamCachingStrategy
- All Superinterfaces:
AutoCloseable, Service, StaticService
Strategy that controls when and how stream-based message bodies (such as
InputStream) are buffered so
they can be re-read, as described in the Stream
Caching documentation.
Without stream caching, a stream body can only be consumed once; any EIP or log statement that reads the body a
second time will see an empty stream. When stream caching is enabled, Camel wraps the body in a StreamCache
that buffers data either in memory or in a temporary spool file on disk, depending on thresholds configured on this
strategy.
The key thresholds are:
setSpoolThreshold(long)— streams larger than this value are spooled to disk rather than held in memory, preventing out-of-memory errors for large payloads.setBufferSize(int)— the internal read-buffer size when copying bytes to the spool file.setSpoolDirectory(java.io.File)— the directory where spool files are written.
getStatistics().- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic interfaceRule for determine if stream caching should be spooled to disk or kept in-memory.static enumUsed for selecting if the memory limit is committed or maximum heap memory setting.static interfaceUtilization statistics of stream caching. -
Method Summary
Modifier and TypeMethodDescriptionvoidAdds theStreamCachingStrategy.SpoolRulerule to be used.@Nullable StreamCacheCaches the value as aStreamCache.@Nullable StreamCacheCaches the body as aStreamCache.@Nullable StreamCacheCaches the body as aStreamCache.Collection<Class<?>> To filter stream caching of a given set of allowed/denied classes.intCollection<Class<?>> To filter stream caching of a given set of allowed/denied classes.@Nullable String@Nullable FilelongintGets the utilization statistics.booleanbooleanbooleanbooleanIs spooling to disk enabled.voidsetAllowClasses(Class<?>... classes) To filter stream caching of a given set of allowed/denied classes.voidsetAllowClasses(String names) To filter stream caching of a given set of allowed/denied classes.voidsetAnySpoolRules(boolean any) Sets whether if just any of theStreamCachingStrategy.SpoolRulerules returns true thenshouldSpoolCache(long)returns true.voidsetBufferSize(int bufferSize) Sets the buffer size to use when allocating in-memory buffers used for in-memory stream caches.voidsetDenyClasses(Class<?>... classes) To filter stream caching of a given set of allowed/denied classes.voidsetDenyClasses(String names) To filter stream caching of a given set of allowed/denied classes.voidsetEnabled(boolean enabled) Sets whether the stream caching is enabled.voidsetRemoveSpoolDirectoryWhenStopping(boolean remove) Whether to remove the temporary directory when stopping.voidsetSpoolCipher(String cipher) Sets a cipher name to use when spooling to disk to write with encryption.voidsetSpoolDirectory(File path) Sets the spool (temporary) directory to use for overflow and spooling to disk.voidsetSpoolDirectory(String path) voidsetSpoolEnabled(boolean spoolEnabled) Enables spooling to disk.voidsetSpoolThreshold(long threshold) Threshold in bytes when overflow to disk is activated.voidSets what the upper bounds should be whensetSpoolUsedHeapMemoryThreshold(int)is in use.voidsetSpoolUsedHeapMemoryThreshold(int percentage) Sets a percentage (1-99) of used heap memory threshold to activate spooling to disk.booleanshouldSpoolCache(long length) Determines if the stream should be spooled or not.
-
Method Details
-
setEnabled
void setEnabled(boolean enabled) Sets whether the stream caching is enabled. Notice: This cannot be changed at runtime. -
isEnabled
boolean isEnabled() -
setAllowClasses
To filter stream caching of a given set of allowed/denied classes. By default, all classes that areInputStreamis allowed. -
setAllowClasses
To filter stream caching of a given set of allowed/denied classes. By default, all classes that areInputStreamis allowed. Multiple class names can be separated by comma. -
getAllowClasses
Collection<Class<?>> getAllowClasses()To filter stream caching of a given set of allowed/denied classes. By default, all classes that areInputStreamis allowed. -
setDenyClasses
To filter stream caching of a given set of allowed/denied classes. By default, all classes that areInputStreamis allowed. -
setDenyClasses
To filter stream caching of a given set of allowed/denied classes. By default, all classes that areInputStreamis allowed. Multiple class names can be separated by comma. -
getDenyClasses
Collection<Class<?>> getDenyClasses()To filter stream caching of a given set of allowed/denied classes. By default, all classes that areInputStreamis allowed. -
setSpoolEnabled
void setSpoolEnabled(boolean spoolEnabled) Enables spooling to disk. Notice: This cannot be changed at runtime. Default is disabled. -
isSpoolEnabled
boolean isSpoolEnabled()Is spooling to disk enabled. -
setSpoolDirectory
Sets the spool (temporary) directory to use for overflow and spooling to disk. If no spool directory has been explicit configured, then a temporary directory is created in the java.io.tmpdir directory. -
getSpoolDirectory
@Nullable File getSpoolDirectory() -
setSpoolDirectory
-
setSpoolThreshold
void setSpoolThreshold(long threshold) Threshold in bytes when overflow to disk is activated. The default threshold isStreamCache.DEFAULT_SPOOL_THRESHOLDbytes (eg 128kb). Use -1 to disable overflow to disk. -
getSpoolThreshold
long getSpoolThreshold() -
setSpoolUsedHeapMemoryThreshold
void setSpoolUsedHeapMemoryThreshold(int percentage) Sets a percentage (1-99) of used heap memory threshold to activate spooling to disk.- Parameters:
percentage- percentage of used heap memory.
-
getSpoolUsedHeapMemoryThreshold
int getSpoolUsedHeapMemoryThreshold() -
setSpoolUsedHeapMemoryLimit
Sets what the upper bounds should be whensetSpoolUsedHeapMemoryThreshold(int)is in use.- Parameters:
bounds- the bounds
-
getSpoolUsedHeapMemoryLimit
@Nullable StreamCachingStrategy.SpoolUsedHeapMemoryLimit getSpoolUsedHeapMemoryLimit() -
setBufferSize
void setBufferSize(int bufferSize) Sets the buffer size to use when allocating in-memory buffers used for in-memory stream caches. The default size isIOHelper.DEFAULT_BUFFER_SIZE -
getBufferSize
int getBufferSize() -
setSpoolCipher
Sets a cipher name to use when spooling to disk to write with encryption. By default the data is not encrypted. -
getSpoolCipher
@Nullable String getSpoolCipher() -
setRemoveSpoolDirectoryWhenStopping
void setRemoveSpoolDirectoryWhenStopping(boolean remove) Whether to remove the temporary directory when stopping. This option is default true -
isRemoveSpoolDirectoryWhenStopping
boolean isRemoveSpoolDirectoryWhenStopping() -
setAnySpoolRules
void setAnySpoolRules(boolean any) Sets whether if just any of theStreamCachingStrategy.SpoolRulerules returns true thenshouldSpoolCache(long)returns true. If this option is false, then all theStreamCachingStrategy.SpoolRulemust return true. The default value is false which means that all the rules must return true. -
isAnySpoolRules
boolean isAnySpoolRules() -
getStatistics
StreamCachingStrategy.Statistics getStatistics()Gets the utilization statistics. -
addSpoolRule
Adds theStreamCachingStrategy.SpoolRulerule to be used. -
shouldSpoolCache
boolean shouldSpoolCache(long length) Determines if the stream should be spooled or not. For example if the stream length is over a threshold. This allows implementations to use custom strategies to determine if spooling is needed or not.- Parameters:
length- the length of the stream- Returns:
- true to spool the cache, or false to keep the cache in-memory
-
cache
Caches the body as aStreamCache.- Parameters:
exchange- the exchange- Returns:
- the body cached as a
StreamCache, or null if not possible or no need to cache the body
-
cache
Caches the body as aStreamCache.- Parameters:
message- the message- Returns:
- the body cached as a
StreamCache, or null if not possible or no need to cache the body
-
cache
Caches the value as aStreamCache.- Parameters:
value- the value- Returns:
- the value cached as a
StreamCache, or null if not possible or no need to cache
-