Skip to content

INT-4185: Fix FileReadingMessageSource for Java 6 #2004

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
import org.springframework.messaging.Message;
import org.springframework.messaging.MessagingException;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;

/**
* {@link MessageSource} that creates messages from a file system directory.
Expand Down Expand Up @@ -85,6 +86,9 @@
*/
public class FileReadingMessageSource extends IntegrationObjectSupport implements MessageSource<File>, Lifecycle {

private static boolean WATCH_SERVICE_PRESENT =
ClassUtils.isPresent("java.nio.file.WatchService", ClassUtils.getDefaultClassLoader());

private static final int DEFAULT_INTERNAL_QUEUE_CAPACITY = 5;

private static final Log logger = LogFactory.getLog(FileReadingMessageSource.class);
Expand Down Expand Up @@ -114,7 +118,7 @@ public class FileReadingMessageSource extends IntegrationObjectSupport implement

private boolean useWatchService;

private WatchEventType[] watchEvents = new WatchEventType[] { WatchEventType.CREATE };
private WatchEventType[] watchEvents;

/**
* Creates a FileReadingMessageSource with a naturally ordered queue of unbounded capacity.
Expand Down Expand Up @@ -161,6 +165,9 @@ public FileReadingMessageSource(int internalQueueCapacity) {
public FileReadingMessageSource(Comparator<File> receptionOrderComparator) {
this.toBeReceived = new PriorityBlockingQueue<File>(
DEFAULT_INTERNAL_QUEUE_CAPACITY, receptionOrderComparator);
if (WATCH_SERVICE_PRESENT) {
this.watchEvents = new WatchEventType[] { WatchEventType.CREATE };
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

else {
    this.useWatchService = false;
}

Also, need to prevent setting to true (perhaps events too) - and reflect that in the javadocs.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah! Agree about Assert.state() in the setUseWatchService(), but not sure what that event should mean...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean add an assertion in setWatchEvents() too.

}


Expand Down Expand Up @@ -263,24 +270,30 @@ public void setScanEachPoll(boolean scanEachPoll) {

/**
* Switch this {@link FileReadingMessageSource} to use its internal
* {@link FileReadingMessageSource.WatchServiceDirectoryScanner}.
* {@link FileReadingMessageSource.WatchServiceDirectoryScanner} based on the Java 7 {@link WatchService}.
* @param useWatchService the {@code boolean} flag to switch to
* {@link FileReadingMessageSource.WatchServiceDirectoryScanner} on {@code true}.
* @throws IllegalArgumentException if {@link WatchService} isn't available, e.g. Java 6 environment.
* @since 4.3
* @see #setWatchEvents
*/
public void setUseWatchService(boolean useWatchService) {
Assert.isTrue(!useWatchService || WATCH_SERVICE_PRESENT,
"'java.nio.file.WatchService' is available only since Java 7.");
this.useWatchService = useWatchService;
}

/**
* The {@link WatchService} event types.
* If {@link #setUseWatchService} isn't {@code true}, this option is ignored.
* @param watchEvents the set of {@link WatchEventType}.
* @throws IllegalArgumentException if {@link WatchService} isn't available, e.g. Java 6 environment.
* @since 4.3
* @see #setUseWatchService
*/
public void setWatchEvents(WatchEventType... watchEvents) {
Assert.isTrue(!this.useWatchService || WATCH_SERVICE_PRESENT,
"'java.nio.file.WatchService' is available only since Java 7.");
Assert.notEmpty(watchEvents, "'watchEvents' must not be empty.");
Assert.noNullElements(watchEvents, "'watchEvents' must not contain null elements.");
Assert.state(!this.running.get(), "Cannot change watch events while running.");
Expand Down