Class FileReadingMessageSource
- All Implemented Interfaces:
- Aware,- BeanFactoryAware,- BeanNameAware,- DisposableBean,- InitializingBean,- Lifecycle,- MessageSource<File>,- IntegrationPattern,- NamedComponent,- IntegrationInboundManagement,- IntegrationManagement,- ManageableLifecycle
MessageSource that creates messages
 from a file system directory.
 To prevent messages for certain files, you may supply a FileListFilter.
 By default, when configuring with XML or the DSL,
 an AcceptOnceFileListFilter is used.
 It ensures files are picked up only once from the directory.
 
 A common problem with reading files is that a file may be detected before it
 is ready. The default
 AcceptOnceFileListFilter
 does not prevent this. In most cases, this can be prevented if the
 file-writing process renames each file as soon as it is ready for reading. A
 pattern-matching filter that accepts only files that are ready (e.g. based on
 a known suffix), composed with the default
 AcceptOnceFileListFilter
 would allow for this.
 
 If a external DirectoryScanner is used, then the FileLocker
 and FileListFilter objects should be set on the external
 DirectoryScanner, not the instance of FileReadingMessageSource. An
 IllegalStateException will result otherwise.
 
 A Comparator can be used to ensure internal ordering of the Files in
 a PriorityBlockingQueue. This does not provide the same guarantees as
 a ResequencingMessageGroupProcessor,
 but in cases where writing files
 and failure downstream are rare it might be sufficient.
 
 FileReadingMessageSource is fully thread-safe under concurrent
 receive() invocations and message delivery callbacks.
- Author:
- Iwein Fuld, Mark Fisher, Oleg Zhurakousky, Gary Russell, Artem Bilan, Steven Pearce, Patryk Ziobron
- 
Nested Class SummaryNested ClassesNested classes/interfaces inherited from interface org.springframework.integration.support.management.IntegrationManagementIntegrationManagement.ManagementOverrides
- 
Field SummaryFields inherited from class org.springframework.integration.util.AbstractExpressionEvaluatorEXPRESSION_PARSER, loggerFields inherited from interface org.springframework.integration.support.management.IntegrationManagementMETER_PREFIX, RECEIVE_COUNTER_NAME, SEND_TIMER_NAME
- 
Constructor SummaryConstructorsConstructorDescriptionCreate a FileReadingMessageSource with a naturally ordered queue of unbounded capacity.FileReadingMessageSource(int internalQueueCapacity) Create a FileReadingMessageSource with a bounded queue of the given capacity.FileReadingMessageSource(Comparator<File> receptionOrderComparator) Create a FileReadingMessageSource with aPriorityBlockingQueueordered with the passed inComparator.
- 
Method SummaryModifier and TypeMethodDescriptionprotected AbstractIntegrationMessageBuilder<File> Subclasses must implement this method.Thescannerproperty accessor to allow to modify its options (filter,lockeretc.) at runtime using theFileReadingMessageSourcebean.booleanbooleanvoidAdds the failed message back to the 'toBeReceived' queue if there is room.protected voidonInit()voidsetAutoCreateDirectory(boolean autoCreateDirectory) Specify whether to create the source directory automatically if it does not yet exist upon initialization.voidsetDirectory(File directory) Specify the input directory.voidsetFilter(FileListFilter<File> filter) Set aFileListFilter.voidsetLocker(FileLocker locker) Set aFileLockerto be used to guard files against duplicate processing.voidsetScanEachPoll(boolean scanEachPoll) Set this flag if you want to make sure the internal queue is refreshed with the latest content of the input directory on each poll.voidsetScanner(DirectoryScanner scanner) Optionally specify a custom scanner, for example theFileReadingMessageSource.WatchServiceDirectoryScanner.voidsetUseWatchService(boolean useWatchService) Switch thisFileReadingMessageSourceto use its internalFileReadingMessageSource.WatchServiceDirectoryScanner.voidsetWatchDirPredicate(Predicate<Path> watchDirPredicate) Set aPredicateto check a directory in theFiles.walkFileTree(Path, Set, int, FileVisitor)call if it is eligible forWatchService.voidsetWatchEvents(FileReadingMessageSource.WatchEventType... watchEvents) TheWatchServiceevent types.voidsetWatchMaxDepth(int watchMaxDepth) Set a max depth for theFiles.walkFileTree(Path, Set, int, FileVisitor)API whenuseWatchServiceis enabled.voidstart()voidstop()Methods inherited from class org.springframework.integration.endpoint.AbstractMessageSourcebuildMessage, destroy, getBeanName, getComponentName, getManagedName, getManagedType, getOverrides, isLoggingEnabled, receive, registerMetricsCaptor, setBeanName, setHeaderExpressions, setLoggingEnabled, setManagedName, setManagedTypeMethods inherited from class org.springframework.integration.util.AbstractExpressionEvaluatorafterPropertiesSet, evaluateExpression, evaluateExpression, evaluateExpression, evaluateExpression, evaluateExpression, evaluateExpression, evaluateExpression, getBeanFactory, getEvaluationContext, getEvaluationContext, getMessageBuilderFactory, setBeanFactory, setConversionService, setSimpleEvaluationContextMethods inherited from class java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.springframework.integration.support.management.IntegrationManagementgetThisAs, isObserved, registerObservationRegistryMethods inherited from interface org.springframework.integration.core.MessageSourcegetIntegrationPatternType
- 
Constructor Details- 
FileReadingMessageSourcepublic FileReadingMessageSource()Create a FileReadingMessageSource with a naturally ordered queue of unbounded capacity.
- 
FileReadingMessageSourcepublic FileReadingMessageSource(int internalQueueCapacity) Create a FileReadingMessageSource with a bounded queue of the given capacity. This can be used to reduce the memory footprint of this component when reading from a large directory.- Parameters:
- internalQueueCapacity- the size of the queue used to cache files to be received internally. This queue can be made larger to optimize the directory scanning. With scanEachPoll set to false and the queue to a large size, it will be filled once and then completely emptied before a new directory listing is done. This is particularly useful to reduce scans of large numbers of files in a directory.
 
