Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call setter method on ${body} in a Camel route?

Tags:

apache-camel

I have tried to set a property on the body of a Java bean constituting the message in transit through a Camel route. I have tried various approaches e.g.

    <route>
        ...
        ..
        <transform>
            <simple>${body.label} = ${property.label}</simple>
        </transform>
        ...
        ..
    </route>

in this particular case the ${body} is a Java bean with a setLabel(String label) method and the ${property.label} is set by other means in another route. In this example the result is not the desired (and I understand why), i.e. after the transform the body of the message is replaced with the ${body.label} = ${property.label} string.

My current work-around is to manually code a transformer as a Spring bean and set the label property of the Java bean in code but I like to find out if there is a simpler/smarter way to achieve this, preferably in XML DSL which is what I use?

Regards, Ola

like image 302
Ola Theander Avatar asked Dec 05 '13 15:12

Ola Theander


People also ask

What is SetProperty in camel?

The SetProperty EIP is used for setting a Exchange property. An Exchange property is a key/value set as a Map on the org. apache. camel. Exchange instance.

How does a camel route work?

Contents. A Camel route is where the integration flow is defined. For example to integrate two systems then a Camel route can be coded to specify how these systems are integrated. An example could be to take files from a FTP server and send to a ActiveMQ messaging system.

What is Route ID in Apache Camel?

routeId() are for identifying routes. By adding ids you can in your tests use adviceWith() to mock or inject or remove parts of your route to perform automated tests without having access to backend systems.

What is camel HTTP?

Camel HTTP Component provides support for calling external HTTP resources. You can typically use it to invoke REST Services.

How to integrate two systems with a camel route?

For example to integrate two systems then a Camel route can be coded to specify how these systems are integrated. An example could be to take files from a FTP server and send to a ActiveMQ messaging system. When coding routes with the Java DSL then you would use a RouteBuilder classes where you code the route in the configure method as shown:

What is the best way to configure a camel endpoint?

The Endpoint DSL can also be used for route configurations. This requires adding camel-endpointdsl to the classpath, and then using org.apache.camel.builder.endpoint.EndpointRouteConfigurationBuilder, which offers the type safe DSL for Camel endpoints. Route configurations are either given an explicit unique ID, or the configuration is nameless.

How to configure camelcontext in Apache Camel?

The CamelContext is the runtime system of Apache Camel and connects its different concepts such as routes, components or endpoints. Below are the steps that we need to consider while configuring the CamelContext: Create CamelContext. Add endpoints or components. Add Routes to connect the endpoints.

How do I Find my Camel routes in Spring Boot?

If you use Spring Boot, then your Camel routes and route configurations can be auto-discovered by the spring boot component scanning. This requires adding the @Component annotation to the class. See the example camel-spring-boot-examples/routes-configuration. The Endpoint DSL can also be used for route configurations.


Video Answer


2 Answers

I'm not sure if it's possible with simple, but you could do it using groovy:

<setBody>
    <groovy>request.body.label = exchange.getProperty('label')
            return request.body
    </groovy>
</setBody>
like image 168
Alexander Durnev Avatar answered Sep 22 '22 07:09

Alexander Durnev


Maybe it can help someone in the future:

As I know You can use standard Java approach with settters anf getters in body:

 .split(body())
      .setBody(simple("${body.setLogin('TEST')}"))
 .end()

It works inside <split></split>. Maybe inside another blocks.

like image 38
Maxim Tiunchik Avatar answered Sep 23 '22 07:09

Maxim Tiunchik