Skip to content

Instantly share code, notes, and snippets.

@garyrussell
Last active May 31, 2016 11:28
Show Gist options
  • Save garyrussell/921e366334b5d8d9b8f2 to your computer and use it in GitHub Desktop.
Save garyrussell/921e366334b5d8d9b8f2 to your computer and use it in GitHub Desktop.
File Splitter **This example is now obsolete - use the FileSplitter that's now part of the framework**
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:int="http://www.springframework.org/schema/integration"
xmlns:int-file="http://www.springframework.org/schema/integration/file"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/integration/file http://www.springframework.org/schema/integration/file/spring-integration-file.xsd
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd">
<int-file:inbound-channel-adapter directory="/tmp/test"
id="filesIn" channel="toSplitter">
<int:poller fixed-delay="5000" />
</int-file:inbound-channel-adapter>
<int:splitter input-channel="toSplitter" output-channel="logger"
ref="fileSplitter" method="split" />
<int:logging-channel-adapter id="logger" level="WARN"/>
<bean id="fileSplitter" class="foo.FileSplitter" />
</beans>
/*
* Copyright 2014 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 foo;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Iterator;
/**
* @author Gary Russell
*
*/
public class FileSplitter {
public Iterator<String> split(final File file) throws IOException {
return new Iterator<String>() {
BufferedReader reader = new BufferedReader(new FileReader(file));
@Override
public boolean hasNext() {
try {
boolean ready = reader.ready();
if (!ready) {
reader.close();;
}
return ready;
}
catch (IOException e) {
try {
reader.close();
}
catch (IOException e1) {}
throw new RuntimeException(e);
}
}
@Override
public String next() {
try {
return reader.readLine();
}
catch (IOException e) {
try {
reader.close();
}
catch (IOException e1) {}
throw new RuntimeException(e);
}
}
};
}
}
@mbgold78
Copy link

Hi Gary, I am using int-file:splitter which works from the xml, but I dont see it in the integration graph. Please advise.

Thanks
Mike

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment