You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by "kraythe ." <kr...@gmail.com> on 2014/03/02 18:05:55 UTC

Re: Is apache-camel supports nested route

That won't work the way you want. There are much better ways of doing this.
Specifically you can use a to("direct:xxx") to do some processing that is
abstracted out of the route. The other comment I have on your route is that
you seem to be heavy in writing processors for things that can be done in
the DSL. Think about the problem you are solving first with each processor.
If it is not unique to your domain or company, it is probably already
solved and you must go look for the solution not write one.

Try
from("file:/tmp/test?include=.*.csv")
  .setProperty("outputFile",
simple(String.format("${header.%s}.tmp.${exchangeId}", Exchange.FILE_NAME)))
  .split().tokenize("\n", 100)
  .process(new RequestProcessor())
  .to("direct:encrypt_file");

from("direct:encrypt_file")
  .pgp(keyFileName, keyUserid)
  .to("file:/tmp/test/output?fileName=" + outputFile + ".pgp");

Now I am coding without an IDE so please excuse any typos. It seems to me
you are using a file as sort of a temporary storage when instead, I would
just encrypt it right in the flow.


*Robert Simmons Jr. MSc. - Lead Java Architect @ EA*
*Author of: Hardcore Java (2003) and Maintainable Java (2012)*
*LinkedIn: **http://www.linkedin.com/pub/robert-simmons/40/852/a39
<http://www.linkedin.com/pub/robert-simmons/40/852/a39>*


On Fri, Feb 28, 2014 at 12:45 AM, abkrt <th...@gmail.com> wrote:

>
>
> Apache Camel Route:
>
>     from("file:/tmp/test?include=.*.csv").process(new Processor() {
>         public void process(Exchange exchange) throws Exception {
>             // set output file name
>             exchange.setProperty("outputFile",
> exchange.getIn().getHeader(Exchange.FILE_NAME, String.class) + ".tmp." +
> exchange.getExchangeId());
>         }
>     }).onCompletion().split().tokenize("\n", 100).process(new
> RequestProcessor()).to("direct:response").end().process(new Processor() {
>         public void process(Exchange exchange) throws Exception {
>             final String outputFile = exchange.getProperty("outputFile",
> String.class);
>
>             // add new rout to encrypt
>             CamelContext context = new DefaultCamelContext();
>             context.addRoutes(new RouteBuilder() {
>                 public void configure() {
>
>             from("file:/tmp/test/output?fileName=" +
> outputFile).marshal().pgp(keyFileName,
> keyUserid).to("file:/tmp/test/output?fileName=" + outputFile + ".pgp");
>                 }
>             });
>
>             context.start();
>             Thread.sleep(5000);
>             context.stop();
>         }
>     });
>
>
>
> from("direct:response").to("file:/tmp/test/output?fileName=${header.outputFile}&fileExist=Append");
>
> Above route is processing big file splitting into chunk (for batch
> processing) and generate output file with results. once generated the
> output
> file I need to encrypt. So I added NEW route inside a processor on
> onCompletion file split/process route. It works but I feel it is not a good
> design (since involve TWO context and need context shutdown explicitly).
>
> Can you anyone suggest me the proper way to fire the encryption route.
>
>
>
>
> --
> View this message in context:
> http://camel.465427.n5.nabble.com/Is-apache-camel-supports-nested-route-tp5748061.html
> Sent from the Camel - Users mailing list archive at Nabble.com.
>

Re: Is apache-camel supports nested route

Posted by abkrt <th...@gmail.com>.
Thank you very much for your valuable comments.



--
View this message in context: http://camel.465427.n5.nabble.com/Is-apache-camel-supports-nested-route-tp5748061p5748264.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Is apache-camel supports nested route

Posted by "kraythe ." <kr...@gmail.com>.
You need to look into an aggregator to combine different exchanges into
one. One of the most valuable references you will find in the camel world
is the EIP reference.
http://camel.apache.org/enterprise-integration-patterns.html That page
gives you a mapping as to how many, if not most, problems are solved in
EIP. You should also try to let go of the old synchronous way of doing
things and think in an asynchronous manner. That will get you further. For
example, your route is watching a directory and splitting up a file. There
are a lot of ways to pass that file reference around and add it into an
exchange. You can enrich it, read it on a from line, pass around the raw
data. Try to think from your goal what are the steps in a general fashion
and then figure out which really have to be sequential and then move on
from there. Even a file that is split up, encrypted and put back together
doesn't have to be sequential so long as you keep track of the order of
assembly.


*Robert Simmons Jr. MSc. - Lead Java Architect @ EA*
*Author of: Hardcore Java (2003) and Maintainable Java (2012)*
*LinkedIn: **http://www.linkedin.com/pub/robert-simmons/40/852/a39
<http://www.linkedin.com/pub/robert-simmons/40/852/a39>*


