INT-4430: FileSplitter close reader on exception
JIRA: https://jira.spring.io/browse/INT-4430
When Iterator-based `FileSplitter` splits the file, and an exception
throws in downstream flow (in the same thread), the exception propagates
to the caller leaving underlying file reader opened.
This commit changes `AbstractMessageSplitter` the way, that,
when any exception happens, if Iterator implements `java.io.Closeable`,
its `close()` method will be called before propagating exception.
Also `FileSplitter`'s underlying iterator implements `Closeable` now.
* Make `CloseableIterator` to follow `Closeable` contract.
Now `CloseableIterator.close()` declares `IOException` and can be used
as base interface for `FunctionIterator`.
* Adjust tests.
Adjust tests to reflect the fact that we call `close()` on the reader
one more time in the end of iterator.
**Cherry-pick to 5.0.x and 4.3.x**
(cherry picked from commit 7f25cba)
# Conflicts:
# spring-integration-core/src/main/java/org/springframework/integration/splitter/AbstractMessageSplitter.java
# spring-integration-core/src/main/java/org/springframework/integration/util/FunctionIterator.java
# spring-integration-file/src/main/java/org/springframework/integration/file/splitter/FileSplitter.java
# spring-integration-file/src/test/java/org/springframework/integration/file/remote/StreamingInboundTests.java
# spring-integration-file/src/test/java/org/springframework/integration/file/splitter/FileSplitterTests.java
This commit is contained in:
committed by
Artem Bilan
parent
f1f876e0d2
commit
c45f9a6544
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2016 the original author or authors.
|
||||
* Copyright 2002-2018 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,7 @@
|
||||
|
||||
package org.springframework.integration.splitter;
|
||||
|
||||
import java.io.Closeable;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
@@ -36,6 +37,7 @@ import org.springframework.messaging.Message;
|
||||
* @author Mark Fisher
|
||||
* @author Dave Syer
|
||||
* @author Artem Bilan
|
||||
* @author Ruslan Stelmachenko
|
||||
*/
|
||||
public abstract class AbstractMessageSplitter extends AbstractReplyProducingMessageHandler {
|
||||
|
||||
@@ -154,10 +156,26 @@ public abstract class AbstractMessageSplitter extends AbstractReplyProducingMess
|
||||
|
||||
@Override
|
||||
protected void produceOutput(Object result, Message<?> requestMessage) {
|
||||
Iterator<?> iterator = (Iterator<?>) result;
|
||||
while (iterator.hasNext()) {
|
||||
super.produceOutput(iterator.next(), requestMessage);
|
||||
|
||||
if (result instanceof Iterator<?>) {
|
||||
Iterator<?> iterator = (Iterator<?>) result;
|
||||
try {
|
||||
while (iterator.hasNext()) {
|
||||
super.produceOutput(iterator.next(), requestMessage);
|
||||
}
|
||||
}
|
||||
finally {
|
||||
if (iterator instanceof Closeable) {
|
||||
try {
|
||||
((Closeable) iterator).close();
|
||||
}
|
||||
catch (Exception e) {
|
||||
// ignored
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
super.produceOutput(result, requestMessage);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
/*
|
||||
* Copyright 2018 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.integration.util;
|
||||
|
||||
import java.io.Closeable;
|
||||
import java.util.Iterator;
|
||||
|
||||
/**
|
||||
* A {@link CloseableIterator} is intended to be used when it may hold resources (such as file or socket handles).
|
||||
* This allows implementations to clean up any resources they need to keep open to iterate over elements.
|
||||
*
|
||||
* @author Ruslan Stelmachenko
|
||||
*
|
||||
* @since 4.3.15
|
||||
*/
|
||||
public interface CloseableIterator<E> extends Iterator<E>, Closeable {
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2014-2016 the original author or authors.
|
||||
* Copyright 2014-2018 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.util;
|
||||
|
||||
import java.io.Closeable;
|
||||
import java.io.IOException;
|
||||
import java.util.Iterator;
|
||||
|
||||
/**
|
||||
@@ -23,9 +25,10 @@ import java.util.Iterator;
|
||||
* {@link #iterator} to a new object applying the {@link #function} on {@link #next()}.
|
||||
*
|
||||
* @author Artem Bilan
|
||||
* @author Ruslan Stelmachenko
|
||||
* @since 4.1
|
||||
*/
|
||||
public final class FunctionIterator<T, V> implements Iterator<V> {
|
||||
public final class FunctionIterator<T, V> implements CloseableIterator<V> {
|
||||
|
||||
private final Iterator<T> iterator;
|
||||
|
||||
@@ -55,5 +58,11 @@ public final class FunctionIterator<T, V> implements Iterator<V> {
|
||||
return this.function.apply(this.iterator.next());
|
||||
}
|
||||
|
||||
}
|
||||
@Override
|
||||
public void close() throws IOException {
|
||||
if (this.iterator instanceof Closeable) {
|
||||
((Closeable) this.iterator).close();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user