Camel Components

JGroups raft

Since Camel 2.24

Both producer and consumer are supported

JGroups-raft is a Raft implementation in JGroups. The jgroups-raft: component provides interoperability between camel and a JGroups-raft clusters.

Maven users will need to add the following dependency to their pom.xml for this component.

<dependency>
    <groupId>org.apache.camel</groupId>
    <artifactId>camel-jgroups-raft</artifactId>
    <!-- use the same version as your Camel core version -->
    <version>x.y.z</version>
</dependency>

URI format

jgroups-raft:clusterName[?options]

Where clusterName represents the name of the JGroups-raft cluster, the component should connect to.

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 JGroups raft component supports the following options which are listed below.

Name Description Default Type

channelProperties (common)

The JGroups XML configuration of the JChannel that each endpoint creates, as a file, a URL or a classpath resource. If not set, the raft.xml in the camel-jgroups-raft JAR is used, and a WARN is logged when an endpoint starts. That raft.xml is a sample for development and tests: it uses UDP multicast without authentication or encryption, and lists A as the only RAFT member. In production, set your own configuration, with AUTH and SYM_ENCRYPT or ASYM_ENCRYPT, and the members of the cluster in the members attribute of RAFT. Not used when the raftHandle option is set.

raft.xml

String

raftHandle (common)

To use a custom org.jgroups.raft.RaftHandle for all the endpoints of this component. When set, the raftId, channelProperties and stateMachine options are not used. The component closes its channel and its log when the component stops.

RaftHandle

raftId (common)

Required The Raft ID of this member, which also names the JChannel. A member whose ID is not in the members attribute of RAFT in the channel configuration is a Learner, which does not vote. If not set, the raft_id of the channel configuration is used. Not used when the raftHandle option is set.

String

stateMachine (common)

The org.jgroups.raft.StateMachine that applies the committed log entries, for the RaftHandle that each endpoint creates. The default NopStateMachine ignores the entries and returns an empty byte array. Not used when the raftHandle option is set.

NopStateMachine

StateMachine

bridgeErrorHandler (consumer)

Allows for bridging the consumer to the Camel routing Error Handler, which mean any exceptions (if possible) occurred while the Camel consumer is trying to pickup incoming messages, or the likes, will now be processed as a message and handled by the routing Error Handler. Important: This is only possible if the 3rd party component allows Camel to be alerted if an exception was thrown. Some components handle this internally only, and therefore bridgeErrorHandler is not possible. In other situations we may improve the Camel component to hook into the 3rd party component and make this possible for future releases. By default the consumer will use the org.apache.camel.spi.ExceptionHandler to deal with exceptions, that will be logged at WARN or ERROR level and ignored.

false

boolean

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 JGroups raft endpoint is configured using URI syntax:

jgroups-raft:clusterName

With the following path and query parameters:

Path Parameters

Name Description Default Type

clusterName (common)

Required The name of the JGroups cluster that the channel of the RaftHandle connects to. All the members of the Raft cluster use the same name.

String

Query Parameters

Name Description Default Type

channelProperties (common)

The JGroups XML configuration of the JChannel that the endpoint creates, as a file, a URL or a classpath resource. If not set, the channelProperties of the component is used, else the raft.xml in the camel-jgroups-raft JAR, and a WARN is logged when the endpoint starts. That raft.xml is a sample for development and tests: it uses UDP multicast without authentication or encryption, and lists A as the only RAFT member. In production, set your own configuration, with AUTH and SYM_ENCRYPT or ASYM_ENCRYPT, and the members of the cluster in the members attribute of RAFT. Not used when the raftHandle option is set.

raft.xml

String

raftHandle (common)

To use a custom org.jgroups.raft.RaftHandle for this endpoint instead of the raftHandle of the component. When set, the raftId, channelProperties and stateMachine options are not used. The component closes its channel and its log when the component stops.

RaftHandle

raftId (common)

The Raft ID of this member, which also names the JChannel. A member whose ID is not in the members attribute of RAFT in the channel configuration is a Learner, which does not vote. If not set, the raftId of the component is used, else the raft_id of the channel configuration. Not used when the raftHandle option is set.

String

stateMachine (common)

The org.jgroups.raft.StateMachine that applies the committed log entries, for the RaftHandle that the endpoint creates. If not set, the stateMachine of the component is used. The default NopStateMachine ignores the entries and returns an empty byte array. Not used when the raftHandle option is set.

NopStateMachine

StateMachine

enableRoleChangeEvents (consumer)

Whether the consumer routes an exchange each time this member becomes the leader or a follower, with LEADER or FOLLOWER in the CamelJGroupsRaftEventType header. A Learner counts as a follower. If false, the consumer only connects the channel and routes no exchanges.

false

boolean

bridgeErrorHandler (consumer (advanced))