- 
FileReadingMessageSourceCreate a FileReadingMessageSource with aPriorityBlockingQueueordered with the passed inComparator.The size of the queue used should be large enough to hold all the files in the input directory in order to sort all of them, so restricting the size of the queue is mutually exclusive with ordering. No guarantees about file delivery order can be made under concurrent access. - Parameters:
- receptionOrderComparator- the comparator to be used to order the files in the internal queue
 
 
- 
- 
Method Details- 
setDirectorySpecify the input directory.- Parameters:
- directory- to monitor
 
- 
setScannerOptionally specify a custom scanner, for example theFileReadingMessageSource.WatchServiceDirectoryScanner.- Parameters:
- scanner- scanner implementation
 
- 
getScannerThescannerproperty accessor to allow to modify its options (filter,lockeretc.) at runtime using theFileReadingMessageSourcebean.- Returns:
- the DirectoryScannerof thisFileReadingMessageSource.
- Since:
- 4.2
 
- 
setAutoCreateDirectorypublic void setAutoCreateDirectory(boolean autoCreateDirectory) Specify whether to create the source directory automatically if it does not yet exist upon initialization. By default, this value is true. If set to false and the source directory does not exist, an Exception will be thrown upon initialization.- Parameters:
- autoCreateDirectory- should the directory to be monitored be created when this component starts up?
 
- 
setFilterSet aFileListFilter. By default aAcceptOnceFileListFilterwith no bounds is used. In most cases a customizedFileListFilterwill be needed to deal with modification and duplication concerns. If multiple filters are required aCompositeFileListFiltercan be used to group them together.The supplied filter must be thread safe.. - Parameters:
- filter- a filter
 
- 
setLockerSet aFileLockerto be used to guard files against duplicate processing.The supplied FileLocker must be thread safe - Parameters:
- locker- a locker
 
- 
setScanEachPollpublic void setScanEachPoll(boolean scanEachPoll) Set this flag if you want to make sure the internal queue is refreshed with the latest content of the input directory on each poll.By default, this implementation will empty its queue before looking at the directory again. In cases where order is relevant it is important to consider the effects of setting this flag. The internal BlockingQueuethat this class is keeping will more likely be out of sync with the file system if this flag is set to false, but it will change more often (causing expensive reordering) if it is set to true.- Parameters:
- scanEachPoll- whether the component should re-scan (as opposed to not rescanning until the entire backlog has been delivered)
 
- 
setUseWatchServicepublic void setUseWatchService(boolean useWatchService) Switch thisFileReadingMessageSourceto use its internalFileReadingMessageSource.WatchServiceDirectoryScanner.- Parameters:
- useWatchService- the- booleanflag to switch to- FileReadingMessageSource.WatchServiceDirectoryScanneron- true.
- Since:
- 4.3
- See Also:
 
- 
isUseWatchServicepublic boolean isUseWatchService()
- 
setWatchEvents- Parameters:
- watchEvents- the set of- FileReadingMessageSource.WatchEventType.
- Since:
- 4.3
- See Also:
 
- 
setWatchMaxDepthpublic void setWatchMaxDepth(int watchMaxDepth) Set a max depth for theFiles.walkFileTree(Path, Set, int, FileVisitor)API whenuseWatchServiceis enabled. Defaults toInteger.MAX_VALUE- walk the whole tree.- Parameters:
- watchMaxDepth- the depth for- Files.walkFileTree(Path, Set, int, FileVisitor).
- Since:
- 6.1
 
- 
setWatchDirPredicateSet aPredicateto check a directory in theFiles.walkFileTree(Path, Set, int, FileVisitor)call if it is eligible forWatchService.- Parameters:
- watchDirPredicate- the- Predicateto check dirs for walking.
- Since:
- 6.1
 
- 
getComponentType- Specified by:
- getComponentTypein interface- NamedComponent
 
- 
startpublic void start()- Specified by:
- startin interface- Lifecycle
- Specified by:
- startin interface- ManageableLifecycle
 
- 
stoppublic void stop()- Specified by:
- stopin interface- Lifecycle
- Specified by:
- stopin interface- ManageableLifecycle
 
- 
isRunningpublic boolean isRunning()- Specified by:
- isRunningin interface- Lifecycle
- Specified by:
- isRunningin interface- ManageableLifecycle
 
- 
onInitprotected void onInit()- Overrides:
- onInitin class- AbstractExpressionEvaluator
 
- 
doReceiveDescription copied from class:AbstractMessageSourceSubclasses must implement this method. Typically, the returned value will be thepayloadof type T, but the returned value may also be aMessageinstance whose payload is of type T; also can beAbstractIntegrationMessageBuilderwhich is used for additional headers population.- Specified by:
- doReceivein class- AbstractMessageSource<File>
- Returns:
- The value returned.
 
- 
onFailure
 
-