You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by Bruno Dusausoy <bd...@yp5.be> on 2013/05/11 09:34:48 UTC

Route design

Hi,

I've set up a simple sandbox project[1] which polls a directory for 
files, sends them via ftp to a specific input directory. Then I wait for 
the server to transform the files. In the end I fetch them from another 
specific directory on the ftp server.

Basically my route is defined as is :

from("file:src/data?noop=true")
   .bean(ftpSender)
   .delay(10000)
   .bean(ftpReceiver)
   .log("End route");

ftpSender and ftpReceiver are Spring beans :


@Component
public class FtpSender {

     private final FtpProperties ftpProperties;

     @Autowired
     ProducerTemplate producerTemplate;

     @Autowired
     public FtpSender(FtpProperties ftpProperties) {
         this.ftpProperties = ftpProperties;
     }

     @Handler
     public String send(File file) throws InterruptedException {
         String fileName = file.getName();
         String uri = createInputFtpUri(fileName);
         producerTemplate.sendBody(uri, file);
         return fileName;
     }

     private String createInputFtpUri(String filename) {
         StringBuilder builder = new StringBuilder("ftp://")
                 .append(ftpProperties.getUsername()).append('@')
                 .append(ftpProperties.getHostname()).append(':')
                 .append(ftpProperties.getPort()).append('/')
                 .append(ftpProperties.getInputDirectory()).append('?')
 
.append("password=").append(ftpProperties.getPassword()).append('&')
                 .append("fileName=").append(filename).append('&')
 
.append("delete=").append(ftpProperties.isDeleteAfterConsumption());

         return builder.toString();
     }
}

My question is quite simple : is there a cleaner way to code this ?
I don't really like the route builder with the bean() method, I would 
like to use to() with URI's and some expression language or script.

Regards.

[1] https://github.com/bdusauso/sandbox-camel-dynamic-ftp
-- 
Bruno Dusausoy
Software Engineer
YP5 Software
--
Pensez environnement : limitez l'impression de ce mail.
Please don't print this e-mail unless you really need to.

Re: Route design

Posted by Christoph Emmersberger <ce...@googlemail.com>.
Sorry for my quick response regarding the documentation.

(1) Your example in Spring DSL works also when applying the Java DSL, It's just about the endpoint configuration, respectively the String parameter you are passing
(2) May I try to summarize what you are trying

	a) Send a file to FTP server 1 and store it there
	b) Some magic changes the file on that server and you need to get it back? You therefore simply wait for some time and than pull the document

If that's the case, there might be a better option of what you want to achieve.

Please take a look into e.g. something like AggregationStrategy. This might enable you to have two different routes and combine them, even with the same file name.

You can do it of course always also via the bean construct. But if you want to utilize more of the Camel code, you probably need to go down somewhere like that.

Hope this helps.

- Christoph


On May 11, 2013, at 4:44 PM, Bruno Dusausoy wrote:

