You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by daninovac <da...@gmail.com> on 2014/11/20 16:25:35 UTC

Camel noob question about how to make a precisely number of GET request on a url

Hello,
I've been dealing with this problem for some hours and after searches and
searches I decided to post it here. I'm new to Apache Camel, so don't judge
me, but I need a response or an idea to my issue.

Ok, so I need to get a json from a GET request on a specified url. Luckily,
that GET method returns a JSON so no many headaches. 
I've made this like:
<camel:route>
<camel:from uri="GET_reqURL" />
<camel:to uri="file:src/main/resources/TEST" />
</camel:route>

I put all the json files in a TEST folder, as you can see. The problem is
the GET request are made continuously and my TEST folder keep updating. The
files from the folder had the same content but the last two numbers, or
three from the file names, keep changing, wich is ok, I think.

Now the problem is: I need to get some id's from a txt file (let's say) and
for each id I need to call a GET method. If I have 10 id's in a txt file, I
need to see just 10 files in my TEST folder and I really don't know how to
put this in my camel code.
Oh, I use Spring DSL, btw.

Thanks in advance and sorry If it is a stupid question.



--
View this message in context: http://camel.465427.n5.nabble.com/Camel-noob-question-about-how-to-make-a-precisely-number-of-GET-request-on-a-url-tp5759365.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Camel noob question about how to make a precisely number of GET request on a url

Posted by daninovac <da...@gmail.com>.
Great! Thank you very much.
Now, if I want to get a counter for every element i have in this list, can I
use the header property? I saw smthg like this, but I'm not sure how to use
it.



--
View this message in context: http://camel.465427.n5.nabble.com/Camel-noob-question-about-how-to-make-a-precisely-number-of-GET-request-on-a-url-tp5759365p5759450.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Camel noob question about how to make a precisely number of GET request on a url

Posted by Claus Ibsen <cl...@gmail.com>.
You can also just configure fileExist=Append so you append to the
file, then you do not need the aggregator. However the aggregator
should be used if you need to do more complicated merging. But if you
just need to append, then the file component can do that out of the
box.

On Fri, Nov 21, 2014 at 2:04 PM, daninovac <da...@gmail.com> wrote:
> No, I want to write from a file (in.txt) to another file(out.txt)
> out.txt will contain the values after the split, but I get in out.txt just
> the last value from in.txt.
> I've read about it and I see that Camel write just the last value after the
> splitter and that I need to implement an aggregator.
>
> I think I need to save the id's from in.txt in a List then pass that list to
> out.txt just to write the values from my array.
>
> I'm proceeding like this.
>
> public class MyAggregationStrategy implements AggregationStrategy {
>
>         public MyAggregationStrategy() {
>                 super();
>         }
>
>         public Exchange aggregate(Exchange oldExchange, Exchange newExchange) {
>                 Message newIn = newExchange.getIn();
>                 Object newBody = newIn.getBody();
>                 ArrayList list = null;
>                 if (oldExchange == null) {
>                         list = new ArrayList();
>                         list.add(newBody);
>                         newIn.setBody(list);
>                         return newExchange;
>                 } else {
>                         Message in = oldExchange.getIn();
>                         list = in.getBody(ArrayList.class);
>                         list.add(newBody);
>                         return oldExchange;
>                 }
>
>         }
>
> }
>
>
> *AND*
>
> <camel:route>
>                         <camel:from uri="file:src/main/resources/idList/" />
>                         <camel:split strategyRef="aggregatorStrategy">
>                                 <camel:method ref="splitterBean" method="splitBody">
>                                 </camel:method>
>                                 <camel:to uri="file:src/main/resources/idListOut/" />
>                         </camel:split>
>
>                 </camel:route>
>
> where aggregatorStrategy is a bean to the class i've shown you.
> But the problem is that in out.txt all I see is only the last value, nothing
> changes. Where am I wrong? Please, I need a quick reply.
>
>
>
> --
> View this message in context: http://camel.465427.n5.nabble.com/Camel-noob-question-about-how-to-make-a-precisely-number-of-GET-request-on-a-url-tp5759365p5759447.html
> Sent from the Camel - Users mailing list archive at Nabble.com.



-- 
Claus Ibsen
-----------------
Red Hat, Inc.
Email: cibsen@redhat.com
Twitter: davsclaus
Blog: http://davsclaus.com
Author of Camel in Action: http://www.manning.com/ibsen
hawtio: http://hawt.io/
fabric8: http://fabric8.io/

Re: Camel noob question about how to make a precisely number of GET request on a url