Allows for bridging the consumer to the Camel routing Error Handler, which mean any exceptions (if possible) occurred while the Camel consumer is trying to pickup incoming messages, or the likes, will now be processed as a message and handled by the routing Error Handler. Important: This is only possible if the 3rd party component allows Camel to be alerted if an exception was thrown. Some components handle this internally only, and therefore bridgeErrorHandler is not possible. In other situations we may improve the Camel component to hook into the 3rd party component and make this possible for future releases. By default the consumer will use the org.apache.camel.spi.ExceptionHandler to deal with exceptions, that will be logged at WARN or ERROR level and ignored.

false

boolean

exceptionHandler (consumer (advanced))

To let the consumer use a custom ExceptionHandler. Notice if the option bridgeErrorHandler is enabled then this option is not in use. By default the consumer will deal with exceptions, that will be logged at WARN or ERROR level and ignored.

ExceptionHandler

exchangePattern (consumer (advanced))

Sets the exchange pattern when the consumer creates an exchange.

Enum values:

  • InOnly

  • InOut

ExchangePattern

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

Message Headers

The JGroups raft component supports the following message header(s), which is/are listed below:

Name Description Default Type

CamelJGroupsRaftLogSize (consumer)

Constant: HEADER_JGROUPSRAFT_LOG_SIZE

The number of entries in the Raft log of this member. The consumer sets it on each event, and the producer after each set.

int

CamelJGroupsRaftCommitIndex (consumer)

Constant: HEADER_JGROUPSRAFT_COMMIT_INDEX

The commit index: the index of the last log entry that is committed. The consumer sets it on each event, and the producer after each set.

int

CamelJGroupsRaftCurrentTerm (consumer)

Constant: HEADER_JGROUPSRAFT_CURRENT_TERM

The current Raft term of this member. The consumer sets it on each event, and the producer after each set.

int

CamelJGroupsRaftIsLeader (consumer)

Constant: HEADER_JGROUPSRAFT_IS_LEADER

Whether this member is the Raft leader. The consumer sets it on each event, and the producer after each set.

boolean

CamelJGroupsRaftLastApplied (consumer)

Constant: HEADER_JGROUPSRAFT_LAST_APPLIED

The index of the last log entry that this member applied to its state machine. The consumer sets it on each event, and the producer after each set.

int

CamelJGroupsRaftLeaderAddress (consumer)

Constant: HEADER_JGROUPSRAFT_LEADER_ADDRESS

The JGroups address of the Raft leader, or null if no leader is known. The consumer sets it on each event, and the producer after each set.

Address

CamelJGroupsRaftRaftId (consumer)

Constant: HEADER_JGROUPSRAFT_RAFT_ID

The Raft ID of this member. The consumer sets it on each event, and the producer after each set.

String

CamelJGroupsRaftEventType (consumer)

Constant: HEADER_JGROUPSRAFT_EVENT_TYPE

The role that this member changed to: LEADER, or FOLLOWER (also for a Learner). The consumer sets it only with enableRoleChangeEvents.

Enum values:

  • LEADER

  • FOLLOWER

  • APPLY

  • READ_CONTENT_FROM

  • WRITE_CONTENT_TO

JGroupsRaftEventType

CamelJGroupsRaftSetOffset (producer)

Constant: HEADER_JGROUPSRAFT_SET_OFFSET

The offset in the body byte array of the data to set. Used only together with CamelJGroupsRaftSetLength: if either is missing, the whole body is set.

Integer

CamelJGroupsRaftSetLength (producer)

Constant: HEADER_JGROUPSRAFT_SET_LENGTH

The number of bytes of the body to set, from CamelJGroupsRaftSetOffset. Used only together with CamelJGroupsRaftSetOffset: if either is missing, the whole body is set.

Integer

CamelJGroupsRaftSetTimeout (producer)

Constant: HEADER_JGROUPSRAFT_SET_TIMEOUT

How long the producer waits for the set to complete, in the unit of CamelJGroupsRaftSetTimeUnit, before it fails with a TimeoutException. Used only when CamelJGroupsRaftSetOffset, CamelJGroupsRaftSetLength and CamelJGroupsRaftSetTimeUnit are set too; otherwise the producer waits without a timeout.

Long

CamelJGroupsRaftSetTimeUnit (producer)

Constant: HEADER_JGROUPSRAFT_SET_TIMEUNIT

The java.util.concurrent.TimeUnit of CamelJGroupsRaftSetTimeout. Used only when CamelJGroupsRaftSetOffset, CamelJGroupsRaftSetLength and CamelJGroupsRaftSetTimeout are set too.

Enum values:

  • NANOSECONDS

  • MICROSECONDS

  • MILLISECONDS

  • SECONDS

  • MINUTES

  • HOURS

  • DAYS

TimeUnit

Message Body

The message body at each side of the JGroups raft 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

Consumer

null

No body: an event for each change of the role of this member (only with enableRoleChangeEvents), with LEADER or FOLLOWER in the CamelJGroupsRaftEventType header and the state of the raft in the other headers.

Producer input

