There are two ways of doing this: Using Spring Expression Language or usign Simple component.
Camel allows you integrate multiples languages in your routes, for example, Groovy Javascript, or Spring. More precisely, you can use SpEL (Spring Expression Language) so you can work with beans as in java class. In this way you can create a new instance of a bean (in this case it's not a bean, just a simple POJO) and put it in the body.
The other way is declaring a bean as usual and ref it from inside a simple expression.
This is my class:
import java.io.Serializable; public class User implements Serializable{ private static final long serialVersionUID = -8687467303034052162L; private String nombre; private String nif; public User() { } public User(final String nombre, final String nif) { this.nombre = nombre; this.nif = nif; } public String getNombre() { return nombre; } public void setNombre(final String nombre) { this.nombre = nombre; } public String getNif() { return nif; } public void setNif(final String nif) { this.nif = nif; } @Override public String toString() { return "User [nombre=" + nombre + ", nif=" + nif + "]"; } }
Then I create an instance of this class in Spring XML config file:
<bean id="alice" class="es.pruebas.modelo.User"> <constructor-arg name="nombre" value="Alice"/> <constructor-arg name="nif" value="00000001R"/> </bean>
And finally, this is my route in wich the body is set by using the two ways presented:
<camelContext id="camelContext" xmlns="http://camel.apache.org/schema/spring"> <route id="myRoute"> <from uri="timer://foo?fixedRate=true&period=5000"/> <setBody> <simple>ref:alice</simple> </setBody> <log message="The body should be Alice: ${body}" loggingLevel="INFO"/> <setBody> <spel>#{new es.pruebas.modelo.User('Bob', '12345678Z')}</spel> </setBody> <log message="The body should be Bob: ${body}" loggingLevel="INFO"/> </route> </camelContex>
The outcome of this execution will be:
2015-06-26 16:39:04 INFO myRoute:96 - New execution starts 2015-06-26 16:39:04 INFO myRoute:96 - The body should be Alice: User [nombre=Alice, nif=00000001R] 2015-06-26 16:39:04 INFO myRoute:96 - The body should be Bob: User [nombre=Bob, nif=12345678Z]
No hay comentarios:
Publicar un comentario