Posted by daninovac <da...@gmail.com>.
No, I want to write from a file (in.txt) to another file(out.txt) 
out.txt will contain the values after the split, but I get in out.txt just
the last value from in.txt.
I've read about it and I see that Camel write just the last value after the
splitter and that I need to implement an aggregator.

I think I need to save the id's from in.txt in a List then pass that list to
out.txt just to write the values from my array.

I'm proceeding like this.

public class MyAggregationStrategy implements AggregationStrategy {

	public MyAggregationStrategy() {
		super();
	}

	public Exchange aggregate(Exchange oldExchange, Exchange newExchange) {
		Message newIn = newExchange.getIn();
		Object newBody = newIn.getBody();
		ArrayList list = null;
		if (oldExchange == null) {
			list = new ArrayList();
			list.add(newBody);
			newIn.setBody(list);
			return newExchange;
		} else {
			Message in = oldExchange.getIn();
			list = in.getBody(ArrayList.class);
			list.add(newBody);
			return oldExchange;
		}

	}

}


*AND*

<camel:route>
			<camel:from uri="file:src/main/resources/idList/" />
			<camel:split strategyRef="aggregatorStrategy">
				<camel:method ref="splitterBean" method="splitBody">
				</camel:method>
				<camel:to uri="file:src/main/resources/idListOut/" />
			</camel:split>
			
		</camel:route>

where aggregatorStrategy is a bean to the class i've shown you.
But the problem is that in out.txt all I see is only the last value, nothing
changes. Where am I wrong? Please, I need a quick reply.



--
View this message in context: http://camel.465427.n5.nabble.com/Camel-noob-question-about-how-to-make-a-precisely-number-of-GET-request-on-a-url-tp5759365p5759447.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Camel noob question about how to make a precisely number of GET request on a url

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

If you write to the same file name, then Camel will override by default

See the fileExist option at
http://camel.apache.org/file2

On Fri, Nov 21, 2014 at 1:16 PM, daninovac <da...@gmail.com> wrote:
> Hi, thank you very much for your reply. Now everything is a lot more clearer.
> With the response I need to unmarshal it to a xml. I think i did this on a
> specific example (marshal & unmarshal). The problem was that, with the ID's,
> to get them from a txt and for each one to get a json from the get req. If
> you want, you can post the entire schema wich I need to follow, would help
> me  a
> Thank you very much!
>
> L.E:
> let's say I have
> in.txt -> 1, 3, 5, 10.
> And I did
> <camel:route>
>                         <camel:from uri="file:.../in" />
>                                 <camel:split streaming="true">
>                                         <camel:tokenize token="," />
>                                         <camel:to uri="file:../out" />
>                                 </camel:split>
>                 </camel:route>
>
> Why in the out file, the only value I get is the last one from in, more
> exactly 10 ??!
>
>
>
> --
> View this message in context: http://camel.465427.n5.nabble.com/Camel-noob-question-about-how-to-make-a-precisely-number-of-GET-request-on-a-url-tp5759365p5759443.html
> Sent from the Camel - Users mailing list archive at Nabble.com.



-- 
Claus Ibsen
-----------------
Red Hat, Inc.
Email: cibsen@redhat.com
Twitter: davsclaus
Blog: http://davsclaus.com
Author of Camel in Action: http://www.manning.com/ibsen
hawtio: http://hawt.io/
fabric8: http://fabric8.io/

Re: Camel noob question about how to make a precisely number of GET request on a url

Posted by daninovac <da...@gmail.com>.
Hi, thank you very much for your reply. Now everything is a lot more clearer. 
With the response I need to unmarshal it to a xml. I think i did this on a
specific example (marshal & unmarshal). The problem was that, with the ID's,
to get them from a txt and for each one to get a json from the get req. If
you want, you can post the entire schema wich I need to follow, would help
me  a 
Thank you very much! 

L.E: 
let's say I have 
in.txt -> 1, 3, 5, 10. 
And I did 
<camel:route>
                        <camel:from uri="file:.../in" />
                                <camel:split streaming="true">
                                        <camel:tokenize token="," />
                                        <camel:to uri="file:../out" />
                                </camel:split>
                </camel:route>  

Why in the out file, the only value I get is the last one from in, more
exactly 10 ??!



--
View this message in context: http://camel.465427.n5.nabble.com/Camel-noob-question-about-how-to-make-a-precisely-number-of-GET-request-on-a-url-tp5759365p5759443.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Camel noob question about how to make a precisely number of GET request on a url

Posted by daninovac <da...@gmail.com>.
Hi, thank you very much for your reply. Now everything is a lot more clearer.
With the response I need to unmarshal it to a xml. I think i did this on a
specific example (marshal & unmarshal). The problem was that, with the ID's,
to get them from a txt and for each one to get a json from the get req.
Thank you very much!



--
View this message in context: http://camel.465427.n5.nabble.com/Camel-noob-question-about-how-to-make-a-precisely-number-of-GET-request-on-a-url-tp5759365p5759373.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Camel noob question about how to make a precisely number of GET request on a url

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

If you need to get the ids from a file first, then start with that


from file
  split body tokenize \n (assume each id is on a new line or something)
       to http (*)
     ?? what to do with the response from the get ?
   end

what do you need to do with the response from each get? Do you need to
discard it, or write to a new file, or do you need to combine all 10
responses into a single response?

Also see the link for (*)

I suggest to study a few of the components and eips you need to use

http://camel.apache.org/file2
http://camel.apache.org/http
http://camel.apache.org/how-to-use-a-dynamic-uri-in-to.html (*)
http://camel.apache.org/splitter.html



On Thu, Nov 20, 2014 at 4:25 PM, daninovac <da...@gmail.com> wrote:
> Hello,
> I've been dealing with this problem for some hours and after searches and
> searches I decided to post it here. I'm new to Apache Camel, so don't judge
> me, but I need a response or an idea to my issue.
>
> Ok, so I need to get a json from a GET request on a specified url. Luckily,
> that GET method returns a JSON so no many headaches.
> I've made this like:
> <camel:route>
> <camel:from uri="GET_reqURL" />
> <camel:to uri="file:src/main/resources/TEST" />
> </camel:route>
>
> I put all the json files in a TEST folder, as you can see. The problem is
> the GET request are made continuously and my TEST folder keep updating. The
> files from the folder had the same content but the last two numbers, or
> three from the file names, keep changing, wich is ok, I think.
>
> Now the problem is: I need to get some id's from a txt file (let's say) and
> for each id I need to call a GET method. If I have 10 id's in a txt file, I
> need to see just 10 files in my TEST folder and I really don't know how to
> put this in my camel code.
> Oh, I use Spring DSL, btw.
>
> Thanks in advance and sorry If it is a stupid question.
>
>
>
> --
> View this message in context: http://camel.465427.n5.nabble.com/Camel-noob-question-about-how-to-make-a-precisely-number-of-GET-request-on-a-url-tp5759365.html
> Sent from the Camel - Users mailing list archive at Nabble.com.



-- 
Claus Ibsen
-----------------
Red Hat, Inc.
Email: cibsen@redhat.com
Twitter: davsclaus
Blog: http://davsclaus.com
Author of Camel in Action: http://www.manning.com/ibsen
hawtio: http://hawt.io/
fabric8: http://fabric8.io/

Re: Camel noob question about how to make a precisely number of GET request on a url

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

And btw the http keeps looping, you can use a timer to start the route
so you can call it every X second, or only 1 time etc.

But it seems you need to start with the id file to get the ids to use

On Thu, Nov 20, 2014 at 4:25 PM, daninovac <da...@gmail.com> wrote:
> Hello,
> I've been dealing with this problem for some hours and after searches and
> searches I decided to post it here. I'm new to Apache Camel, so don't judge
> me, but I need a response or an idea to my issue.
>
> Ok, so I need to get a json from a GET request on a specified url. Luckily,
> that GET method returns a JSON so no many headaches.
> I've made this like:
> <camel:route>
> <camel:from uri="GET_reqURL" />
> <camel:to uri="file:src/main/resources/TEST" />
> </camel:route>
>
> I put all the json files in a TEST folder, as you can see. The problem is
> the GET request are made continuously and my TEST folder keep updating. The
> files from the folder had the same content but the last two numbers, or
> three from the file names, keep changing, wich is ok, I think.
>
> Now the problem is: I need to get some id's from a txt file (let's say) and
> for each id I need to call a GET method. If I have 10 id's in a txt file, I
> need to see just 10 files in my TEST folder and I really don't know how to
> put this in my camel code.
> Oh, I use Spring DSL, btw.
>
> Thanks in advance and sorry If it is a stupid question.
>
>
>
> --
> View this message in context: http://camel.465427.n5.nabble.com/Camel-noob-question-about-how-to-make-a-precisely-number-of-GET-request-on-a-url-tp5759365.html
> Sent from the Camel - Users mailing list archive at Nabble.com.



-- 
Claus Ibsen
-----------------
Red Hat, Inc.
Email: cibsen@redhat.com
Twitter: davsclaus
Blog: http://davsclaus.com
Author of Camel in Action: http://www.manning.com/ibsen
hawtio: http://hawt.io/
fabric8: http://fabric8.io/