bytes, text, stream or file

The bytes to set() in the replicated log: a byte array, or text, a stream or a file converted to bytes. A null body is skipped: nothing is set. A Map, a List, a number or a POJO has no conversion to bytes and fails with an InvalidPayloadException.

Producer output

bytes or unchanged

The byte array the state machine returns for the set (an empty array with the default NopStateMachine), with the state of the raft in the headers. A null body is left as it was.

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.

Channel configuration

The channelProperties option (of the endpoint or the component) sets the JGroups XML configuration of the channel, as a file, a URL or a classpath resource. When it is not set, the endpoint uses raft.xml, which is the configuration in the camel-jgroups-raft JAR unless the application has its own raft.xml first on the classpath. The jgroupsConfig option of the JGroupsRaftClusterService has the same default.

The raft.xml in the JAR is a sample for development and tests:

  • it uses UDP multicast (228.5.5.5), without authentication or encryption. Any host on the same network that uses the same cluster name can join the cluster and take part in the Raft log, and the messages are sent in clear text;

  • it lists A as the only member in the members attribute of RAFT.

A WARN is logged when an endpoint (or the cluster service) starts with this default. In production, use your own JGroups configuration, under another name, with:

  • the AUTH protocol, and SYM_ENCRYPT or ASYM_ENCRYPT, so that only trusted members join the cluster and the messages are encrypted;

  • the Raft IDs of all the members of the cluster in the members attribute of RAFT (for example members="node1,node2,node3"), and the raft_id of each member;

  • a transport that fits the network, such as TCP with a list of the members, when multicast is not wanted.

from("direct:start").to("jgroups-raft:clusterName?raftId=node1&channelProperties=my-raft.xml");

See the JGroups-raft and JGroups documentation for the protocols.

Usage

Using jgroups-raft component with enableRoleChangeEvents=true on the consumer side of the route will capture change in JGroups-raft role and forward them to the Camel route. JGroups-raft consumer processes incoming messages asynchronously.

  • Java

  • XML

  • YAML

from("jgroups-raft:clusterName?enableRoleChangeEvents=true").to("seda:queue");
<route>
  <from uri="jgroups-raft:clusterName?enableRoleChangeEvents=true"/>
  <to uri="seda:queue"/>
</route>
- route:
    from:
      uri: jgroups-raft:clusterName
      parameters:
        enableRoleChangeEvents: true
      steps:
        - to:
            uri: seda:queue

Using jgroups-raft component on the producer side of the route will use the body of the camel exchange to perform a setX() operation on the raftHandle associated with the endpoint. The body is converted to a byte[] (such as from text, a stream or a file). A body that cannot be converted, such as a Map, a List, a number or a POJO, fails the exchange with an InvalidPayloadException. A null body is skipped: nothing is set.

  • Java

  • XML

  • YAML

from("direct:start").to("jgroups-raft:clusterName");
<route>
  <from uri="direct:start"/>
  <to uri="jgroups-raft:clusterName"/>
</route>
- route:
    from:
      uri: direct:start
      steps:
        - to:
            uri: jgroups-raft:clusterName

Examples

Receive cluster view change notifications

The snippet below demonstrates how to create the consumer endpoint listening to the change role events. By default, this option is off.

  • Java

  • XML

  • YAML

from("jgroups-raft:clusterName?enableRoleChangeEvents=true")
    .to("mock:mockEndpoint");
<route>
  <from uri="jgroups-raft:clusterName?enableRoleChangeEvents=true"/>
  <to uri="mock:mockEndpoint"/>
</route>
- route:
    from:
      uri: jgroups-raft:clusterName
      parameters:
        enableRoleChangeEvents: true
      steps:
        - to:
            uri: mock:mockEndpoint

The role change is forwarded as a LEADER or FOLLOWER event. A Learner (a non-voting member that replicates the log but cannot vote or become leader) is reported as a FOLLOWER event, so a route may receive consecutive FOLLOWER events, for example when a node steps down from leader to learner and again when it becomes a voting follower.

Keeping singleton route within the cluster

The snippet below demonstrates how to keep the singleton consumer route in the cluster of Camel Contexts. As soon as the master node dies, one of the slaves will be elected as a new master and started. In this particular example, we want to keep singleton jetty instance listening for the requests on address` http://localhost:8080/orders`.

Java-only: programmatic JGroupsRaftClusterService configuration
JGroupsRaftClusterService service = new JGroupsRaftClusterService();
service.setId("raftId");
service.setRaftId("raftId");
service.setJgroupsClusterName("clusterName");
...
context.addService(service);

from("master:mycluster:jetty:http://localhost:8080/orders").to("jms:orders");
All the namespaces of a node share one Raft member: the JGroupsRaftClusterService connects one RaftHandle (with the raft id of the service) for the cluster views of all namespaces. So the Raft leader is the leader of every namespace, and the master routes of all namespaces run on that node. Raft does not elect a leader per namespace.