You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by Olaf <om...@gmail.com> on 2014/03/20 18:24:52 UTC

bean members with parallel processing

Hello,

would the following route work correctly with multiple threads? In
beanRef("TestProcessor", "step2") the instance variable mapper is used, so I
suppose it is not thread safe, right? Should I set the mapper as a property
of the exchange?

        from(inputsource)
            .beanRef("TestProcessor", "step1")
            .beanRef("TestProcessor", "step2")
            .beanRef("TestProcessor", "step3")
            .log("${body}")


package testcamel;

import java.util.Map;
import org.apache.camel.Headers;

public class TestProcessor {

    private String generator;
    private JsonMapper mapper;

    public String step1(String filedata, @Headers Map headers) {
        //create mapper for each new message
        mapper = new JsonMapper();
        //do something here
        return "resultFromStep1";

    }

    public String step2(String contentFromStep1) {
        //do something here with mapper
        mapper.doSomething();
        return "resultFromStep2";
    }

    public String step3(String contentFromStep2) {
        //do something here with mapper
        mapper.doSomething();
        return "resultFromStep2";
    }

}



--
View this message in context: http://camel.465427.n5.nabble.com/bean-members-with-parallel-processing-tp5749157.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: bean members with parallel processing

Posted by Claus Ibsen <cl...@gmail.com>.
Not if the mapper is not thread safe, and when you have concurrent
messages processed in that route.

You can store the mapper on exchange as a property and grab it in
step2 and 3 if you want to reuse the same mapper per exchange.

Or create a new mapper per step and do not reuse it

On Thu, Mar 20, 2014 at 6:24 PM, Olaf <om...@gmail.com> wrote:
> Hello,
>
> would the following route work correctly with multiple threads? In
> beanRef("TestProcessor", "step2") the instance variable mapper is used, so I
> suppose it is not thread safe, right? Should I set the mapper as a property
> of the exchange?
>
>         from(inputsource)
>             .beanRef("TestProcessor", "step1")
>             .beanRef("TestProcessor", "step2")
>             .beanRef("TestProcessor", "step3")
>             .log("${body}")
>
>
> package testcamel;
>
> import java.util.Map;
> import org.apache.camel.Headers;
>
> public class TestProcessor {
>
>     private String generator;
>     private JsonMapper mapper;
>
>     public String step1(String filedata, @Headers Map headers) {
>         //create mapper for each new message
>         mapper = new JsonMapper();
>         //do something here
>         return "resultFromStep1";
>
>     }
>
>     public String step2(String contentFromStep1) {
>         //do something here with mapper
>         mapper.doSomething();
>         return "resultFromStep2";
>     }
>
>     public String step3(String contentFromStep2) {
>         //do something here with mapper
>         mapper.doSomething();
>         return "resultFromStep2";
>     }
>
> }
>
>
>
> --
> View this message in context: http://camel.465427.n5.nabble.com/bean-members-with-parallel-processing-tp5749157.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