You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by Charles Berger <ch...@googlemail.com> on 2017/12/19 22:50:03 UTC

Question about using Splitter

Hi,

I'm building my first application using Camel and have a question
about how to use a splitter.

My route receives a serialized Java class which contains a private
ArrayList of a POJO and getter and setter methods for the list.

I convert the serialized class into a its Java representation and then
apply it to a splitter.  What I want to get out of the Splitter is
each individual instance of the POJOs in the List but what I get
instead is the complete list.

Here is some code to show what I mean

public class ImageCollection {

private List<SingleImageModel> images = new ArrayList<SingleImageModel>();

public void setImages(List) { ... }

public List getImages() { }

}

public class SingleImageModel {

members ...

getters & setters

}

Routes:

The first route accepts a POST from a servlet, validates the incoming
payload and converts it into an instance of ImageCollection, which is
dispatched to the requestQueue, then returns a response to the client.

        from("servlet:uploadUrl")
        .log("Request: ${body}")
        .to("bean:uploadRequestQueue")
        .log("${body}")
        .marshal().json(JsonLibrary.Jackson)
        .wireTap("activemq:requestQueue")
        .to("bean:formatResponse")
        .end()


The second route is supposed to take the ImageCollection and split it
into the SingleImageModel instances


        from("activemq:requestQueue")
        .convertBodyTo(ImageCollection.class)
        .log("${body}")
        .split(bodyAs(ImageCollection.class))
        .log("${body}")
        .to("bean:downloadImageQueue")
        .marshal().json(JsonLibrary.Jackson)
        .to("activemq:ftpQueue");

I think I need to use an Aggregator to return the individual instances
of the SingleImageModel class from the list inside ImageCollection,
but I can't work out what that should do.

Instead, what is happening is that the bean registered for the
downloadImageQueue is receiving an instance of ImageCollection when it
expects an instance of SingleImageModel and the type conversion in
that bean is failing.

Any guidance much appreciated.

Thanks,

Charles.

Re: Question about using Splitter

Posted by Charles Berger <ch...@googlemail.com>.
> You should be able to convert your ImageCollection into List<SingleImageModel>
> by simply doing something like:
>
>         from("activemq:requestQueue")
>         .convertBodyTo(ImageCollection.class)
>         .setBody(simple("${body.images}"))
>         .log("${body}")
>         .split(body())
>         .log("${body}")
>         .to("bean:downloadImageQueue")
>         .marshal().json(JsonLibrary.Jackson)
>         .to("activemq:ftpQueue");

That has worked - thank you so much - I had spent way too long trying
to get past that step.

Re: Question about using Splitter

Posted by Tadayoshi Sato <sa...@gmail.com>.
Hi,

The point is that to split object message body using Splitter DSL it needs
to be either Collection, Iterator, or array. ImageCollection is a custom
class so there's no way for Camel to know how to split it.

You should be able to convert your ImageCollection into List<SingleImageModel>
by simply doing something like:

        from("activemq:requestQueue")
        .convertBodyTo(ImageCollection.class)
        .setBody(simple("${body.images}"))
        .log("${body}")
        .split(body())
        .log("${body}")
        .to("bean:downloadImageQueue")
        .marshal().json(JsonLibrary.Jackson)
        .to("activemq:ftpQueue");



On Wed, Dec 20, 2017 at 5:07 PM, Charles Berger <
charlesb.yesmail@googlemail.com> wrote:

> Hi,
>
> Thanks for replying.
>
> > Instead of splitting ImageCollection as the body, I think you just need
> to
> > split List<SingleImageModel> as the body.
>
> Are you suggesting that the ImageCollection class is actually
> superfluous and I should use the List<SingleImageModel> as the output
> from the bean uploadRequestQueue instead?
>
> Or is there a way in the route that I can call the method
> ImageCollection.getImages() method to pass into the split?
>
> Thanks,
>
> Charles.
>

Re: Question about using Splitter

Posted by Charles Berger <ch...@googlemail.com>.
Hi,

Thanks for replying.

> Instead of splitting ImageCollection as the body, I think you just need to
> split List<SingleImageModel> as the body.

Are you suggesting that the ImageCollection class is actually
superfluous and I should use the List<SingleImageModel> as the output
from the bean uploadRequestQueue instead?

Or is there a way in the route that I can call the method
ImageCollection.getImages() method to pass into the split?

Thanks,

Charles.

Re: Question about using Splitter

Posted by Tadayoshi Sato <sa...@gmail.com>.
Hi,

Instead of splitting ImageCollection as the body, I think you just need to
split List<SingleImageModel> as the body.

On Wed, Dec 20, 2017 at 7:50 AM, Charles Berger <
charlesb.yesmail@googlemail.com> wrote:

> Hi,
>
> I'm building my first application using Camel and have a question
> about how to use a splitter.
>
> My route receives a serialized Java class which contains a private
> ArrayList of a POJO and getter and setter methods for the list.
>
> I convert the serialized class into a its Java representation and then
> apply it to a splitter.  What I want to get out of the Splitter is
> each individual instance of the POJOs in the List but what I get
> instead is the complete list.
>
> Here is some code to show what I mean
>
> public class ImageCollection {
>
> private List<SingleImageModel> images = new ArrayList<SingleImageModel>();
>
> public void setImages(List) { ... }
>
> public List getImages() { }
>
> }
>
> public class SingleImageModel {
>
> members ...
>
> getters & setters
>
> }
>
> Routes:
>
> The first route accepts a POST from a servlet, validates the incoming
> payload and converts it into an instance of ImageCollection, which is
> dispatched to the requestQueue, then returns a response to the client.
>
>         from("servlet:uploadUrl")
>         .log("Request: ${body}")
>         .to("bean:uploadRequestQueue")
>         .log("${body}")
>         .marshal().json(JsonLibrary.Jackson)
>         .wireTap("activemq:requestQueue")
>         .to("bean:formatResponse")
>         .end()
>
>
> The second route is supposed to take the ImageCollection and split it
> into the SingleImageModel instances
>
>
>         from("activemq:requestQueue")
>         .convertBodyTo(ImageCollection.class)
>         .log("${body}")
>         .split(bodyAs(ImageCollection.class))
>         .log("${body}")
>         .to("bean:downloadImageQueue")
>         .marshal().json(JsonLibrary.Jackson)
>         .to("activemq:ftpQueue");
>
> I think I need to use an Aggregator to return the individual instances
> of the SingleImageModel class from the list inside ImageCollection,
> but I can't work out what that should do.
>
> Instead, what is happening is that the bean registered for the
> downloadImageQueue is receiving an instance of ImageCollection when it
> expects an instance of SingleImageModel and the type conversion in
> that bean is failing.
>
> Any guidance much appreciated.
>
> Thanks,
>
> Charles.
>