Skip to content

Instantly share code, notes, and snippets.

@robinhowlett
Created July 25, 2012 06:40
Show Gist options
  • Save robinhowlett/3174786 to your computer and use it in GitHub Desktop.
Save robinhowlett/3174786 to your computer and use it in GitHub Desktop.
How to find only the files with unique valid content in a directory with Camel
package com.sc;
import org.apache.camel.Exchange;
import org.apache.camel.Handler;
import org.apache.camel.Header;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.processor.idempotent.MemoryIdempotentRepository;
/**
* A Camel Java DSL Router
*/
public class MyRouteBuilder extends RouteBuilder {
private int counter = 0;
/**
* The incoming file name has the timestamp as part of it, so we are just replacing that with a counter
* @param fileName
* @return
*/
@Handler
public String setFileName(@Header(Exchange.FILE_NAME) String fileName) {
return fileName.replaceAll("\\d+", Integer.toString(counter++));
}
public void configure() {
/*
* Read in all the files in a directory, filter by file name, filter by valid content,
* use idempotentConsumer to exclude duplicate file content, rename the file, write the file
*/
from("file:src/data/m-footbl?noop=true")
.filter(header(Exchange.FILE_NAME).contains("ftp"))
.filter(body().startsWith("<fbgame"))
.idempotentConsumer(body(String.class), MemoryIdempotentRepository.memoryIdempotentRepository(1000))
.setHeader(Exchange.FILE_NAME, method(MyRouteBuilder.class))
.to("file:src/data/m-footbl-uniq")
;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment