You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by pminearo <pm...@reardencommerce.com> on 2011/04/11 22:14:41 UTC

Default Behavior for aggregate

I didn't see anything posted on this (though I may have missed it).  I am
using Camel 2.7.0.

When using aggregate(), is there a default behavior?  What I mean by this
is, let say you have the following Route in your RouteBUilder:


from("jms:queue:myQueue").aggregate().process(new
BatchSaveMessagesToDBProcessor());


Would the default behavior of Camel be to just create a List of whatever
objects are in Exchange.getIn().getBody()?

I noticed there are 4 different aggregate() methods:


aggregate()
aggregate(AggregationStrategy)

aggregate(Expression)
aggregate(Expression, AggregationStrategy)

Let's say all you want to do is batch up the messages coming in in order to
do a batch save to the DB; would you need to specify an AggregationStrategy
and/or Expression; or will Camel handle creating a List of what is in the
"In" message body?

What I have noticed is aggregate() and aggregate(AggregationStrategy) have a
different API than aggregate(Expression) and aggregate(Expression,
AggregationStrategy).  Why the difference?

Thanks!

--
View this message in context: http://camel.465427.n5.nabble.com/Default-Behavior-for-aggregate-tp4296790p4296790.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Default Behavior for aggregate

Posted by "ben.oday" <be...@initekconsulting.com>.
I just wanted to use an out-of-the-box strategy for a quick example.  I
generally create my own strategy and use the body instead.  I created the
JIRA issue because I think this should be a standard strategy (if not the
default mode as you suggested).  

Here is a simple strategy to build up a List in the aggregated exchange's IN
body (submitted for inclusion in the JIRA)...

Is this what you were looking for?

public class AggregateAsListStrategy implements AggregationStrategy {

    @SuppressWarnings("unchecked")
    public Exchange aggregate(Exchange oldExchange, Exchange newExchange) {
        List list;
        Exchange answer = oldExchange;

        if (oldExchange == null) {
            answer = new DefaultExchange(newExchange);
            list = new ArrayList();
            answer.getIn().setBody(list);
        } else {
            list = (List) oldExchange.getIn().getBody();
        }

        if (newExchange != null) {
            list.add(newExchange.getIn().getBody());
        }
        return answer;
    }

}


pminearo wrote:
> 
> Out of curiousity, why put this in as a Property instead of replacing the
> Message Body?
> 


-----
Ben O'Day
IT Consultant -http://benoday.blogspot.com

--
View this message in context: http://camel.465427.n5.nabble.com/Default-Behavior-for-aggregate-tp4296790p4299300.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Default Behavior for aggregate

Posted by pminearo <pm...@reardencommerce.com>.
Out of curiousity, why put this in as a Property instead of replacing the
Message Body?

--
View this message in context: http://camel.465427.n5.nabble.com/Default-Behavior-for-aggregate-tp4296790p4299267.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Default Behavior for aggregate

Posted by "ben.oday" <be...@initekconsulting.com>.
added  https://issues.apache.org/jira/browse/CAMEL-3855 CAMEL-3855  to
explore this further...



-----
Ben O'Day
IT Consultant -http://benoday.blogspot.com

--
View this message in context: http://camel.465427.n5.nabble.com/Default-Behavior-for-aggregate-tp4296790p4299254.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Default Behavior for aggregate

Posted by "ben.oday" <be...@initekconsulting.com>.
Currently, an expression and strategy are required (or you get a DSL
compilation error).  Your best bet is to simply use the "constant(true)"
expression to match all exchanges and the groupExchanges() strategy to
simply store them all in an exchange property...

from()
    .aggregate(constant(true))
        .completionTimeout(500)   //or completionInterval, completionSize,
etc...
        .groupExchanges()				
    .process(...)

Then in your processor, just pull the exchanges from the exchange like
this...

List grouped = exchange.getProperty(Exchange.GROUPED_EXCHANGE, List.class);

That being said, it would be nice if this just was the default behavior... 


pminearo wrote:
> 
> When using aggregate(), is there a default behavior? 
> ...
> Let's say all you want to do is batch up the messages coming in in order
> to do a batch save to the DB; would you need to specify an
> AggregationStrategy and/or Expression; or will Camel handle creating a
> List of what is in the "In" message body?
> 


-----
Ben O'Day
IT Consultant -http://benoday.blogspot.com

--
View this message in context: http://camel.465427.n5.nabble.com/Default-Behavior-for-aggregate-tp4296790p4297064.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Default Behavior for aggregate

Posted by Claus Ibsen <cl...@gmail.com>.
On Mon, Apr 11, 2011 at 10:14 PM, pminearo <pm...@reardencommerce.com> wrote:
> I didn't see anything posted on this (though I may have missed it).  I am
> using Camel 2.7.0.
>
> When using aggregate(), is there a default behavior?  What I mean by this
> is, let say you have the following Route in your RouteBUilder:
>
>
> from("jms:queue:myQueue").aggregate().process(new
> BatchSaveMessagesToDBProcessor());
>
>
> Would the default behavior of Camel be to just create a List of whatever
> objects are in Exchange.getIn().getBody()?
>
> I noticed there are 4 different aggregate() methods:
>
>
> aggregate()
> aggregate(AggregationStrategy)
>
> aggregate(Expression)
> aggregate(Expression, AggregationStrategy)
>
> Let's say all you want to do is batch up the messages coming in in order to
> do a batch save to the DB; would you need to specify an AggregationStrategy
> and/or Expression; or will Camel handle creating a List of what is in the
> "In" message body?
>
> What I have noticed is aggregate() and aggregate(AggregationStrategy) have a
> different API than aggregate(Expression) and aggregate(Expression,
> AggregationStrategy).  Why the difference?
>

There are methods on AggregationDefintion you use the continue
configure the EIP. Its part of the DSL.

See its documentation at
http://camel.apache.org/aggregator2

For example using .completionPredicate(predicate) etc.

Also the pattern is extensively covered in the Camel in Action book in
chapter 8.



> Thanks!
>
> --
> View this message in context: http://camel.465427.n5.nabble.com/Default-Behavior-for-aggregate-tp4296790p4296790.html
> Sent from the Camel - Users mailing list archive at Nabble.com.
>



-- 
Claus Ibsen
-----------------
FuseSource
Email: cibsen@fusesource.com
Web: http://fusesource.com
CamelOne 2011: http://fusesource.com/camelone2011/
Twitter: davsclaus
Blog: http://davsclaus.blogspot.com/
Author of Camel in Action: http://www.manning.com/ibsen/