> On 05/11/2013 04:14 PM, Christoph Emmersberger wrote:
>> Well here is the documentation: http://camel.apache.org/ftp.html
>> 
>> (1) Add the maven dependency to your project
>> (2) Configure your endpoints (there are some parameters you can set as parameters, please refer to the documentation)
>> 
> 
> Thanks for pointing me to the docs but I already know them.
> I also own a copy of Camel in Action.
> 
> Maybe I don't explain myself clearly, I apologise for that.
> 
> What I was doing in the first place was actually working fine : I just asked if it could be less ugly and if I could get rid of the consumer and producer templates easily.
> 
> Now I've configured my endpoints, or at least one of them, with Spring DSL like this :
> 
>    <camel:camelContext xmlns="http://camel.apache.org/schema/spring">
>        <endpoint id="ftpIn"
> uri="ftp://${user}@${hostname}:${port}/${inputDir}?password=${password}&amp;fileName=$simple{header.CamelFileNameOnly}"/>
>        <route>
>            <from uri="file:src/data?noop=true"/>
>            <to uri="ref:ftpIn"/>
>            <delay>
>                <constant>10000</constant>
>            </delay>
>        </route>
>    </camel:camelContext>
> 
> It's working fine.
> 
> But now I'm facing another challenge I hadn't when using Camel's consumer template : I need to fetch the transformed file from FTP again, but in another directory. Clearly I need to pass the filename in the URI. But then the component needs to be in the same route as in this INCORRECT example :
> 
>        <endpoint id="ftpSender"
> uri="ftp://${user}@${hostname}:${port}/${inputDir}?password=${password}&amp;fileName=$simple{header.CamelFileNameOnly}"/>
> 
>        <endpoint id="ftpReceiver"
> uri="ftp://${user}@${hostname}:${port}/${inputDir}?password=${password}&amp;deleteAfter=false&amp;fileName=$simple{header.CamelFileNameOnly}"/>
> 
>        <route>
>            <from uri="file:src/data?noop=true"/>
>            <to uri="ref:ftpSender"/>
>            <delay>
>                <constant>10000</constant>
>            </delay>
>            <from uri="ref:ftpReceiver"/>
>            <log message="End route"/>
>        </route>
> 
> 
> In my first attempt, with ConsumerTemplate, it was easy, I simply used the beans() element of the route and did my job by calling ConsumerTemplate.
> 
> But how do I do it without beans() ?
> I could use another route but then how would I pass the file name to this new route ?
> 
> I must admit, as you can see, that I'm completely confused.
> 
> I hope it is clearer.
> 
> Regards.
> -- 
> Bruno Dusausoy
> Software Engineer
> YP5 Software
> --
> Pensez environnement : limitez l'impression de ce mail.
> Please don't print this e-mail unless you really need to.


Re: Route design

Posted by Bruno Dusausoy <bd...@yp5.be>.
On 05/11/2013 04:14 PM, Christoph Emmersberger wrote:
> Well here is the documentation: http://camel.apache.org/ftp.html
>
> (1) Add the maven dependency to your project
> (2) Configure your endpoints (there are some parameters you can set as parameters, please refer to the documentation)
>

Thanks for pointing me to the docs but I already know them.
I also own a copy of Camel in Action.

Maybe I don't explain myself clearly, I apologise for that.

What I was doing in the first place was actually working fine : I just 
asked if it could be less ugly and if I could get rid of the consumer 
and producer templates easily.

Now I've configured my endpoints, or at least one of them, with Spring 
DSL like this :

     <camel:camelContext xmlns="http://camel.apache.org/schema/spring">
         <endpoint id="ftpIn"
 
uri="ftp://${user}@${hostname}:${port}/${inputDir}?password=${password}&amp;fileName=$simple{header.CamelFileNameOnly}"/>
         <route>
             <from uri="file:src/data?noop=true"/>
             <to uri="ref:ftpIn"/>
             <delay>
                 <constant>10000</constant>
             </delay>
         </route>
     </camel:camelContext>

It's working fine.

But now I'm facing another challenge I hadn't when using Camel's 
consumer template : I need to fetch the transformed file from FTP again, 
but in another directory. Clearly I need to pass the filename in the 
URI. But then the component needs to be in the same route as in this 
INCORRECT example :

         <endpoint id="ftpSender"
 
uri="ftp://${user}@${hostname}:${port}/${inputDir}?password=${password}&amp;fileName=$simple{header.CamelFileNameOnly}"/>

         <endpoint id="ftpReceiver"
 
uri="ftp://${user}@${hostname}:${port}/${inputDir}?password=${password}&amp;deleteAfter=false&amp;fileName=$simple{header.CamelFileNameOnly}"/>

         <route>
             <from uri="file:src/data?noop=true"/>
             <to uri="ref:ftpSender"/>
             <delay>
                 <constant>10000</constant>
             </delay>
             <from uri="ref:ftpReceiver"/>
             <log message="End route"/>
         </route>


In my first attempt, with ConsumerTemplate, it was easy, I simply used 
the beans() element of the route and did my job by calling ConsumerTemplate.

But how do I do it without beans() ?
I could use another route but then how would I pass the file name to 
this new route ?

I must admit, as you can see, that I'm completely confused.

I hope it is clearer.

