You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by ChantingWolf <sy...@narod.ru> on 2010/04/24 19:05:01 UTC

Generating multible files based on DB query in a nice way.

Dear all,

I have a following question.
I have to generate many files based on sql query.

Let's say for example, I have get from database a list of orders made today
and genarate file for each order and later store each file on ftp.

Ideally I would like to get follewing.
Not quite sure how to get it.

from(MyBean).to(Ftp)

The problem and main question is how to generate multiple messages by custom
bean (for example).

I am not sure if splitter EIP is ok in this case 
because in my case I have not just one message to split, but I just have to
generate and send many messages.
http://camel.apache.org/splitter.html

I hope, someone meet this problem before.

If the task will be to generate just one file - everything is quite simple -
you need just fill Exchange.OutMessage (or something like this). But what
about multiple files - I really can't get, how to manage this situation.

P.S. Sorry if this question is stupid. 
I am novice in Camel (working with it just for coupe weeks).
It's a great tool.
Actually, that's why I want to use in in the best way.

Thanks a lot.

-- 
View this message in context: http://old.nabble.com/Generating-multible-files-based-on-DB-query-in-a-nice-way.-tp28351534p28351534.html
Sent from the Camel - Users mailing list archive at Nabble.com.


Re: Generating multiple files based on DB query in a nice way.

Posted by ChantingWolf <sy...@narod.ru>.
Hi,

Just in case if someone will need a short and full example (code snippet).
http://main-framer.livejournal.com/122017.html

Thanks.

-- 
View this message in context: http://old.nabble.com/Generating-multiple-files-based-on-DB-query-in-a-nice-way.-tp28351534p28358845.html
Sent from the Camel - Users mailing list archive at Nabble.com.


Re: Generating multiple files based on DB query in a nice way.

Posted by Claus Ibsen <cl...@gmail.com>.
Hi

You most likely need to use the Polling Consumer EIP pattern
http://camel.apache.org/polling-consumer.html

To have some timer trigger by X interval. For example every 10th
second invoke your bean.
http://camel.apache.org/timer.html

Then you can use your BEAN to retrieve the list of data which you need
to be written as files.

You also have to decide if the file name is important or just use an
auto generated UUID as name.
If you need to dictate the filename, then you need to provide this
information in a Header with the key: Exchange.FILE_NAME

If so this makes the Splitter a bit more challeging as you need to provide BOTH
- content of the file
- name of the file

And you can only have 1 return type in Java. So in this situation you can either
- use a org.apache.camel.Message as return type, eg List<Message>
where you store the file content in message IN body. And the file name
in message IN header with that key.
- Or not use the Splitter EIP but write the files directly from your
BEAN. For that you can use the ProducerTemplate in Camel which makes
this easy

Just invoke this code line from your BEAN
template.sendBodyAndHeader("file://somepath", contentOfFile,
Exchange.FILE_NAME, "myFileName.txt");

And you can just define ProducerTemplate template in the parameter
list of your bean

public void writeFilesAndDoStuff(ProducerTemplate template) throws Exception {
}


Then the route is simply just
from("timer://foo?period=5000").bean(MyCoolBean.class, "writeFilesAndDoStuff");





On Sat, Apr 24, 2010 at 10:56 PM, ChantingWolf <sy...@narod.ru> wrote:
>
> Dear all,
>
> Well, let's say, we have a very simple bean:
>
> <<<<
> import java.util.*;
> public class MySplitterBean {
>    public List<String> splitBody() {
>        List<String> answer = new ArrayList<String>();
>
>                answer.add("11");
>                answer.add("12");
>        return answer;
>    }
> }
>>>>>
>
> I want to use it as source for "from" clause.
> And after this write output of splitBody function to separate files.
>
> Can someone help with a working example?
>
> Thanks
>
> --
> View this message in context: http://old.nabble.com/Generating-multiple-files-based-on-DB-query-in-a-nice-way.-tp28351534p28352808.html
> Sent from the Camel - Users mailing list archive at Nabble.com.
>
>



-- 
Claus Ibsen
Apache Camel Committer

Author of Camel in Action: http://www.manning.com/ibsen/
Open Source Integration: http://fusesource.com
Blog: http://davsclaus.blogspot.com/
Twitter: http://twitter.com/davsclaus

Re: Generating multiple files based on DB query in a nice way.

Posted by ChantingWolf <sy...@narod.ru>.
Dear all,

Well, let's say, we have a very simple bean:

<<<<
import java.util.*;
public class MySplitterBean {
    public List<String> splitBody() {
        List<String> answer = new ArrayList<String>();

		answer.add("11");
		answer.add("12");
        return answer;
    }    
}
>>>>

I want to use it as source for "from" clause.
And after this write output of splitBody function to separate files.

Can someone help with a working example?

Thanks

-- 
View this message in context: http://old.nabble.com/Generating-multiple-files-based-on-DB-query-in-a-nice-way.-tp28351534p28352808.html
Sent from the Camel - Users mailing list archive at Nabble.com.


Re: Generating multible files based on DB query in a nice way.

Posted by Norman Maurer <no...@apache.org>.
I think you got it already .

Just use the splitter eip for that.

Bye
Norman

2010/4/24, ChantingWolf <sy...@narod.ru>:
>
> Dear all,
>
> I have a following question.
> I have to generate many files based on sql query.
>
> Let's say for example, I have get from database a list of orders made today
> and genarate file for each order and later store each file on ftp.
>
> Ideally I would like to get follewing.
> Not quite sure how to get it.
>
> from(MyBean).to(Ftp)
>
> The problem and main question is how to generate multiple messages by custom
> bean (for example).
>
> I am not sure if splitter EIP is ok in this case
> because in my case I have not just one message to split, but I just have to
> generate and send many messages.
> http://camel.apache.org/splitter.html
>
> I hope, someone meet this problem before.
>
> If the task will be to generate just one file - everything is quite simple -
> you need just fill Exchange.OutMessage (or something like this). But what
> about multiple files - I really can't get, how to manage this situation.
>
> P.S. Sorry if this question is stupid.
> I am novice in Camel (working with it just for coupe weeks).
> It's a great tool.
> Actually, that's why I want to use in in the best way.
>
> Thanks a lot.
>
> --
> View this message in context:
> http://old.nabble.com/Generating-multible-files-based-on-DB-query-in-a-nice-way.-tp28351534p28351534.html
> Sent from the Camel - Users mailing list archive at Nabble.com.
>
>