Skip to content

Instantly share code, notes, and snippets.

@ptaylor
Created September 28, 2015 08:15
Show Gist options
  • Save ptaylor/77ab515dc55ed5c51964 to your computer and use it in GitHub Desktop.
Save ptaylor/77ab515dc55ed5c51964 to your computer and use it in GitHub Desktop.
Output stream what writes to a FTP file via Camel.
class ExampleCamelFtpStream {
static void main(String[] args) {
new ExampleCamelFtpStream().run()
}
void run() {
try {
CamelContext camelContext = new DefaultCamelContext();
Map options = [
//username: 'test',
//password: 'test777',
fileName: 'OUT.txt'
]
OutputStream os = getFtpOutputStream("file:///tmp", options, camelContext)
(1..100).each {
println "line ${it}"
os << "This is line number ${it}\n"
sleep 50
}
os.close()
} catch (Exception e) {
println "EXCEPTION ${e}"
e.printStackTrace()
}
}
OutputStream getFtpOutputStream(String baseUrl, Map config, CamelContext camelContext) {
String url = buildUrl(baseUrl, config)
println "URL ${url}"
ProducerTemplate producer = camelContext.createProducerTemplate()
producer.start()
PipedOutputStream pos = new PipedOutputStream()
PipedInputStream pis = new PipedInputStream(pos)
Future<Object> future = producer.asyncSendBody(url, pis)
pos
}
String buildUrl(String baseUrl, Map config) {
String optionString = config.findAll {k, v -> v != null}.collect { k, v -> "${k}=${v}" }.join('&')
"${baseUrl}?${optionString}"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment