Monday, September 18, 2017

Zoo

Zookeeper Leader Election
People
Bipin Gupta <bipin_gupta@yahoo.com>  Today at 11:01
To
yogesh.d.kulkarni@hsbc.co.in
Message body
package com.jini.basics.zookeeper;

// http://www.tothenew.com/blog/zookeeper-leader-election-simplified/

import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.recipes.locks.InterProcessMutex;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException.NodeExistsException;

public class LeaderElectionMain implements Runnable {

    public static void main(String[] args) {

        Thread object = new Thread(new LeaderElectionMain());
        Thread object3 = new Thread(new LeaderElectionMain());
        Thread object4 = new Thread(new LeaderElectionMain());
        Thread object5 = new Thread(new LeaderElectionMain());
        Thread object6 = new Thread(new LeaderElectionMain());
       
        object.start();
        object3.start();
        object4.start();
        object5.start();
        object6.start();
       

    } // end of main

    public synchronized static CuratorFramework getCuratorClient() {
        CuratorFramework _client = null;
            String zookeeperStr = "127.0.0.1:2181"; // zookeeper address
            RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
            _client = CuratorFrameworkFactory.newClient(zookeeperStr, retryPolicy);
            _client.start();
        return _client;
    } // getCuratorClient

    public static String createEphemeral(CuratorFramework client, String path, byte[] payload) throws Exception {
        // this will create the given EPHEMERAL ZNode with the given data using Curator protection.
        return client.create().withMode(CreateMode.EPHEMERAL).forPath(path, payload);
    }

    public static void delete(CuratorFramework client, String path) throws Exception {
        // delete the given node
        if (client.checkExists().forPath(path) != null) {
            client.delete().deletingChildrenIfNeeded().forPath(path);
        }
    }

    @Override
    public void run() {
        try {
            CuratorFramework _client = getCuratorClient();
            String lockPath = "/crud/a";
            String nodeAPath = null;
            InterProcessMutex lock = new InterProcessMutex(_client, lockPath);
            try {
                nodeAPath = createEphemeral(_client, "/crud/a", "a is ephemeral node".getBytes());
                System.out.println("creating master node created successfully =>> " +  nodeAPath );
                System.out.println("I'm Leader");
                System.out.println("work in progress ........");
                Thread.sleep(5000);
                System.out.println("work completed ........");
            } catch (NodeExistsException e) {
                System.out.println("==== Skipping JOB ====");
                // e.printStackTrace();
            }
        } catch (Exception e) {

            e.printStackTrace();
        }
    } // end of run

} // end of main()

No comments:

Post a Comment