You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by edvicif <ed...@gmail.com> on 2012/02/23 17:10:36 UTC

Re: How to grab a (only one) file with camel ?

I have a solution. Not sure this is the final form.

Dynamic Router Factory


I've created a DynamicRouteBuilder. Which dynamically create a route, with a
direct entry point and a configurable exit point.

class DynamicRouteBuilder extends RouteBuilder{
  
  private final String enrichUri;
  private final String outUri;
  private final String routeId;
  private static final AggregationStrategy MY_AGGREGATION_STRATEGY = new
MyAggregationStrategy();

  public DynamicRouteBuilder(String enrichUri, String outUri, String
routeId) {
    this.enrichUri = enrichUri;
    this.outUri = outUri;
    this.routeId = routeId;
  }

  //Dynamic route creation
  @Override
  public void configure() throws Exception {
    from("direct:"+routeId).id(routeId) //Dynamic entry point
      .pollEnrich(enrichUri, 2000, MY_AGGREGATION_STRATEGY)
      .to(outUri) //Exit point, where we can wait staically to continue
processing
      .process(new *ShutDownProcessor*());
  }
  
  //Route shutdown it self after completion
  private final class *ShutDownProcessor* implements Processor {
    @Override
    public void process(Exchange exchange) throws Exception {
      CamelContext camelContext = exchange.getContext();
      camelContext.getInflightRepository().remove(exchange);
      /camelContext.stopRoute(routeId);
      camelContext.removeRoute(routeId);/
    }
  }
  ... Aggregation Strategy implemetation left off to spare some space
}

The router expects a name, which will be served as an entry point to this
route. We enrich it, than forwardingit to an existing static route.

After sending out the message basically we call the Shutdown processor,
which destroys the route. So basically the route exist for one message.
There could be an issue if because async we send multiple message. But that
is why routeId is configruable. Maybe with combining 
http://camel.apache.org/dynamic-router.html dynamic router EIP  it can be
solved.

Invoking Dynamic Route


 from("direct:fileCopier")
            .process(new Processor() {
              @Override
              public void process(Exchange exchange) throws Exception {
                String fileName = exchange.getIn().getBody(String.class);
                DynamicRouteBuilder dynamicRouteBuilder = new
DynamicRouteBuilder("file:target/test-classes/fileTest?fileName=" +
fileName, 
                    "direct:*eventFileProducer*",
                    "*eventFilePooler*"); //dynamic route name
                exchange.getContext().addRoutes(dynamicRouteBuilder);
              }
            })
            .to("direct:*eventFilePooler*"); //sending the message to the
dynamic route

The fileCopier route just creates the dynamic route with a specific direct
endpoint. It specify the file, what he want to consume using enpoint uri and
where I'd like to route the results (direct:eventFileProducer)

Exitpoint of Dynamic Route


So from now on I have a statical route to continue the processing
from("direct:*eventFileProducer*").to("log:doEverything as usual");

I don't know this is what we are looking for.

--
View this message in context: http://camel.465427.n5.nabble.com/How-to-grab-a-only-one-file-with-camel-tp4930104p5508497.html
Sent from the Camel - Users mailing list archive at Nabble.com.