camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Claus Ibsen (Confluence)" <conflue...@apache.org>
Subject [CONF] Apache Camel > Zookeeper
Date Fri, 01 Nov 2013 09:11:00 GMT
<html>
<head>
    <base href="https://cwiki.apache.org/confluence">
            <link rel="stylesheet" href="/confluence/s/en/2176/1/1/_/styles/combined.css?spaceKey=CAMEL&amp;forWysiwyg=true"
type="text/css">
    </head>
<body style="background: white;" bgcolor="white" class="email-body">
<div id="pageContent">
<div id="notificationFormat">
<div class="wiki-content">
<div class="email">
    <h2><a href="https://cwiki.apache.org/confluence/display/CAMEL/Zookeeper">Zookeeper</a></h2>
    <h4>Page <b>edited</b> by             <a href="https://cwiki.apache.org/confluence/display/~davsclaus">Claus
Ibsen</a>
    </h4>
        <br/>
                         <h4>Changes (10)</h4>
                                 
    
<div id="page-diffs">
                    <table class="diff" cellpadding="0" cellspacing="0">
    
            <tr><td class="diff-snipped" >...<br></td></tr>
            <tr><td class="diff-unchanged" >{div:class=confluenceTableSmall} <br>||
Name || Default Value || Description || <br></td></tr>
            <tr><td class="diff-deleted-lines" style="color:#999;background-color:#fdd;text-decoration:line-through;">|
{{sessionId}} | {{null}} | The session id used to identify a connection to the cluster | <br>|
{{password}} | {{null}} | The password to use when making a connection | <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">|
{{path}} | | The node in the ZooKeeper server (aka znode) | <br></td></tr>
            <tr><td class="diff-changed-lines" >| {{listChildren}} | {{false}}
| <span class="diff-changed-words"><span class="diff-deleted-chars"style="color:#999;background-color:#fdd;text-decoration:line-through;">w</span><span
class="diff-added-chars"style="background-color: #dfd;">W</span>hether</span>
the children of the node should be listed | <br></td></tr>
            <tr><td class="diff-changed-lines" >| {{repeat}} | {{false}} | <span
class="diff-changed-words"><span class="diff-deleted-chars"style="color:#999;background-color:#fdd;text-decoration:line-through;">s</span><span
class="diff-added-chars"style="background-color: #dfd;">S</span>hould</span>
changes to the znode be &#39;watched&#39; and repeatedly processed. | <br></td></tr>
            <tr><td class="diff-changed-lines" >| {{backoff}} | {{5000}} | <span
class="diff-changed-words"><span class="diff-deleted-chars"style="color:#999;background-color:#fdd;text-decoration:line-through;">t</span><span
class="diff-added-chars"style="background-color: #dfd;">T</span>he</span> time
interval to backoff for after an error before retrying. | <br></td></tr>
            <tr><td class="diff-changed-lines" >| {{timeout}} | {{5000}} | <span
class="diff-changed-words"><span class="diff-deleted-chars"style="color:#999;background-color:#fdd;text-decoration:line-through;">t</span><span
class="diff-added-chars"style="background-color: #dfd;">T</span>he</span> time
interval to wait on connection before timing out. | <br></td></tr>
            <tr><td class="diff-changed-lines" >| {{create}} | {{false}} | <span
class="diff-changed-words"><span class="diff-deleted-chars"style="color:#999;background-color:#fdd;text-decoration:line-through;">s</span><span
class="diff-added-chars"style="background-color: #dfd;">S</span>hould</span>
the endpoint create the node if it does not currently exist. | <br></td></tr>
            <tr><td class="diff-changed-lines" >| {{createMode}} | {{EPHEMERAL}}
| <span class="diff-changed-words"><span class="diff-deleted-chars"style="color:#999;background-color:#fdd;text-decoration:line-through;">t</span><span
class="diff-added-chars"style="background-color: #dfd;">T</span>he</span> create
mode that should be used for the newly created node (see below). | <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">|
{{listChildren}} | {{false}} | Whether to recursive get children of the zknode as well. |
<br></td></tr>
            <tr><td class="diff-changed-lines" >| {{sendEmptyMessageOnDelete}}
| {{true}} | *Camel 2.10:* <span class="diff-changed-words"><span class="diff-deleted-chars"style="color:#999;background-color:#fdd;text-decoration:line-through;">u</span><span
class="diff-added-chars"style="background-color: #dfd;">U</span>pon</span>
the delete of a znode, should an empty message be send to the consumer | <br></td></tr>
            <tr><td class="diff-unchanged" >{div} <br> <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
    
            </table>
    </div>                            <h4>Full Content</h4>
                    <div class="notificationGreySide">
        <h2><a name="Zookeeper-ZooKeeper"></a>ZooKeeper</h2>

<p><b>Available as of Camel 2.9</b></p>

<p>The ZooKeeper component allows interaction with a <a href="http://hadoop.apache.org/zookeeper/"
class="external-link" rel="nofollow">ZooKeeper</a> cluster and exposes the following
features to Camel:</p>

<ol>
	<li>Creation of nodes in any of the ZooKeeper create modes.</li>
	<li>Get and Set the data contents of arbitrary cluster nodes.</li>
	<li>Create and retrieve the list the child nodes attached to a particular node.</li>
	<li>A Distributed <a href="/confluence/display/CAMEL/RoutePolicy" title="RoutePolicy">RoutePolicy</a>
that leverages a Leader election coordinated by ZooKeeper to determine if exchanges should
get processed.</li>
</ol>


<p>Maven users will need to add the following dependency to their <tt>pom.xml</tt>
for this component:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="theme: Default; brush: xml; gutter: false" style="font-size:12px; font-family:
ConfluenceInstalledFont,monospace;">
&lt;dependency&gt;
    &lt;groupId&gt;org.apache.camel&lt;/groupId&gt;
    &lt;artifactId&gt;camel-zookeeper&lt;/artifactId&gt;
    &lt;version&gt;x.x.x&lt;/version&gt;
    &lt;!-- use the same version as your Camel core version --&gt;
&lt;/dependency&gt;
</pre>
</div></div>

<h3><a name="Zookeeper-URIformat"></a>URI format</h3>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="theme: Default; brush: java; gutter: false" style="font-size:12px; font-family:
ConfluenceInstalledFont,monospace;">
zookeeper://zookeeper-server[:port][/path][?options]
</pre>
</div></div>

<p>The path from the uri specifies the node in the ZooKeeper server (aka znode) that
will be the target of the endpoint.</p>

<h3><a name="Zookeeper-Options"></a>Options</h3>
<div class="confluenceTableSmall"><div class='table-wrap'>
<table class='confluenceTable'><tbody>
<tr>
<th class='confluenceTh'> Name </th>
<th class='confluenceTh'> Default Value </th>
<th class='confluenceTh'> Description </th>
</tr>
<tr>
<td class='confluenceTd'> <tt>path</tt> </td>
<td class='confluenceTd'>&nbsp;</td>
<td class='confluenceTd'> The node in the ZooKeeper server (aka znode) </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>listChildren</tt> </td>
<td class='confluenceTd'> <tt>false</tt> </td>
<td class='confluenceTd'> Whether the children of the node should be listed </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>repeat</tt> </td>
<td class='confluenceTd'> <tt>false</tt> </td>
<td class='confluenceTd'> Should changes to the znode be 'watched' and repeatedly processed.
</td>
</tr>
<tr>
<td class='confluenceTd'> <tt>backoff</tt> </td>
<td class='confluenceTd'> <tt>5000</tt> </td>
<td class='confluenceTd'> The time interval to backoff for after an error before retrying.
</td>
</tr>
<tr>
<td class='confluenceTd'> <tt>timeout</tt> </td>
<td class='confluenceTd'> <tt>5000</tt> </td>
<td class='confluenceTd'> The time interval to wait on connection before timing out.
</td>
</tr>
<tr>
<td class='confluenceTd'> <tt>create</tt> </td>
<td class='confluenceTd'> <tt>false</tt> </td>
<td class='confluenceTd'> Should the endpoint create the node if it does not currently
exist. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>createMode</tt> </td>
<td class='confluenceTd'> <tt>EPHEMERAL</tt> </td>
<td class='confluenceTd'> The create mode that should be used for the newly created
node (see below). </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>listChildren</tt> </td>
<td class='confluenceTd'> <tt>false</tt> </td>
<td class='confluenceTd'> Whether to recursive get children of the zknode as well. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>sendEmptyMessageOnDelete</tt> </td>
<td class='confluenceTd'> <tt>true</tt> </td>
<td class='confluenceTd'> <b>Camel 2.10:</b> Upon the delete of a znode,
should an empty message be send to the consumer </td>
</tr>
</tbody></table>
</div>
</div>

<h3><a name="Zookeeper-Usecases"></a>Use cases</h3>

<h4><a name="Zookeeper-Readingfromaznode."></a>Reading from a znode.</h4>

<p>The following snippet will read the data from the znode '/somepath/somenode/' provided
that it already exists. The data retrieved will<br/>
be placed into an exchange and passed onto the rest of the route.</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="theme: Default; brush: java; gutter: false" style="font-size:12px; font-family:
ConfluenceInstalledFont,monospace;">
from("zookeeper://localhost:39913/somepath/somenode").to("mock:result");
</pre>
</div></div>

<p>if the node does not yet exist then a flag can be supplied to have the endpoint await
its creation</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="theme: Default; brush: java; gutter: false" style="font-size:12px; font-family:
ConfluenceInstalledFont,monospace;">
from("zookeeper://localhost:39913/somepath/somenode?awaitCreation=true").to("mock:result");
</pre>
</div></div>

<h4><a name="Zookeeper-Readingfromaznode%28additionalCamel2.10onwards%29"></a>Reading
from a znode - (additional Camel 2.10 onwards)</h4>

<p>When data is read due to a WatchedEvent received from the ZooKeeper ensemble, the
CamelZookeeperEventType header holds ZooKeeper's <a href="http://zookeeper.apache.org/doc/current/api/org/apache/zookeeper/Watcher.Event.EventType.html"
class="external-link" rel="nofollow">EventType</a> value from that WatchedEvent.
If the data is read initially (not triggered by a WatchedEvent) the CamelZookeeperEventType
header will not be set.</p>


<h4><a name="Zookeeper-Writingtoaznode."></a>Writing to a znode.</h4>

<p>The following snippet will write the payload of the exchange into the znode at '/somepath/somenode/'
provided that it already exists</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="theme: Default; brush: java; gutter: false" style="font-size:12px; font-family:
ConfluenceInstalledFont,monospace;">
from("direct:write-to-znode").to("zookeeper://localhost:39913/somepath/somenode");
</pre>
</div></div>

<p>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'</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="theme: Default; brush: java; gutter: false" style="font-size:12px; font-family:
ConfluenceInstalledFont,monospace;">
Exchange e = createExchangeWithBody(testPayload);
template.sendBodyAndHeader("direct:write-to-znode", e, "CamelZooKeeperNode", "/somepath/someothernode");
</pre>
</div></div>

<p>To also create the node if it does not exist the 'create' option should be used.</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="theme: Default; brush: java; gutter: false" style="font-size:12px; font-family:
ConfluenceInstalledFont,monospace;">
from("direct:create-and-write-to-znode").to("zookeeper://localhost:39913/somepath/somenode?create=true");
</pre>
</div></div>

<p>Starting <b>version 2.11</b> it is also possible to <b>delete</b>
a node using the header 'CamelZookeeperOperation' by setting it to 'DELETE'.</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="theme: Default; brush: java; gutter: false" style="font-size:12px; font-family:
ConfluenceInstalledFont,monospace;">
from("direct:delete-znode").setHeader(ZooKeeperMessage.ZOOKEEPER_OPERATION, constant("DELETE")).to("zookeeper://localhost:39913/somepath/somenode");
</pre>
</div></div>

<p>or equivalently</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="theme: Default; brush: xml; gutter: false" style="font-size:12px; font-family:
ConfluenceInstalledFont,monospace;">
&lt;route&gt;
  &lt;from uri="direct:delete-znode" /&gt;
  &lt;setHeader headerName="CamelZookeeperOperation"&gt;
     &lt;constant&gt;DELETE&lt;/constant&gt;
  &lt;/setHeader&gt;
  &lt;to uri="zookeeper://localhost:39913/somepath/somenode" /&gt;
&lt;/route&gt;
</pre>
</div></div>

<p>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 <a
href="http://zookeeper.apache.org/doc/trunk/zookeeperProgrammers.html#Ephemeral+Nodes" class="external-link"
rel="nofollow">here</a>. 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</p>

<ul class="alternate" type="square">
	<li>PERSISTENT</li>
	<li>PERSISTENT_SEQUENTIAL</li>
	<li>EPHEMERAL</li>
	<li>EPHEMERAL_SEQUENTIAL</li>
