com.alibaba.otter.shared.arbitrate.impl.zookeeper.ZooKeeperClient.java Source code

Java tutorial

Introduction

Here is the source code for com.alibaba.otter.shared.arbitrate.impl.zookeeper.ZooKeeperClient.java

Source

/*
 * Copyright (C) 2010-2101 Alibaba Group Holding Limited.
 *
 * 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 com.alibaba.otter.shared.arbitrate.impl.zookeeper;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.I0Itec.zkclient.IZkStateListener;
import org.apache.commons.lang.StringUtils;
import org.apache.zookeeper.Watcher.Event.KeeperState;

import com.alibaba.otter.shared.arbitrate.impl.config.ArbitrateConfigUtils;
import com.alibaba.otter.shared.common.utils.zookeeper.ZkClientx;
import com.google.common.base.Function;
import com.google.common.collect.MapMaker;

/**
 * ?ZooKeeper??zk???zk
 * 
 * @author jianghang 2011-9-8 ?07:55:44
 * @version 4.0.0
 */
public class ZooKeeperClient {

    private static String cluster;
    private static int sessionTimeout = 10 * 1000;
    private static Map<Long, ZkClientx> clients = new MapMaker().makeComputingMap(new Function<Long, ZkClientx>() {

        public ZkClientx apply(Long pipelineId) {
            return createClient();
        }
    });
    private static Long defaultId = 0L;

    /**
     * ?zookeeper
     */
    public static ZkClientx getInstance() {
        return getInstance(defaultId);
    }

    /**
     * ?pipelineId?zookeeper?pipelineId?zookeeper??
     */
    public static ZkClientx getInstance(Long pipelineId) {
        return clients.get(pipelineId);
    }

    public static void destory() {
        for (ZkClientx zkClient : clients.values()) {
            zkClient.close();
        }
    }

    public static void registerNotification(final SessionExpiredNotification notification) {
        getInstance().subscribeStateChanges(new IZkStateListener() {

            public void handleStateChanged(KeeperState state) throws Exception {

            }

            public void handleNewSession() throws Exception {
                notification.notification();
            }
        });

    }

    private static ZkClientx createClient() {
        List<String> serveraddrs = getServerAddrs();
        return new ZkClientx(StringUtils.join(serveraddrs, ","), sessionTimeout);
    }

    /**
     * ?node??zk?
     */
    private static List<String> getServerAddrs() {
        List<String> result = ArbitrateConfigUtils.getServerAddrs();
        if (result == null || result.size() == 0) {
            result = Arrays.asList(cluster);
        }
        return result;
    }

    public void setCluster(String cluster) {
        ZooKeeperClient.cluster = cluster;
    }

    public void setSessionTimeout(int timeout) {
        ZooKeeperClient.sessionTimeout = timeout;
    }

}