You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by roger_rabbit <ro...@gmx.fr> on 2013/08/01 17:28:45 UTC

Aggregator won't work with filter?

Hi,

I have a strange problem that may be related to my poor knowledge of  EIP
<http://camel.apache.org/eip.html>  . But althouth I took time to read the
whole of it, I may have missed stg cause I couldn't find a solution.

Thus, here is it : 
I have 2 routes, one filtering incoming (XML as String) messages, and the
other one getting the queued messages from this filtering and aggregating
them, and enventually marshall/zip them...
My problem is that the completion that is number-based is never reached...
though far more than the expected amount of messages are read from the
queue. And if I remove my filter, everything works fine (but for my
former-filter indeed)... thus If you have any tip on what I am doing wrong,
I will listen it with great care from my beginner ears.

    	from("direct:files") // xml as String messages
    	  .choice()
    		.when().xpath("//root/total='0'")
    		    .log("no items found for " + simple("${in.headers[itemId]}") + " :
skipping this item")
    		    .stop()
    		.otherwise()
    		.inOnly("direct:items");

    	from("direct:items")
			.process(processItems)
			.aggregate(body(),new AggregationStrategy() {
							 public Exchange aggregate(Exchange oldExchange, Exchange
newExchange) {
							        if (oldExchange == null) {
							            return newExchange;
							        }
							 
							        String oldBody = oldExchange.getIn().getBody(String.class);
							        String newBody = newExchange.getIn().getBody(String.class);
							        oldExchange.getIn().setBody(oldBody + "+" + newBody);
							        return oldExchange;
						    }
						})
			.completionSize(10) // completed only when I work directly on
"direct:files"
			.convertBodyTo(byte[].class, "UTF-8")
			.marshal().gzip()
			.setHeader(
Exchange.FILE_NAME,constant("${date:now:yyyyMMddHHmmssSSS"}.gz"))
			.to(folderOutUri);



--
View this message in context: http://camel.465427.n5.nabble.com/Aggregator-won-t-work-with-filter-tp5736645.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Aggregator won't work with filter?

Posted by roger_rabbit <ro...@gmx.fr>.
Claus,

I just figured it out a few minutes ago and was about to answer :) 
... I confirm my correlation expression was wrong. A night of routing lead
me to the solution.

What confused me what that without a filter, it worked well : because all
bodies where the same (ie a default xml content with no data).
I did it as you suggest, and it works fine now:

.setHeader(CATEGORY_FOR_FILE,someSmartCategory)

...

.aggregate(header(EXCHANGE_CATEGORY_FOR_FILE),new AggregationStrategy() {
                                                         public Exchange
aggregate(Exchange oldExchange, Exchange newExchange) {
                                                                if
(oldExchange == null) {
                                                                    return
newExchange;
                                                                }
                                                         
                                                                String
oldBody = oldExchange.getIn().getBody(String.class);
                                                                String
newBody = newExchange.getIn().getBody(String.class);
                                                               
oldExchange.getIn().setBody(oldBody + "+" + newBody);
                                                                return
oldExchange;
                                                    }
                                                })
                        .completionSize(10) // completed only when I work
directly on "direct:files"
                        .convertBodyTo(byte[].class, "UTF-8") 




Thanks a lot for having a look and finding the solution, and also for the
suggestions. I once had access to that book, and It helped a lot at start.
So I agree I should consider adding it to my library!



--
View this message in context: http://camel.465427.n5.nabble.com/Aggregator-won-t-work-with-filter-tp5736645p5736686.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Aggregator won't work with filter?

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

The correlation expression in your aggregator is wrong. You use body()
which is likely not what you want.
If you just want to put all in the same group then use a constant
instead, eg constant(true) or something.

I suggest to read about the aggregate EIP again. And if you have a
copy of Camel in Action book, then chapter 8 covers this EIP
extensively and explains how it works also from the inside and its
principle etc.

The EIP book has also great covered of the EIPs as a background
material. This book is highly recommended.
For people wanting to know more about the EIPs in general




On Thu, Aug 1, 2013 at 5:28 PM, roger_rabbit <ro...@gmx.fr> wrote:
> Hi,
>
> I have a strange problem that may be related to my poor knowledge of  EIP
> <http://camel.apache.org/eip.html>  . But althouth I took time to read the
> whole of it, I may have missed stg cause I couldn't find a solution.
>
> Thus, here is it :
> I have 2 routes, one filtering incoming (XML as String) messages, and the
> other one getting the queued messages from this filtering and aggregating
> them, and enventually marshall/zip them...
> My problem is that the completion that is number-based is never reached...
> though far more than the expected amount of messages are read from the
> queue. And if I remove my filter, everything works fine (but for my
> former-filter indeed)... thus If you have any tip on what I am doing wrong,
> I will listen it with great care from my beginner ears.
>
>         from("direct:files") // xml as String messages
>           .choice()
>                 .when().xpath("//root/total='0'")
>                     .log("no items found for " + simple("${in.headers[itemId]}") + " :
> skipping this item")
>                     .stop()
>                 .otherwise()
>                 .inOnly("direct:items");
>
>         from("direct:items")
>                         .process(processItems)
>                         .aggregate(body(),new AggregationStrategy() {
>                                                          public Exchange aggregate(Exchange oldExchange, Exchange
> newExchange) {
>                                                                 if (oldExchange == null) {
>                                                                     return newExchange;
>                                                                 }
>
>                                                                 String oldBody = oldExchange.getIn().getBody(String.class);
>                                                                 String newBody = newExchange.getIn().getBody(String.class);
>                                                                 oldExchange.getIn().setBody(oldBody + "+" + newBody);
>                                                                 return oldExchange;
>                                                     }
>                                                 })
>                         .completionSize(10) // completed only when I work directly on
> "direct:files"
>                         .convertBodyTo(byte[].class, "UTF-8")
>                         .marshal().gzip()
>                         .setHeader(
> Exchange.FILE_NAME,constant("${date:now:yyyyMMddHHmmssSSS"}.gz"))
>                         .to(folderOutUri);
>
>
>
> --
> View this message in context: http://camel.465427.n5.nabble.com/Aggregator-won-t-work-with-filter-tp5736645.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