</ul>


<p>For example to create a persistent znode via the URI config</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="theme: Default; brush: java; gutter: false" style="font-size:12px; font-family:
ConfluenceInstalledFont,monospace;">
from("direct:create-and-write-to-persistent-znode").to("zookeeper://localhost:39913/somepath/somenode?create=true&amp;createMode=PERSISTENT");
</pre>
</div></div>

<p>or using the header 'CamelZookeeperCreateMode'</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="theme: Default; brush: java; gutter: false" style="font-size:12px; font-family:
ConfluenceInstalledFont,monospace;">
Exchange e = createExchangeWithBody(testPayload);
template.sendBodyAndHeader("direct:create-and-write-to-persistent-znode", e, "CamelZooKeeperCreateMode",
"PERSISTENT");
</pre>
</div></div>


<h3><a name="Zookeeper-ZooKeeperenabledRoutepolicy."></a>ZooKeeper enabled
Route policy.</h3>

<p>ZooKeeper allows for very simple and effective leader election out of the box; This
component exploits this election capability in a <a href="/confluence/display/CAMEL/RoutePolicy"
title="RoutePolicy">RoutePolicy</a> to control when and how routes are enabled. This
policy would typically be used in fail-over scenarios, to control identical instances of a
route across a cluster of Camel based servers. A very common scenario is a simple 'Master-Slave'
setup where there are multiple instances of a route distributed across a cluster but only
one of them, that of the master, should be running at a time. If the master fails, a new master
should be elected from the available slaves and the route in this new master should be started.</p>

<p>The policy uses a common znode path across all instances of the RoutePolicy that
will be involved in the election. Each policy writes its id into this node and zookeeper will
order the writes in the order it received them. The policy then reads the listing of the node
to see what postion of its id; this postion is used to determine if the route should be started
or not. The policy is configured at startup with the number of route instances that should
be started across the cluster and if its position in the list is less than this value then
its route will be started. For a Master-slave scenario, the route is configured with 1 route
instance and only the first entry in the listing will start its route. All policies watch
for updates to the listing and if the listing changes they recalculate if their route should
be started. For more info on Zookeeper's Leader election capability see <a href="http://zookeeper.apache.org/doc/trunk/recipes.html#sc_leaderElection"
class="external-link" rel="nofollow">this page</a>.</p>

<p>The following example uses the node '/someapplication/somepolicy' for the election
and is set up to start only the top '1' entries in the node listing i.e. elect a master</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="theme: Default; brush: java; gutter: false" style="font-size:12px; font-family:
ConfluenceInstalledFont,monospace;">
ZooKeeperRoutePolicy policy = new ZooKeeperRoutePolicy("zookeeper:localhost:39913/someapp/somepolicy",
1);
from("direct:policy-controlled").routePolicy(policy).to("mock:controlled");
</pre>
</div></div>
<h3><a name="Zookeeper-SeeAlso"></a>See Also</h3>
<ul>
	<li><a href="/confluence/display/CAMEL/Configuring+Camel" title="Configuring Camel">Configuring
Camel</a></li>
	<li><a href="/confluence/display/CAMEL/Component" title="Component">Component</a></li>
	<li><a href="/confluence/display/CAMEL/Endpoint" title="Endpoint">Endpoint</a></li>
	<li><a href="/confluence/display/CAMEL/Getting+Started" title="Getting Started">Getting
Started</a></li>
</ul>

    </div>
        <div id="commentsSection" class="wiki-content pageSection">
        <div style="float: right;" class="grey">
                        <a href="https://cwiki.apache.org/confluence/users/removespacenotification.action?spaceKey=CAMEL">Stop
watching space</a>
            <span style="padding: 0px 5px;">|</span>
                <a href="https://cwiki.apache.org/confluence/users/editmyemailsettings.action">Change
email notification preferences</a>
</div>
        <a href="https://cwiki.apache.org/confluence/display/CAMEL/Zookeeper">View Online</a>
        |
        <a href="https://cwiki.apache.org/confluence/pages/diffpagesbyversion.action?pageId=27825114&revisedVersion=12&originalVersion=11">View
Changes</a>
            </div>
</div>
</div>
</div>
</div>
</body>
</html>

Mime
View raw message