You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by 41n212 <da...@cloud40.co.uk> on 2017/06/04 09:08:04 UTC

Creating new objects inside constant()

Hi,

After reviewing Claus's message in the following post:
http://camel.465427.n5.nabble.com/camel-message-headers-td4406101.html

I tried to set a new object into a header using the constant(new Object())
syntax, but this doesn't work as I had expected - am I doing something
wrong, or have I misunderstood. 

Basically, when the route is executed a second time, my new Object already
isn't instanciated as a new class, rather is the same class instance as was
from the previous execution. I'm assuming that is to do with the use of the
"constant" keyword. It's an easy workaround (just create the object in an
inline processor to instanciate a new instance), but took me a little while
to track down the nature of the issue so wanted to understand if this is
intentional or not.

An example to show what I mean:

from("timer:Constant Initialiser?delay=5000&repeatCount=3")
    .setBody(constant(new ArrayList()))
    .process(new Processor() {
        public void process(Exchange exchange) throws Exception {
            @SuppressWarnings("unchecked")
            ArrayList<String> constantArrayList =
(ArrayList<String>)exchange.getIn().getBody(ArrayList.class);
            constantArrayList.add("New value");
        }
    })
    .to("log:Constant Array List?showBody=true");
    
from("timer:Variable Initialiser?delay=10000&repeatCount=3")
    .process(new Processor() {
        public void process(Exchange exchange) throws Exception {
            exchange.getIn().setBody(new ArrayList());
        }
    })
    .process(new Processor() {
        public void process(Exchange exchange) throws Exception {
            @SuppressWarnings("unchecked")
            ArrayList<String> variableArrayList =
(ArrayList<String>)exchange.getIn().getBody(ArrayList.class);
            variableArrayList.add("New value");
        }
    })
    .to("log:Variable Array List?showBody=true");            


I would have expected the same result from both of these routes, but the
output is:

2017-06-04 09:53:49.769  INFO 14400 --- [ant Initialiser] Constant Array
List                      : Exchange[ExchangePattern: InOnly, BodyType:
java.util.ArrayList, Body: New value]
2017-06-04 09:53:50.767  INFO 14400 --- [ant Initialiser] Constant Array
List                      : Exchange[ExchangePattern: InOnly, BodyType:
java.util.ArrayList, Body: New value,New value]
2017-06-04 09:53:51.766  INFO 14400 --- [ant Initialiser] Constant Array
List                      : Exchange[ExchangePattern: InOnly, BodyType:
java.util.ArrayList, Body: New value,New value,New value]
2017-06-04 09:53:54.767  INFO 14400 --- [ble Initialiser] Variable Array
List                      : Exchange[ExchangePattern: InOnly, BodyType:
java.util.ArrayList, Body: New value]
2017-06-04 09:53:55.769  INFO 14400 --- [ble Initialiser] Variable Array
List                      : Exchange[ExchangePattern: InOnly, BodyType:
java.util.ArrayList, Body: New value]
2017-06-04 09:53:56.769  INFO 14400 --- [ble Initialiser] Variable Array
List                      : Exchange[ExchangePattern: InOnly, BodyType:
java.util.ArrayList, Body: New value]





--
View this message in context: http://camel.465427.n5.nabble.com/Creating-new-objects-inside-constant-tp5801725.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Creating new objects inside constant()

Posted by 41n212 <da...@cloud40.co.uk>.
Oops - my mistake. I have realised my error after posting this message.

Obviously in the first example the ArrayList is only being instanciated once
- in the same way that any object used directly in the route like that is
instanciated once whilst the route is starting. The constant is then passing
the same reference each time through the route.

In the second example the processor runs each time, hence the new object.

My mistake - whoops!! :-/

41n212



--
View this message in context: http://camel.465427.n5.nabble.com/Creating-new-objects-inside-constant-tp5801725p5801726.html
Sent from the Camel - Users mailing list archive at Nabble.com.