1

The problem

I have a spring mvc application that uses apache camel. I am confused on the role that the RouteBuilder class plays and how it actually gets initialized. I know that the docs say that the configure() method is:

Called on initialization to build the routes using the fluent builder syntax.

but when does this initialization occur? Does it occur at application startup or some time later when the route is about to be used?

The purpose of this question is ultimately to ask how I can modify the route at runtime. I want to be able to build different routes as needed.

Examples

xml definitions:

<service name="myService" tier="3">
    <requestType>my.package.RequestType</requestType>
    <responseType>my.package.ResponseType</responseType>
    <endpoint>
        <httpEndpoint>
            <url default="true" value="someUrl"/>
            <timeout value="5000"/>
        </httpEndpoint>
    </endpoint>
</service>

Route Builder template:

public class myRouteBuilder extends RouteBuilder {

    @Override
    public void configure() throws Exception {
        // When does this method get executed?
    }
}

Questions

  • When does configure() execute?
  • How can I dynamically set the endpoint url?
Mark
  • 9,718
  • 6
  • 29
  • 47

3 Answers3

2

You are able to use toD to dynamically change the endpoint at runtime based on an expression. See the documentation

If you want to change more of the route or add a completely new route then look at the API on the CamelContext. This Stackoverflow question has an example of adding a completely new route.

matt helliwell
  • 2,574
  • 16
  • 24
0

The lifecycle of the Camel service is documented here : https://camel.apache.org/lifecycle.html

Camel uses a simple lifecycle interface called Service which has a single start() and stop() method. Various classes implement Service such as CamelContext along with a number of Component and Endpoint classes. When you use Camel you typically have to start the CamelContext which will start all the various components and endpoints and activate the routing rules until the context is stopped again.

It is when the context starts that the various components start. Not sure i understand the dynamic url part. If it is to indicate a dynamic endpoint (if the data is this , then queue1 else queue2) you should be able to use something like the DynamicRouter EIP which is as explained here (https://camel.apache.org/dynamic-router.html)

Ramachandran.A.G
  • 4,788
  • 1
  • 12
  • 24
-1

You have several options.

  1. Inject them as spring properties.
  2. Inject them from external properties source.
  3. Inject them from some bean method.

Then you can put the property value in a header and later put the value in the .toD("$header.routeEndpoint"). This can take care of dynamic endpoints.

Off course to rebuild the entire route you need to play with the API.

Souciance Eqdam Rashti
  • 3,143
  • 3
  • 15
  • 31