Camel Components

Dynamic Router Control

Since Camel 4.4

Only producer is supported

The Dynamic Router Control endpoint is a special type of endpoint in the Dynamic Router component where routing participants can subscribe or unsubscribe dynamically at runtime. By sending control messages to this endpoint, participants can specify their own routing rules and alter the dynamic rule base of the Dynamic Router component in real-time. Participants can choose between using URI query parameters, and sending a control message as the exchange message body.

URI format

dynamic-router-control:controlAction[?options]

Configuring Options

Camel components are configured on two separate levels:

  • component level

  • endpoint level

Configuring Component Options

At the component level, you set general and shared configurations that are, then, inherited by the endpoints. It is the highest configuration level.

For example, a component may have security settings, credentials for authentication, urls for network connection and so forth.

Some components only have a few options, and others may have many. Because components typically have pre-configured defaults that are commonly used, then you may often only need to configure a few options on a component; or none at all.

You can configure components using:

  • the Component DSL.

  • in a configuration file (application.properties, *.yaml files, etc).

  • directly in the Java code.

Configuring Endpoint Options

You usually spend more time setting up endpoints because they have many options. These options help you customize what you want the endpoint to do. The options are also categorized into whether the endpoint is used as a consumer (from), as a producer (to), or both.

Configuring endpoints is most often done directly in the endpoint URI as path and query parameters. You can also use the Endpoint DSL and DataFormat DSL as a type safe way of configuring endpoints and data formats in Java.

A good practice when configuring options is to use Property Placeholders.

Property placeholders provide a few benefits:

  • They help prevent using hardcoded urls, port numbers, sensitive information, and other settings.

  • They allow externalizing the configuration from the code.

  • They help the code to become more flexible and reusable.

The following two sections list all the options, firstly for the component followed by the endpoint.

Component Options

The Dynamic Router Control component supports the following options which are listed below.

Name Description Default Type

lazyStartProducer (producer)

Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a producer may otherwise fail during starting and cause the route to fail being started. By deferring this startup to be lazy then the startup failure can be handled during routing messages via Camel’s routing error handlers. Beware that when the first message is processed then creating and starting the producer may take a little time and prolong the total processing time of the processing.

false

boolean

autowiredEnabled (advanced)

Whether autowiring is enabled. This is used for automatic autowiring options (the option must be marked as autowired) by looking up in the registry to find if there is a single instance of matching type, which then gets configured on the component. This can be used for automatic configuring JDBC data sources, JMS connection factories, AWS Clients, etc.

true

boolean

Endpoint Options

The Dynamic Router Control endpoint is configured using URI syntax:

dynamic-router-control:controlAction

With the following path and query parameters:

Path Parameters

Name Description Default Type

controlAction (producer)

Required The control action to perform: subscribe adds a subscription, update replaces the subscription with the same subscriptionId, unsubscribe removes a subscription, list returns the subscriptions of a channel, and statistics returns their routing statistics. The CamelDynamicRouterControlAction header overrides it.

Enum values:

  • subscribe

  • unsubscribe

  • update

  • list

  • statistics

String

Query Parameters

Name Description Default Type

lazyStartProducer (producer (advanced))

Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a producer may otherwise fail during starting and cause the route to fail being started. By deferring this startup to be lazy then the startup failure can be handled during routing messages via Camel’s routing error handlers. Beware that when the first message is processed then creating and starting the producer may take a little time and prolong the total processing time of the processing.

false

boolean

destinationUri (control)

The endpoint URI that the Dynamic Router sends the exchanges that match the predicate of the subscription to. The CamelDynamicRouterDestinationUri header overrides it.

String

expressionLanguage (control)

The name of the language that compiles the predicate expression, such as simple or groovy.

simple

String

predicate (control)

The predicate expression of the subscription, in the language of expressionLanguage, which the Dynamic Router evaluates against each exchange on the channel. Without a predicate and without the CamelDynamicRouterPredicateBean header, the message body must be the Predicate.

String

predicateBean (control)

The name of a Predicate bean in the registry to use as the predicate of the subscription. It is only applied when the endpoint is called with toD, which passes it on as the CamelDynamicRouterPredicateBean header; with to, set that header instead.

Predicate

priority (control)

The priority of the subscription, required for subscribe and update. The Dynamic Router evaluates the subscriptions of a channel in order of priority, the lowest number first (and by subscriptionId for the same priority), so with recipientMode firstMatch the matching subscription with the lowest number gets the exchange. The CamelDynamicRouterPriority header overrides it.

Integer

subscribeChannel (control)

The channel of the subscription, which is the channel of the dynamic-router endpoint that routes to it. For list and statistics, the channel whose subscriptions to return. For unsubscribe, when not set, the subscription is looked up on all channels. The CamelDynamicRouterSubscribeChannel header overrides it.

String

subscriptionId (control)

The ID of the subscription to add, update or remove, unique within its channel. Subscribe and update return it as the message body. The CamelDynamicRouterSubscriptionId header overrides it.

String

allowPredicateFromMessage (security)

Whether the subscription predicate, and the language used to compile it, may be taken from the incoming control message. When disabled, the predicate and expressionLanguage configured on this endpoint are used instead. Enabling this lets the sender of a control message choose both the expression language and the expression that the Dynamic Router compiles into a live predicate, so only enable it when control messages come from a trusted source.

false

boolean

Message Headers

The Dynamic Router Control component supports the following message header(s), which is/are listed below:

Name Description Default Type

CamelDynamicRouterControlAction (producer)

Constant: CONTROL_ACTION_HEADER

The control action to perform: subscribe, unsubscribe, update, list or statistics. It overrides the controlAction of the endpoint URI.

String

CamelDynamicRouterSubscribeChannel (producer)

Constant: CONTROL_SUBSCRIBE_CHANNEL

The channel of the subscription, or for list and statistics the channel whose subscriptions to return. It overrides the subscribeChannel option.

String

CamelDynamicRouterSubscriptionId (producer)

Constant: CONTROL_SUBSCRIPTION_ID

The ID of the subscription to add, update or remove. It overrides the subscriptionId option.

String

CamelDynamicRouterDestinationUri (producer)

Constant: CONTROL_DESTINATION_URI

The endpoint URI that the Dynamic Router sends the exchanges that match the predicate of the subscription to. It overrides the destinationUri option.

String

CamelDynamicRouterPriority (producer)

Constant: CONTROL_PRIORITY

The priority of the subscription, where the lowest number is evaluated first. It overrides the priority option.

String

CamelDynamicRouterPredicate (producer)

Constant: CONTROL_PREDICATE

The predicate expression of the subscription. It is rejected unless the endpoint has allowPredicateFromMessage enabled.

String

CamelDynamicRouterPredicateBean (producer)

Constant: CONTROL_PREDICATE_BEAN

The name of a Predicate bean in the registry to use as the predicate of the subscription. It takes precedence over a predicate expression.

String

CamelDynamicRouterExpressionLanguage (producer)

Constant: CONTROL_EXPRESSION_LANGUAGE

The name of the language of the predicate expression. It is rejected unless the endpoint has allowPredicateFromMessage enabled.

String

Message Body

The message body at each side of the Dynamic Router Control component: Consumer is the body of the messages the consumer creates, Consumer reply the bodies a consumer that answers its caller can reply with, Producer input the bodies the producer can send or write, and Producer output the body after the producer.

Side Body Description

Producer input

any

A DynamicRouterControlMessage, or the subscription in the options or the headers. For subscribe and update without a predicate or a predicateBean, the body must be the Predicate.

Producer output

unknown

By the action: the id of the subscription for subscribe and update, true when unsubscribe removed it, and the subscriptions or their statistics as text for list and statistics. The CamelDynamicRouterControlAction header overrides the action.

  • With controlAction=subscribe: text (java.lang.String)

  • With controlAction=update: text (java.lang.String)

  • With controlAction=unsubscribe: value (java.lang.Boolean)

  • With controlAction=list: text (java.lang.String)

  • With controlAction=statistics: text (java.lang.String)

The bodies are the ones with the default options. A side that is not listed is not known, and tooling does not guess it. The carriers, the sides and the tools that use them are described in Message Body Types.

Supplying the subscription predicate

A subscription needs a predicate that decides which exchanges the participant receives. There are three ways to supply one, and they differ in who chooses it:

  • predicateBean — the name of a Predicate bound in the registry. The route author decides which predicates exist, and the control message only selects one of them by name.

  • predicate and expressionLanguage as control endpoint URI parameters — the route author writes the expression, and every subscription made through that endpoint uses it.

  • predicate and expressionLanguage carried in the control message, in its body or its headers — the sender of the control message chooses both the expression language and the expression.

The third form is disabled by default. The predicate is compiled and then evaluated against every exchange on the channel, so letting the control message choose both the language and the expression means the sender of that message decides what runs inside the Camel process. Set allowPredicateFromMessage=true on the control endpoint to enable it, and only do so when control messages can only come from a trusted source:

from("kafka:subscriptions")
    .unmarshal().json(DynamicRouterControlMessage.class)
    .to("dynamic-router-control:subscribe?allowPredicateFromMessage=true");

When allowPredicateFromMessage is false, which is the default, a control message that supplies a predicate or an expressionLanguage is rejected with an IllegalArgumentException. Prefer predicateBean, or configure the predicate on the endpoint, when control messages arrive from outside the application.

Subscription parameters that the control message does not carry fall back to the values configured on the control endpoint, so a participant can send only the parameters that identify it and let the endpoint supply the rest.

Subscribing

Subscribing can be achieved by using query parameters in the control endpoint URI, or by sending a DynamicRouterControlMessage to the control endpoint URI.

URI examples

Example URI RouteBuilder Subscription
  • Java

  • XML

  • YAML

// Send a subscribe request to the dynamic router that will match every exchange
// and route messages to the URI: "direct:myDestination"
from("direct:subscribe")
    .to("dynamic-router-control:subscribe?subscribeChannel=myChannel&subscriptionId=mySubId&destinationUri=direct:myDestination&priority=5&predicate=true&expressionLanguage=simple");
<route>
  <from uri="direct:subscribe"/>
  <to uri="dynamic-router-control:subscribe?subscribeChannel=myChannel&amp;subscriptionId=mySubId&amp;destinationUri=direct:myDestination&amp;priority=5&amp;predicate=true&amp;expressionLanguage=simple"/>
</route>
- route:
    from:
      uri: direct:subscribe
      steps:
        - to:
            uri: dynamic-router-control:subscribe
            parameters:
              subscribeChannel: myChannel
              subscriptionId: mySubId
              destinationUri: direct:myDestination
              priority: 5
              predicate: "true"
              expressionLanguage: simple
Example Java URI ProducerTemplate Subscription
CamelContext context = new DefaultCamelContext();
context.start();
ProducerTemplate template = context.createProducerTemplate();
RouteBuilder.addRoutes(context, rb -> {
    // Route for subscriber destination
    rb.from("direct:myDestination")
            .to("log:dynamicRouterExample?showAll=true&multiline=true");
    // Route for subscribing
    rb.from("direct://subscribe")
            .toD("dynamic-router-control://subscribe" +
                    "?subscribeChannel=${header.subscribeChannel}" +
                    "&subscriptionId=${header.subscriptionId}" +
                    "&destinationUri=${header.destinationUri}" +
                    "&priority=${header.priority}" +
                    "&predicateBean=${header.predicateBean}");
});
Predicate predicate = PredicateBuilder.constant(true);
context.getRegistry().bind("predicate", predicate);
template.sendBodyAndHeaders("direct:subscribe", "",
        Map.of("subscribeChannel", "test",
                "subscriptionId", "testSubscription1",
                "destinationUri", "direct:myDestination",
                "priority", "1",
                "predicateBean", "predicate"));

Above, because the control URI is dynamic, and since a ProducerTemplate does not have a built-in way to send to a dynamic URI, we have to send subscription parameters from a ProducerTemplate in a different way. The dynamic-aware endpoint uses headers "under the hood", because the URI params are converted to headers, so we can set the headers deliberately.

DynamicRouterControlMessage example

Example Java DynamicRouterControlMessage Subscription
DynamicRouterControlMessage controlMessage = DynamicRouterControlMessage.newBuilder()
    .subscribeChannel("myChannel")
    .subscriptionId("mySubId")
    .destinationUri("direct:myDestination")
    .priority(5)
    .predicate("true")
    .expressionLanguage("simple")
    .build();
producerTemplate.sendBody("dynamic-router-control:subscribe?allowPredicateFromMessage=true", controlMessage);

Unsubscribing

Like subscribing, unsubscribing can also be achieved by using query parameters in the control endpoint URI, or by sending a DynamicRouterControlMessage to the control endpoint URI. The difference is that unsubscribing can be achieved by using either one or two parameters.

URI examples

Example URI RouteBuilder Unsubscribe
  • Java

  • XML

  • YAML

from("direct:subscribe")
    .to("dynamic-router-control:unsubscribe?subscribeChannel=myChannel&subscriptionId=mySubId");
<route>
  <from uri="direct:subscribe"/>
  <to uri="dynamic-router-control:unsubscribe?subscribeChannel=myChannel&amp;subscriptionId=mySubId"/>
</route>
- route:
    from:
      uri: direct:subscribe
      steps:
        - to:
            uri: dynamic-router-control:unsubscribe
            parameters:
              subscribeChannel: myChannel
              subscriptionId: mySubId
