alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Java example source code file (ZookeeperConfigurationRetriever.java)

This example Java source code file (ZookeeperConfigurationRetriever.java) is included in the alvinalexander.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Learn more about this Java project at its project page.

Java - Java tags/keywords

attempting, configuration, exception, found, illegalstateexception, interruptedexception, logger, net, network, nothing, override, returning, stat, string, util, watcher, zookeeperconfigurationretriever

The ZookeeperConfigurationRetriever.java Java example source code

/*
 *
 *  * Copyright 2015 Skymind,Inc.
 *  *
 *  *    Licensed under the Apache License, Version 2.0 (the "License");
 *  *    you may not use this file except in compliance with the License.
 *  *    You may obtain a copy of the License at
 *  *
 *  *        http://www.apache.org/licenses/LICENSE-2.0
 *  *
 *  *    Unless required by applicable law or agreed to in writing, software
 *  *    distributed under the License is distributed on an "AS IS" BASIS,
 *  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  *    See the License for the specific language governing permissions and
 *  *    limitations under the License.
 *
 */

package org.deeplearning4j.scaleout.zookeeper;

import java.net.InetAddress;
import java.util.Arrays;
import java.util.List;

import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.Watcher.Event.KeeperState;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.Stat;
import org.canova.api.conf.Configuration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Retrieves configuration data serialized
 * by {@link ZooKeeperConfigurationRegister}
 * @author Adam Gibson
 *
 */
public class ZookeeperConfigurationRetriever implements Watcher {

    private ZooKeeper keeper;
    private String host;
    private int port;
    private String id;
    private static final Logger log = LoggerFactory.getLogger(ZookeeperConfigurationRetriever.class);



    public ZookeeperConfigurationRetriever( String host,
                                            int port, String id) {
        super();
        this.keeper = new ZookeeperBuilder().setHost(host).setPort(port).build();
        this.host = host;
        this.port = port;
        this.id = id;
    }



    public Configuration retrieve(String host) throws Exception {
        Configuration conf;
        String path = new ZookeeperPathBuilder().addPaths(Arrays.asList("tmp",id)).setHost(host).setPort(port).build();
        Stat stat = keeper.exists(path, false);
        if(stat == null) {
            List<String> list = keeper.getChildren( new ZookeeperPathBuilder().setHost(host).setPort(port).addPath("tmp").build(), false);
            throw new IllegalStateException("Nothing found for " + path + " possible children include " + list);
        }
        byte[] data = keeper.getData(path, false, stat ) ;
        conf = (Configuration) ZooKeeperConfigurationRegister.deserialize(data);


        return conf;
    }

    public Configuration retrieve() throws Exception {
        Configuration c = null;
        String localhost = InetAddress.getLocalHost().getHostName();

        String[] hosts = { host,"127.0.0.1","localhost",localhost };

        for(int i = 0; i < hosts.length; i++) {
            try {
                log.info("Attempting to retrieve conf from " + hosts[i]);
                c = retrieve(hosts[i]);
                if(c != null) {
                    log.info("Found from host " + hosts[i]);
                    break;
                }
            }catch(Exception e) {
                log.warn("Trying next host " + hosts[i] + " failed");
            }


        }

        log.info("Returning conf from host" + host);
        return c;
    }

    public void close() {
        try {
            keeper.close();
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
    }


    @Override
    public void process(WatchedEvent event) {
        if(event.getState() == KeeperState.Expired) {
            keeper = new ZookeeperBuilder().setHost(host).setPort(port).setWatcher(this).build();

        }
    }
}

Other Java examples (source code examples)

Here is a short list of links related to this Java ZookeeperConfigurationRetriever.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.