[[zookeeper-component]]
= ZooKeeper Component

*Available as of Camel version 2.9*

The ZooKeeper component allows interaction with a
http://hadoop.apache.org/zookeeper/[ZooKeeper] cluster and exposes the
following features to Camel:

1.  Creation of nodes in any of the ZooKeeper create modes.
2.  Get and Set the data contents of arbitrary cluster nodes (data
being set must be convertible to `byte[]`).
3.  Create and retrieve the list the child nodes attached to a
particular node.

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

[source,xml]
----
<dependency>
    <groupId>org.apache.camel</groupId>
    <artifactId>camel-zookeeper</artifactId>
    <version>x.x.x</version>
    <!-- use the same version as your Camel core version -->
</dependency>
----

== URI format

[source]
----
zookeeper://zookeeper-server[:port][/path][?options]
----

The path from the URI specifies the node in the ZooKeeper server (a.k.a.
_znode_) that will be the target of the endpoint:

== Options

// component options: START
The ZooKeeper component supports 2 options, which are listed below.



[width="100%",cols="2,5,^1,2",options="header"]
|===
| Name | Description | Default | Type
| *configuration* (advanced) | To use a shared ZooKeeperConfiguration |  | ZooKeeperConfiguration
| *basicPropertyBinding* (advanced) | Whether the component should use basic property binding (Camel 2.x) or the newer property binding with additional capabilities | false | boolean
|===
// component options: END

// endpoint options: START
The ZooKeeper endpoint is configured using URI syntax:

----
zookeeper:serverUrls/path
----

with the following path and query parameters:

=== Path Parameters (2 parameters):


[width="100%",cols="2,5,^1,2",options="header"]
|===
| Name | Description | Default | Type
| *serverUrls* | *Required* The zookeeper server hosts (multiple servers can be separated by comma) |  | String
| *path* | *Required* The node in the ZooKeeper server (aka znode) |  | String
|===


=== Query Parameters (13 parameters):


[width="100%",cols="2,5,^1,2",options="header"]
|===
| Name | Description | Default | Type
| *listChildren* (common) | Whether the children of the node should be listed | false | boolean
| *timeout* (common) | The time interval to wait on connection before timing out. | 5000 | int
| *backoff* (consumer) | The time interval to backoff for after an error before retrying. | 5000 | long
| *bridgeErrorHandler* (consumer) | Allows for bridging the consumer to the Camel routing Error Handler, which mean any exceptions occurred while the consumer is trying to pickup incoming messages, or the likes, will now be processed as a message and handled by the routing Error Handler. 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
| *repeat* (consumer) | Should changes to the znode be 'watched' and repeatedly processed. | false | boolean
| *sendEmptyMessageOnDelete* (consumer) | Upon the delete of a znode, should an empty message be send to the consumer | true | boolean
| *exceptionHandler* (consumer) | 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) | Sets the exchange pattern when the consumer creates an exchange. |  | ExchangePattern
| *create* (producer) | Should the endpoint create the node if it does not currently exist. | false | boolean
| *createMode* (producer) | The create mode that should be used for the newly created node | EPHEMERAL | String
| *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
| *basicPropertyBinding* (advanced) | Whether the endpoint should use basic property binding (Camel 2.x) or the newer property binding with additional capabilities | false | boolean
| *synchronous* (advanced) | Sets whether synchronous processing should be strictly used, or Camel is allowed to use asynchronous processing (if supported). | false | boolean
|===
// endpoint options: END
// spring-boot-auto-configure options: START
== Spring Boot Auto-Configuration

When using Spring Boot make sure to use the following Maven dependency to have support for auto configuration:

[source,xml]
----
<dependency>
  <groupId>org.apache.camel</groupId>
  <artifactId>camel-zookeeper-starter</artifactId>
  <version>x.x.x</version>
  <!-- use the same version as your Camel core version -->
</dependency>
----


The component supports 56 options, which are listed below.