Example Java URI ProducerTemplate Unsubscribe
CamelContext context = new DefaultCamelContext();
context.start();
ProducerTemplate template = context.createProducerTemplate();
RouteBuilder.addRoutes(context, rb -> {
    // Route for unsubscribing
    rb.from("direct://unsubscribe")
            .toD("dynamic-router-control://unsubscribe" +
                    "?subscribeChannel=${header.subscribeChannel}" +
                    "&subscriptionId=${header.subscriptionId}");
});
template.sendBodyAndHeaders("direct:unsubscribe", "",
        Map.of("subscribeChannel", "test",
                "subscriptionId", "testSubscription1"));

Above, because the control URI is dynamic, we have to send it from a ProducerTemplate in a different way. The dynamic-aware endpoint uses headers, rather than URI params, so we set the headers deliberately.

DynamicRouterControlMessage example

Example Java DynamicRouterControlMessage Unsubscribe
DynamicRouterControlMessage controlMessage = DynamicRouterControlMessage.newBuilder()
    .subscribeChannel("myChannel")
    .subscriptionId("mySubId")
    .build();
producerTemplate.sendBody("dynamic-router-control:unsubscribe", controlMessage);

The Dynamic Rule Base

To determine if an exchange is suitable for any of the participants, all predicates for the participants that are subscribed to the channel are evaluated until the first result of "true" is found, by default. If the Dynamic Router is configured with the recipientMode set to allMatch, then all recipients with matching predicates will be selected. The exchange will be routed to the corresponding endpoint(s). The rule base contains a default filter registered at the least priority (which is the highest integer number). Like the "default" case of a switch statement in Java, any message that is not appropriate for any registered participants will be processed by this filter. The filter logs information about the dropped message at debug level, by default. To turn the level up to warn, include warnDroppedMessage=true in the component URI.

Rules are registered in a channel, and they are logically separate from rules in another channel. Subscription IDs must be unique within a channel, although multiple subscriptions of the same name may coexist in a dynamic router instance if they are in separate channels.

The Dynamic Router employs the use of Predicate as rules. Any valid predicate may be used to determine the suitability of exchanges for a participating recipient, whether they are simple or compound predicates. Although it is advised to view the complete documentation, an example simple predicate might look like the following:

Example simple predicate
// The "messageType" must be "payment"
Predicate msgType = header("messageType").isEqualTo("payment");

JMX Control and Monitoring Operations

The Dynamic Router Control component supports some JMX operations that allow you to control and monitor the component. It is beyond the scope of this document to go into detail about JMX, so this is a list of the operations that are supported. For more information about JMX, see the JMX documentation.

Subscribing with a predicate expression
String subscribeWithPredicateExpression(String, String, String, int, String, String, boolean)

This operation provides the ability to subscribe to a channel with a predicate expression. The parameters, in order, are as follows:

  • subscription ID

  • channel name

  • destination URI

  • priority

  • predicate expression

  • expression language

  • update the subscription (true), or add a new one (false)

Subscribing with a predicate bean
String subscribeWithPredicateBean(String, String, String, int, String, boolean)

This operation provides the ability to subscribe to a channel with the name of a Predicate that has been bound in the registry. The parameters, in order, are as follows:

  • subscription ID

  • channel name

  • destination URI

  • priority

  • predicate bean name

  • update the subscription (true), or add a new one (false)

Subscribing with a predicate instance
String subscribeWithPredicateInstance(String, String, String, int, Object, boolean)

This operation provides the ability to subscribe to a channel with an instance of a Predicate. The parameters, in order, are as follows:

  • subscription ID

  • channel name

  • destination URI

  • priority

  • predicate instance

  • update the subscription (true), or add a new one (false)

Unsubscribing
boolean removeSubscription(String, String)

This operation provides the ability to unsubscribe from a channel. The parameters, in order, are as follows:

  • subscription ID

  • channel name

Getting the subscriptions map
Map<String, ConcurrentSkipListSet<PrioritizedFilter>> getSubscriptionsMap()

This operation provides the ability to get the subscriptions map. The map is keyed by channel name, and the values are a set of prioritized filters.

Getting the subscriptions statistics map
Map<String, List<PrioritizedFilterStatistics>> getSubscriptionsStatisticsMap()

This operation provides the ability to get the subscriptions statistics map. The map is keyed by channel name, and the values are a list of prioritized filter statistics, including the number of messages that have matched the filter, and had the exchange sent to the destination URI.