camel-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From 41n212 <dan.babing...@cloud40.co.uk>
Subject Creating new objects inside constant()
Date Sun, 04 Jun 2017 09:08:04 GMT
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.

Mime
View raw message