user2824073
user2824073

Reputation: 2485

Setting an Object in route body


I'm looking for a way to construct an Object and setting it in the Body of a route. Say my Bean is named sample.Person, I'd need to call one of its constructors, Person(name) and set it into the Body. Something like this:

<bean id="myBean" class="sample.Person"/>

  <route id="myroute">
    <from uri="timer:foo?repeatCount=1"/>
    <setBody>
      <simple>${myBean("name")}</simple>
    </setBody>
     . . . .
    <to uri="mock:result"/>
  </route>

Which unfortunately does not work..... any help? Thanks

Upvotes: 2

Views: 4505

Answers (1)

cslysy
cslysy

Reputation: 830

At first I would create PersonFactory to instantiate Person objects:

public class PersonFactory {

    public Person createPerson(String name){
        return new Person(name);
    }
}

Then use it inside camel route:

<bean id="personFactory" class="sample.PersonFactory" />

<camelContext xmlns="http://camel.apache.org/schema/spring">
.....
<route>
    <from uri="timer:foo?repeatCount=1"/>
    <setProperty propertyName="personName">
        <constant>John Doe</constant>
    </setProperty>
    <setBody>
      <spel>#{@personFactory.createPerson(properties['personName'])}</spel>
    </setBody>
     . . . .
    <to uri="mock:result"/>
</route>
...
<camelContext>

Upvotes: 4

Related Questions