[width="100%",cols="2,5,^1,2",options="header"]
|===
| Name | Description | Default | Type
| *camel.component.zookeeper.basic-property-binding* | Whether the component should use basic property binding (Camel 2.x) or the newer property binding with additional capabilities | false | Boolean
| *camel.component.zookeeper.cluster.service.attributes* | Custom service attributes. |  | Map
| *camel.component.zookeeper.cluster.service.auth-info-list* |  |  | List
| *camel.component.zookeeper.cluster.service.base-path* |  |  | String
| *camel.component.zookeeper.cluster.service.connection-timeout* |  |  | Long
| *camel.component.zookeeper.cluster.service.connection-timeout-unit* |  |  | TimeUnit
| *camel.component.zookeeper.cluster.service.curator-framework* |  |  | CuratorFramework
| *camel.component.zookeeper.cluster.service.enabled* | Sets if the zookeeper cluster service should be enabled or not, default is false. | false | Boolean
| *camel.component.zookeeper.cluster.service.id* | Cluster Service ID |  | String
| *camel.component.zookeeper.cluster.service.max-close-wait* |  |  | Long
| *camel.component.zookeeper.cluster.service.max-close-wait-unit* |  |  | TimeUnit
| *camel.component.zookeeper.cluster.service.namespace* |  |  | String
| *camel.component.zookeeper.cluster.service.nodes* |  |  | List
| *camel.component.zookeeper.cluster.service.order* | Service lookup order/priority. |  | Integer
| *camel.component.zookeeper.cluster.service.reconnect-base-sleep-time* |  |  | Long
| *camel.component.zookeeper.cluster.service.reconnect-base-sleep-time-unit* |  |  | TimeUnit
| *camel.component.zookeeper.cluster.service.reconnect-max-retries* |  |  | Integer
| *camel.component.zookeeper.cluster.service.reconnect-max-sleep-time* |  |  | Long
| *camel.component.zookeeper.cluster.service.reconnect-max-sleep-time-unit* |  |  | TimeUnit
| *camel.component.zookeeper.cluster.service.retry-policy* |  |  | RetryPolicy
| *camel.component.zookeeper.cluster.service.session-timeout* |  |  | Long
| *camel.component.zookeeper.cluster.service.session-timeout-unit* |  |  | TimeUnit
| *camel.component.zookeeper.configuration.backoff* | The time interval to backoff for after an error before retrying. | 5000 | Long
| *camel.component.zookeeper.configuration.create* | Should the endpoint create the node if it does not currently exist. | false | Boolean
| *camel.component.zookeeper.configuration.create-mode* | The create mode that should be used for the newly created node | EPHEMERAL | String
| *camel.component.zookeeper.configuration.list-children* | Whether the children of the node should be listed | false | Boolean
| *camel.component.zookeeper.configuration.path* | The node in the ZooKeeper server (aka znode) |  | String
| *camel.component.zookeeper.configuration.repeat* | Should changes to the znode be 'watched' and repeatedly processed. | false | Boolean
| *camel.component.zookeeper.configuration.send-empty-message-on-delete* | Upon the delete of a znode, should an empty message be send to the consumer | true | Boolean
| *camel.component.zookeeper.configuration.servers* | The zookeeper server hosts |  | List
| *camel.component.zookeeper.configuration.timeout* | The time interval to wait on connection before timing out. | 5000 | Integer
| *camel.component.zookeeper.enabled* | Enable zookeeper component | true | Boolean
| *camel.component.zookeeper.service-registry.attributes* | Custom service attributes. |  | Map
| *camel.component.zookeeper.service-registry.auth-info-list* |  |  | List
| *camel.component.zookeeper.service-registry.base-path* |  |  | String
| *camel.component.zookeeper.service-registry.connection-timeout* |  |  | Long
| *camel.component.zookeeper.service-registry.connection-timeout-unit* |  |  | TimeUnit
| *camel.component.zookeeper.service-registry.curator-framework* |  |  | CuratorFramework
| *camel.component.zookeeper.service-registry.deregister-services-on-stop* |  |  | Boolean
| *camel.component.zookeeper.service-registry.enabled* | Sets if the zookeeper service registry should be enabled or not, default is false. | false | Boolean
| *camel.component.zookeeper.service-registry.id* | Service Registry ID |  | String
| *camel.component.zookeeper.service-registry.max-close-wait* |  |  | Long
| *camel.component.zookeeper.service-registry.max-close-wait-unit* |  |  | TimeUnit
| *camel.component.zookeeper.service-registry.namespace* |  |  | String
| *camel.component.zookeeper.service-registry.nodes* |  |  | List
| *camel.component.zookeeper.service-registry.order* | Service lookup order/priority. |  | Integer
| *camel.component.zookeeper.service-registry.override-service-host* |  |  | Boolean
| *camel.component.zookeeper.service-registry.reconnect-base-sleep-time* |  |  | Long
| *camel.component.zookeeper.service-registry.reconnect-base-sleep-time-unit* |  |  | TimeUnit
| *camel.component.zookeeper.service-registry.reconnect-max-retries* |  |  | Integer
| *camel.component.zookeeper.service-registry.reconnect-max-sleep-time* |  |  | Long
| *camel.component.zookeeper.service-registry.reconnect-max-sleep-time-unit* |  |  | TimeUnit
| *camel.component.zookeeper.service-registry.retry-policy* |  |  | RetryPolicy
| *camel.component.zookeeper.service-registry.service-host* |  |  | String
| *camel.component.zookeeper.service-registry.session-timeout* |  |  | Long
| *camel.component.zookeeper.service-registry.session-timeout-unit* |  |  | TimeUnit
|===
// spring-boot-auto-configure options: END