Regards.
-- 
Bruno Dusausoy
Software Engineer
YP5 Software
--
Pensez environnement : limitez l'impression de ce mail.
Please don't print this e-mail unless you really need to.

Re: Route design

Posted by Christoph Emmersberger <ce...@googlemail.com>.
Well here is the documentation: http://camel.apache.org/ftp.html

(1) Add the maven dependency to your project
(2) Configure your endpoints (there are some parameters you can set as parameters, please refer to the documentation)

That's about it.

Hope this helps,

- Christoph

On May 11, 2013, at 10:03 AM, Bruno Dusausoy wrote:

> On 05/11/2013 09:46 AM, Christian Müller wrote:
>> Why do you not use the Camel FTP/SFTP/FTPS component?
>> 
> I'd like to, but I don't know how to do it properly hence my question.
> I'm quite confused here because I have two consumers (file and ftp) and one producer (ftp).
> 
> -- 
> Bruno Dusausoy
> Software Engineer
> YP5 Software
> --
> Pensez environnement : limitez l'impression de ce mail.
> Please don't print this e-mail unless you really need to.


Re: Route design

Posted by Bruno Dusausoy <bd...@yp5.be>.
On 05/11/2013 09:46 AM, Christian Müller wrote:
> Why do you not use the Camel FTP/SFTP/FTPS component?
>
I'd like to, but I don't know how to do it properly hence my question.
I'm quite confused here because I have two consumers (file and ftp) and 
one producer (ftp).

-- 
Bruno Dusausoy
Software Engineer
YP5 Software
--
Pensez environnement : limitez l'impression de ce mail.
Please don't print this e-mail unless you really need to.

Re: Route design

Posted by Christian Müller <ch...@gmail.com>.
Why do you not use the Camel FTP/SFTP/FTPS component?

Best,
Christian

Sent from a mobile device
Am 11.05.2013 09:35 schrieb "Bruno Dusausoy" <bd...@yp5.be>:

> Hi,
>
> I've set up a simple sandbox project[1] which polls a directory for files,
> sends them via ftp to a specific input directory. Then I wait for the
> server to transform the files. In the end I fetch them from another
> specific directory on the ftp server.
>
> Basically my route is defined as is :
>
> from("file:src/data?noop=true"**)
>   .bean(ftpSender)
>   .delay(10000)
>   .bean(ftpReceiver)
>   .log("End route");
>
> ftpSender and ftpReceiver are Spring beans :
>
>
> @Component
> public class FtpSender {
>
>     private final FtpProperties ftpProperties;
>
>     @Autowired
>     ProducerTemplate producerTemplate;
>
>     @Autowired
>     public FtpSender(FtpProperties ftpProperties) {
>         this.ftpProperties = ftpProperties;
>     }
>
>     @Handler
>     public String send(File file) throws InterruptedException {
>         String fileName = file.getName();
>         String uri = createInputFtpUri(fileName);
>         producerTemplate.sendBody(uri, file);
>         return fileName;
>     }
>
>     private String createInputFtpUri(String filename) {
>         StringBuilder builder = new StringBuilder("ftp://")
>                 .append(ftpProperties.**getUsername()).append('@')
>                 .append(ftpProperties.**getHostname()).append(':')
>                 .append(ftpProperties.getPort(**)).append('/')
>                 .append(ftpProperties.**getInputDirectory()).append('?**')
>
> .append("password=").append(**ftpProperties.getPassword()).**append('&')
>                 .append("fileName=").append(**filename).append('&')
>
> .append("delete=").append(**ftpProperties.**isDeleteAfterConsumption());
>
>         return builder.toString();
>     }
> }
>
> My question is quite simple : is there a cleaner way to code this ?
> I don't really like the route builder with the bean() method, I would like
> to use to() with URI's and some expression language or script.
>
> Regards.
>
> [1] https://github.com/bdusauso/**sandbox-camel-dynamic-ftp<https://github.com/bdusauso/sandbox-camel-dynamic-ftp>
> --
> Bruno Dusausoy
> Software Engineer
> YP5 Software
> --
> Pensez environnement : limitez l'impression de ce mail.
> Please don't print this e-mail unless you really need to.
>