INT-3606: Implement Closeable/Flushable

JIRA: https://jira.spring.io/browse/INT-3606

Close the store when an (S)FTP synchronizing MessageSource is stopped.

Will need another commit on 4.2.WIP to change to `Lifecycle`.

Add dirty Flag to Properties MetadataStore

Avoid unnecessary persists.

Remove unnecessary implementation of `AbstractInboundFileSynchronizingMessageSource#getComponentType()`
This commit is contained in:
Gary Russell
2015-01-13 15:35:43 +02:00
committed by Artem Bilan
parent 8896d500ed
commit 2581388cc2
8 changed files with 122 additions and 21 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2013-2014 the original author or authors.
* Copyright 2013-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,6 +16,8 @@
package org.springframework.integration.file.filters;
import java.io.Closeable;
import java.io.IOException;
import java.util.List;
import org.springframework.integration.metadata.ConcurrentMetadataStore;
@@ -32,7 +34,7 @@ import org.springframework.util.Assert;
*
*/
public abstract class AbstractPersistentAcceptOnceFileListFilter<F> extends AbstractFileListFilter<F>
implements ReversibleFileListFilter<F> {
implements ReversibleFileListFilter<F>, Closeable {
protected final ConcurrentMetadataStore store;
@@ -78,6 +80,13 @@ public abstract class AbstractPersistentAcceptOnceFileListFilter<F> extends Abst
}
}
@Override
public void close() throws IOException {
if (this.store instanceof Closeable) {
((Closeable) this.store).close();
}
}
/**
* The default value stored for the key is the last modified date.
* @param file The file.

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,6 +16,8 @@
package org.springframework.integration.file.filters;
import java.io.Closeable;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
@@ -37,7 +39,7 @@ import org.springframework.util.Assert;
*
* @param <F> The type that will be filtered.
*/
public class CompositeFileListFilter<F> implements FileListFilter<F> {
public class CompositeFileListFilter<F> implements FileListFilter<F>, Closeable {
private final Set<FileListFilter<F>> fileFilters;
@@ -51,6 +53,15 @@ public class CompositeFileListFilter<F> implements FileListFilter<F> {
}
@Override
public void close() throws IOException {
for (FileListFilter<F> filter : this.fileFilters) {
if (filter instanceof Closeable) {
((Closeable) filter).close();
}
}
}
public CompositeFileListFilter<F> addFilter(FileListFilter<F> filter) {
return this.addFilters(Collections.singletonList(filter));
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -17,6 +17,7 @@
package org.springframework.integration.file.remote.synchronizer;
import java.io.BufferedOutputStream;
import java.io.Closeable;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
@@ -57,7 +58,7 @@ import org.springframework.util.ObjectUtils;
* @since 2.0
*/
public abstract class AbstractInboundFileSynchronizer<F> implements InboundFileSynchronizer,
InitializingBean, IntegrationEvaluationContextAware {
InitializingBean, IntegrationEvaluationContextAware, Closeable {
protected final Log logger = LogFactory.getLog(this.getClass());
@@ -161,6 +162,13 @@ public abstract class AbstractInboundFileSynchronizer<F> implements InboundFileS
return temporaryFileSuffix;
}
@Override
public void close() throws IOException {
if (this.filter instanceof Closeable) {
((Closeable) this.filter).close();
}
}
@Override
public void synchronizeToLocalDirectory(final File localDirectory) {
try {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,12 +16,15 @@
package org.springframework.integration.file.remote.synchronizer;
import java.io.Closeable;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Arrays;
import java.util.Comparator;
import java.util.regex.Pattern;
import org.springframework.integration.core.LifecycleMessageSource;
import org.springframework.integration.endpoint.AbstractMessageSource;
import org.springframework.integration.file.FileReadingMessageSource;
import org.springframework.integration.file.filters.AcceptOnceFileListFilter;
@@ -54,7 +57,10 @@ import org.springframework.util.Assert;
* @author Oleg Zhurakousky
* @author Gary Russell
*/
public abstract class AbstractInboundFileSynchronizingMessageSource<F> extends AbstractMessageSource<File> {
public abstract class AbstractInboundFileSynchronizingMessageSource<F> extends AbstractMessageSource<File>
implements LifecycleMessageSource<File> {
private volatile boolean running;
/**
* Should the endpoint attempt to create the local directory? True by default.
@@ -153,6 +159,29 @@ public abstract class AbstractInboundFileSynchronizingMessageSource<F> extends A
}
}
@Override
public void start() {
this.running = true;
}
@Override
public void stop() {
this.running = false;
if (this.synchronizer instanceof Closeable) {
try {
((Closeable) this.synchronizer).close();
}
catch (IOException e) {
logger.error("Error closing synchronizer", e);
}
}
}
@Override
public boolean isRunning() {
return this.running;
}
/**
* Polls from the file source. If the result is not null, it will be returned.
* If the result is null, it attempts to sync up with the remote directory to populate the file source.