== Use cases

=== Reading from a _znode_

The following snippet will read the data from the _znode_
`/somepath/somenode/` provided that it already exists. The data
retrieved will be placed into an exchange and passed onto
the rest of the route:

[source,java]
----
from("zookeeper://localhost:39913/somepath/somenode").to("mock:result");
----

If the node does not yet exist then a flag can be supplied to have the
endpoint await its creation:

[source,java]
----
from("zookeeper://localhost:39913/somepath/somenode?awaitCreation=true").to("mock:result");
----

=== Reading from a _znode_

When data is read due to a `WatchedEvent` received from the ZooKeeper
ensemble, the `CamelZookeeperEventType` header holds ZooKeeper's
http://zookeeper.apache.org/doc/current/api/org/apache/zookeeper/Watcher.Event.EventType.html[`EventType`]
value from that `WatchedEvent`. If the data is read initially (not
triggered by a `WatchedEvent`) the `CamelZookeeperEventType` header will not
be set.

=== Writing to a _znode_

The following snippet will write the payload of the exchange into the
znode at `/somepath/somenode/` provided that it already exists:

[source,java]
----
from("direct:write-to-znode")
    .to("zookeeper://localhost:39913/somepath/somenode");
----

For flexibility, the endpoint allows the target _znode_ to be specified
dynamically as a message header. If a header keyed by the string
`CamelZooKeeperNode` is present then the value of the header will be
used as the path to the _znode_ on the server. For instance using the same
route definition above, the following code snippet will write the data
not to `/somepath/somenode` but to the path from the header
`/somepath/someothernode`.

WARNING: the `testPayload` must be convertible
to `byte[]` as the data stored in ZooKeeper is byte based.

[source,java]
----
Object testPayload = ...
template.sendBodyAndHeader("direct:write-to-znode", testPayload, "CamelZooKeeperNode", "/somepath/someothernode");
----

To also create the node if it does not exist the `create` option should
be used.

[source,java]
----
from("direct:create-and-write-to-znode")
    .to("zookeeper://localhost:39913/somepath/somenode?create=true");
----

It is also possible to *delete* a node using the
header `CamelZookeeperOperation` by setting it to `DELETE`:

[source,java]
----
from("direct:delete-znode")
    .setHeader(ZooKeeperMessage.ZOOKEEPER_OPERATION, constant("DELETE"))
    .to("zookeeper://localhost:39913/somepath/somenode");
----

or equivalently:

[source,xml]
----
<route>
  <from uri="direct:delete-znode" />
  <setHeader name="CamelZookeeperOperation">
     <constant>DELETE</constant>
  </setHeader>
  <to uri="zookeeper://localhost:39913/somepath/somenode" />
</route>
----

ZooKeeper nodes can have different types; they can be 'Ephemeral' or
'Persistent' and 'Sequenced' or 'Unsequenced'. For further information
of each type you can check
http://zookeeper.apache.org/doc/trunk/zookeeperProgrammers.html#Ephemeral+Nodes[here].
By default endpoints will create unsequenced, ephemeral nodes, but the
type can be easily manipulated via a uri config parameter or via a
special message header. The values expected for the create mode are
simply the names from the `CreateMode` enumeration:

* `PERSISTENT`
* `PERSISTENT_SEQUENTIAL`
* `EPHEMERAL`
* `EPHEMERAL_SEQUENTIAL`

For example to create a persistent _znode_ via the URI config:

[source,java]
----
from("direct:create-and-write-to-persistent-znode")
    .to("zookeeper://localhost:39913/somepath/somenode?create=true&createMode=PERSISTENT");
----

or using the header `CamelZookeeperCreateMode`.

WARNING: the `testPayload` must be convertible to `byte[]` as the data stored in
ZooKeeper is byte based.

[source,java]
----
Object testPayload = ...
template.sendBodyAndHeader("direct:create-and-write-to-persistent-znode", testPayload, "CamelZooKeeperCreateMode", "PERSISTENT");
----

