zookeeper

package com.mobanker.config.bsw.zookeeperScheme.factory;
import java.util.HashMap;
import java.util.Map;
import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.RetryUntilElapsed;
/**
 * Copyright @ 2013QIANLONG. All right reserved. Class Name :
 * com.mobanker.config.core.factory Description : Author : gaohailiang Date :
 * 2016/1/15
 */
public class ZookeeperFactory {

   public static final int MAX_RETRIES = 3;
   public static final int BASE_SLEEP_TIMEMS = 1000;
   public static final int MAX_ELAPSED_TIMEMS = 1000;
   public static final int SLEEP_MS_BETWEEN_RETRIES = 50;
   public static Map<String, CuratorFramework> clientPool = new HashMap<>();
   public synchronized static CuratorFramework getByZookeeperConfig(ZookeeperConfig zookeeperConfig) {
      if (clientPool.get(zookeeperConfig.getNameSpace()) == null) {
         RetryPolicy retryUntilElapsedPolicy = new RetryUntilElapsed(MAX_ELAPSED_TIMEMS, SLEEP_MS_BETWEEN_RETRIES);
CuratorFramework client = CuratorFrameworkFactory.builder().connectString(zookeeperConfig.getConnection())
               .retryPolicy(retryUntilElapsedPolicy).namespace(zookeeperConfig.getNameSpace()).connectionTimeoutMs(5000).build();
client.start();
clientPool.put(zookeeperConfig.getNameSpace(), client);
}
      return clientPool.get(zookeeperConfig.getNameSpace());
}

   public static void closeByZkConfig(ZookeeperConfig zookeeperConfig) {
      CuratorFramework client = clientPool.get(zookeeperConfig.getNameSpace());
      if (client != null) {
         client.close();
clientPool.remove(zookeeperConfig.getNameSpace());
}
   }

   public static void close() {
      CuratorFramework client = clientPool.get("");
      if (client != null) {
         client.close();
clientPool.remove("");
}
   }

   public synchronized static CuratorFramework get(Map<String, String> zookeeperConfigMap) {
      if (clientPool.get("") == null) {
         RetryPolicy retryUntilElapsedPolicy = new RetryUntilElapsed(MAX_ELAPSED_TIMEMS, SLEEP_MS_BETWEEN_RETRIES);
String connection = zookeeperConfigMap.get(ZkConstants.KEY_ZK_CONNECTSTRING);
         if (ValidateUtil.isNullOrEmpty(connection)) {
            return null;
}
         Integer timeOutMs = 30000;
         if (!ValidateUtil.isNullOrEmpty(zookeeperConfigMap.get(ZkConstants.KEY_ZK_CONNECTIONTIMEOUT))) {
            timeOutMs = Integer.valueOf(zookeeperConfigMap.get(ZkConstants.KEY_ZK_CONNECTIONTIMEOUT));
}
         CuratorFramework client = CuratorFrameworkFactory.builder().connectString(connection).retryPolicy(retryUntilElapsedPolicy)
               .connectionTimeoutMs(timeOutMs).build();
client.start();
clientPool.put("", client);
}
      return clientPool.get("");
}

   public static CuratorFramework get() {
      if (clientPool == null) {
         return null;
}
      return clientPool.get("");
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值