You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by a746076drdrb <a7...@drdrb.net> on 2014/04/01 22:09:57 UTC

How to enrich with recipient list?

Hello,

I try to use dynamic endpoint uri with recipientList and then combine old
body with the new one, to enrich the message However in
AggregationStrategy.aggregate method the
oldExchange is null.

I suppose recipientList clears the old body. Is there a way to combine two
exchanges after recipientList? The test throwing
IllegalArgumentException("oldExchange  is null"):



package com.mycompany.cameltest;

import org.apache.camel.Exchange;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.processor.aggregate.AggregationStrategy;
import org.apache.camel.test.junit4.CamelTestSupport;
import org.junit.Test;

public class MyRouteBuilderTest extends CamelTestSupport {

    @Override
    protected RouteBuilder createRouteBuilder() throws Exception {
        return new RouteBuilder() {
            @Override
            public void configure() throws Exception {
                from("direct:start")
                        .recipientList(simple("${body.url}"))
                        .aggregationStrategy(new AggregationStrategy() {

                            @Override
                            public Exchange aggregate(Exchange oldExchange,
Exchange newExchange) {
                                if (oldExchange == null) {
                                    throw new
IllegalArgumentException("oldExchange  is null");
                                }
                                String newBody =
newExchange.getIn().getBody(String.class);
                                PojoObject pojo =
oldExchange.getIn().getBody(PojoObject.class);
                                pojo.setResult(newBody);
                                newExchange.getIn().setBody(pojo);
                                return newExchange;
                            }
                        });

            }
        };
    }

    @Test
    public void testConfigure() {
        context.setTracing(Boolean.TRUE);
        PojoObject pojo = new PojoObject();
        pojo.setUrl("mock:http");
        template.sendBody("direct:start", pojo);
    }

    public class PojoObject {

        private String url = "http://foo";
        private Object result;

        public String getUrl() {
            return url;
        }

        public void setUrl(String url) {
            this.url = url;
        }

        public Object getResult() {
            return result;
        }

        public void setResult(Object result) {
            this.result = result;
        }
        

        @Override
        public String toString() {
            return "PojoBody{" + "url=" + url + '}';
        }

    }

}




--
View this message in context: http://camel.465427.n5.nabble.com/How-to-enrich-with-recipient-list-tp5749644.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: How to enrich with recipient list?

Posted by a746076drdrb <a7...@drdrb.net>.
"It looks like your recipientList just has one url"

well, it should have one url. I'm using recipientList because it supports
dynamic uri



--
View this message in context: http://camel.465427.n5.nabble.com/How-to-enrich-with-recipient-list-tp5749644p5749653.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: How to enrich with recipient list?

Posted by Willem Jiang <wi...@gmail.com>.
It looks like your recipientList just has one url, and it makes sense that the old exchange is null. Please add more endpoint url into the recipientList, you should be able to see the old exchange which is not null.

--  
Willem Jiang

Red Hat, Inc.
Web: http://www.redhat.com
Blog: http://willemjiang.blogspot.com (English)
http://jnn.iteye.com (Chinese)
Twitter: willemjiang  
Weibo: 姜宁willem



On April 2, 2014 at 8:25:38 AM, a746076drdrb (a746076@drdrb.net) wrote:
> Hello,
>  
> I try to use dynamic endpoint uri with recipientList and then combine old
> body with the new one, to enrich the message However in
> AggregationStrategy.aggregate method the
> oldExchange is null.
>  
> I suppose recipientList clears the old body. Is there a way to combine two
> exchanges after recipientList? The test throwing
> IllegalArgumentException("oldExchange is null"):
>  
>  
>  
> package com.mycompany.cameltest;
>  
> import org.apache.camel.Exchange;
> import org.apache.camel.builder.RouteBuilder;
> import org.apache.camel.processor.aggregate.AggregationStrategy;
> import org.apache.camel.test.junit4.CamelTestSupport;
> import org.junit.Test;
>  
> public class MyRouteBuilderTest extends CamelTestSupport {
>  
> @Override
> protected RouteBuilder createRouteBuilder() throws Exception {
> return new RouteBuilder() {
> @Override
> public void configure() throws Exception {
> from("direct:start")
> .recipientList(simple("${body.url}"))
> .aggregationStrategy(new AggregationStrategy() {
>  
> @Override
> public Exchange aggregate(Exchange oldExchange,
> Exchange newExchange) {
> if (oldExchange == null) {
> throw new
> IllegalArgumentException("oldExchange is null");
> }
> String newBody =
> newExchange.getIn().getBody(String.class);
> PojoObject pojo =
> oldExchange.getIn().getBody(PojoObject.class);
> pojo.setResult(newBody);
> newExchange.getIn().setBody(pojo);
> return newExchange;
> }
> });
>  
> }
> };
> }
>  
> @Test
> public void testConfigure() {
> context.setTracing(Boolean.TRUE);
> PojoObject pojo = new PojoObject();
> pojo.setUrl("mock:http");
> template.sendBody("direct:start", pojo);
> }
>  
> public class PojoObject {
>  
> private String url = "http://foo";
> private Object result;
>  
> public String getUrl() {
> return url;
> }
>  
> public void setUrl(String url) {
> this.url = url;
> }
>  
> public Object getResult() {
> return result;
> }
>  
> public void setResult(Object result) {
> this.result = result;
> }
>  
>  
> @Override
> public String toString() {
> return "PojoBody{" + "url=" + url + '}';
> }
>  
> }
>  
> }
>  
>  
>  
>  
> --
> View this message in context: http://camel.465427.n5.nabble.com/How-to-enrich-with-recipient-list-tp5749644.html  
> Sent from the Camel - Users mailing list archive at Nabble.com.
>  


Re: How to enrich with recipient list?

Posted by Claus Ibsen <cl...@gmail.com>.
On Wed, Apr 2, 2014 at 9:50 AM, a746076drdrb <a7...@drdrb.net> wrote:
> I know about content enricher EIP, but I need a dynamic url.
>
> So, why is oldExchange null?
>

That is as designed. oldExchange is null because you only have 1
endpoint. If you have 2+ then oldExchange is the previous so you can
merge all the responses together.

Your use case if content enricher if you want to merge the current
body with something else.


>
>
> --
> View this message in context: http://camel.465427.n5.nabble.com/How-to-enrich-with-recipient-list-tp5749644p5749654.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
Make your Camel applications look hawt, try: http://hawt.io

Re: How to enrich with recipient list?

Posted by a746076drdrb <a7...@drdrb.net>.
I know about content enricher EIP, but I need a dynamic url. 

So, why is oldExchange null?



--
View this message in context: http://camel.465427.n5.nabble.com/How-to-enrich-with-recipient-list-tp5749644p5749654.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: How to enrich with recipient list?

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

You should then use the content enricher EIP if  you want to merge the
current body with the enriched body.

http://camel.apache.org/content-enricher.html

Though the content enricher does not support dynamic uri, as the
recipient list does.

On Tue, Apr 1, 2014 at 10:09 PM, a746076drdrb <a7...@drdrb.net> wrote:
> Hello,
>
> I try to use dynamic endpoint uri with recipientList and then combine old
> body with the new one, to enrich the message However in
> AggregationStrategy.aggregate method the
> oldExchange is null.
>
> I suppose recipientList clears the old body. Is there a way to combine two
> exchanges after recipientList? The test throwing
> IllegalArgumentException("oldExchange  is null"):
>
>
>
> package com.mycompany.cameltest;
>
> import org.apache.camel.Exchange;
> import org.apache.camel.builder.RouteBuilder;
> import org.apache.camel.processor.aggregate.AggregationStrategy;
> import org.apache.camel.test.junit4.CamelTestSupport;
> import org.junit.Test;
>
> public class MyRouteBuilderTest extends CamelTestSupport {
>
>     @Override
>     protected RouteBuilder createRouteBuilder() throws Exception {
>         return new RouteBuilder() {
>             @Override
>             public void configure() throws Exception {
>                 from("direct:start")
>                         .recipientList(simple("${body.url}"))
>                         .aggregationStrategy(new AggregationStrategy() {
>
>                             @Override
>                             public Exchange aggregate(Exchange oldExchange,
> Exchange newExchange) {
>                                 if (oldExchange == null) {
>                                     throw new
> IllegalArgumentException("oldExchange  is null");
>                                 }
>                                 String newBody =
> newExchange.getIn().getBody(String.class);
>                                 PojoObject pojo =
> oldExchange.getIn().getBody(PojoObject.class);
>                                 pojo.setResult(newBody);
>                                 newExchange.getIn().setBody(pojo);
>                                 return newExchange;
>                             }
>                         });
>
>             }
>         };
>     }
>
>     @Test
>     public void testConfigure() {
>         context.setTracing(Boolean.TRUE);
>         PojoObject pojo = new PojoObject();
>         pojo.setUrl("mock:http");
>         template.sendBody("direct:start", pojo);
>     }
>
>     public class PojoObject {
>
>         private String url = "http://foo";
>         private Object result;
>
>         public String getUrl() {
>             return url;
>         }
>
>         public void setUrl(String url) {
>             this.url = url;
>         }
>
>         public Object getResult() {
>             return result;
>         }
>
>         public void setResult(Object result) {
>             this.result = result;
>         }
>
>
>         @Override
>         public String toString() {
>             return "PojoBody{" + "url=" + url + '}';
>         }
>
>     }
>
> }
>
>
>
>
> --
> View this message in context: http://camel.465427.n5.nabble.com/How-to-enrich-with-recipient-list-tp5749644.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
Make your Camel applications look hawt, try: http://hawt.io