On Mon, Mar 3, 2014 at 2:01 AM, abkrt <th...@gmail.com> wrote:

> Thank you very Robert Simmons for you valuable reply.
>
> Still I have one issue, with above solution it will create multiple files
> based on split size. How can I avoid this and encrypt into single file. For
> that I had used "onCompletion" mechanism.
>
> I enhanced the initial design with your feedback. But the encryption route
> is getting the original file as the input, are there any mechanism to
> redirect the generated output file as the input for encryption.
>
> from("file:/home/rt40207/camel_test/gcb/request?include=.*.csv&noop=true")
>             .setProperty("outputFile",
> simple("${header.CamelFileName}.${exchangeId}"))
>             .onCompletion()
>                 .split().tokenize("\n", 5)
>                 .log("splitted body processed & written to file
> ${property.outputFile}.csv")
>                 .process(new RequestProcessor())
>
>
> .to("file:/home/rt40207/camel_test/gcb/temp?fileName=${property.outputFile}.csv&fileExist=Append")
>             .end()
>             .marshal().pgp(keyFileName, keyUserid)
>             .log("PGP encrypted written to file
> ${property.outputFile}.pgp")
>
>
> .to("file:/home/rt40207/camel_test/gcb/response?fileName=${property.outputFile}.pgp");
>
>
> Note:
> RequestProcessor:
> public class RequestProcessor implements Processor {
> public void process(Exchange exchange) throws Exception {
>     String body = exchange.getIn().getBody(String.class);
>     String[] split = body.split("\n");
>     StringBuilder output = new StringBuilder();
>
>     // TODO begin trx
>
>     for (String input : split) {
>         if (input.startsWith("InputHeader")) {
>             output.append("OutputHeader").append(input.substring(11) +
> ",");
>         } else {
>             // TODO process here
>             output.append("\n").append(input).append(",DONE");
>         }
>     }
>
>     // TODO commit trx
>
>     DefaultMessage message = new DefaultMessage();
>     message.setBody(output.toString());
>     message.setHeader(Exchange.FILE_NAME, "output" +
> exchange.getIn().getHeader(Exchange.FILE_NAME, String.class).substring(5));
>
>     exchange.setOut(message);
> }
>
>
>
> --
> View this message in context:
> http://camel.465427.n5.nabble.com/Is-apache-camel-supports-nested-route-tp5748061p5748229.html
> Sent from the Camel - Users mailing list archive at Nabble.com.
>

Re: Is apache-camel supports nested route

Posted by abkrt <th...@gmail.com>.
Thank you very Robert Simmons for you valuable reply.

Still I have one issue, with above solution it will create multiple files
based on split size. How can I avoid this and encrypt into single file. For
that I had used "onCompletion" mechanism.

I enhanced the initial design with your feedback. But the encryption route
is getting the original file as the input, are there any mechanism to
redirect the generated output file as the input for encryption. 
       
from("file:/home/rt40207/camel_test/gcb/request?include=.*.csv&noop=true")
            .setProperty("outputFile",
simple("${header.CamelFileName}.${exchangeId}"))
            .onCompletion()
                .split().tokenize("\n", 5)
                .log("splitted body processed & written to file
${property.outputFile}.csv")
                .process(new RequestProcessor())
               
.to("file:/home/rt40207/camel_test/gcb/temp?fileName=${property.outputFile}.csv&fileExist=Append")
            .end()
            .marshal().pgp(keyFileName, keyUserid)
            .log("PGP encrypted written to file ${property.outputFile}.pgp")
           
.to("file:/home/rt40207/camel_test/gcb/response?fileName=${property.outputFile}.pgp");


Note:
RequestProcessor:
public class RequestProcessor implements Processor {
public void process(Exchange exchange) throws Exception {
    String body = exchange.getIn().getBody(String.class); 
    String[] split = body.split("\n");
    StringBuilder output = new StringBuilder();

    // TODO begin trx

    for (String input : split) {
        if (input.startsWith("InputHeader")) {
            output.append("OutputHeader").append(input.substring(11) + ",");
        } else {
            // TODO process here
            output.append("\n").append(input).append(",DONE");
        }
    }

    // TODO commit trx

    DefaultMessage message = new DefaultMessage();
    message.setBody(output.toString());
    message.setHeader(Exchange.FILE_NAME, "output" +
exchange.getIn().getHeader(Exchange.FILE_NAME, String.class).substring(5));

    exchange.setOut(message);
}



--
View this message in context: http://camel.465427.n5.nabble.com/Is-apache-camel-supports-nested-route-tp5748061p5748229.html
Sent from the Camel - Users mailing list archive at Nabble.com.