Advertisement
Guest User

Untitled

a guest
Jun 28th, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.67 KB | None | 0 0
  1. import org.apache.curator.framework.CuratorFramework;
  2. import org.apache.log4j.Logger;
  3. import org.apache.zookeeper.CreateMode;
  4. import org.apache.zookeeper.data.Stat;
  5.  
  6. import java.io.ByteArrayInputStream;
  7. import java.io.ByteArrayOutputStream;
  8. import java.io.ObjectInputStream;
  9. import java.io.ObjectOutputStream;
  10. import java.util.ArrayList;
  11. import java.util.List;
  12. import java.util.Set;
  13.  
  14. /**
  15. * 共享状态
  16. * Created on 16/11/12.
  17. */
  18. public final class ClusterStateUtils {
  19. private Logger log = Logger.getLogger(ClusterStateUtils.class);
  20.  
  21. private final String namespace = "/RCM";//命名空间
  22. private final String lockpath = "/lock";//锁
  23. private String businessType;//业务类型
  24. private Set<String> regions;
  25. private CuratorFramework client;
  26.  
  27.  
  28. private ClusterStateUtils() {
  29.  
  30. }
  31.  
  32. public ClusterStateUtils(String businessType, Set<String> regions,CuratorFramework client) {
  33. this.businessType = "/" + businessType;
  34. this.client = client;
  35. this.regions = regions;
  36. initWorkPath();
  37. }
  38.  
  39. /**
  40. * 从字节数组获取对象
  41. *
  42. * @EditTime 2007-8-13 上午11:46:34
  43. */
  44. public static <T> Object getObjectFromBytes(byte[] objBytes,Class<T> cls) throws Exception {
  45. if (objBytes == null || objBytes.length == 0) {
  46. return null;
  47. }
  48. ByteArrayInputStream bi = new ByteArrayInputStream(objBytes);
  49. ObjectInputStream oi = new ObjectInputStream(bi);
  50. return (T) oi.readObject();
  51. }
  52.  
  53. /**
  54. * 从对象获取一个字节数组
  55. *
  56. * @EditTime 2007-8-13 上午11:46:56
  57. */
  58. public static <T > byte[] getBytesFromObject(T obj) throws Exception {
  59. if (obj == null) {
  60. return null;
  61. }
  62. ByteArrayOutputStream bo = new ByteArrayOutputStream();
  63. ObjectOutputStream oo = new ObjectOutputStream(bo);
  64. oo.writeObject(obj);
  65. return bo.toByteArray();
  66. }
  67.  
  68. /**
  69. * 初始化工作目录
  70. */
  71. private void initWorkPath() {
  72. try {
  73. safeCreatePath(namespace + businessType + lockpath);
  74. for (String region : regions) {
  75. safeCreatePath(namespace + businessType + "/" + region);
  76. }
  77. } catch (Exception e) {
  78. log.error("initWorkPath",e);
  79. }
  80.  
  81. }
  82.  
  83. private void safeCreatePath(String path) throws Exception {
  84. Stat stat = client.checkExists().forPath(path);
  85. if (stat == null) {
  86. client.create().creatingParentsIfNeeded().forPath(path);
  87. }
  88. }
  89.  
  90. /**
  91. * 获取锁
  92. *
  93. * @param lockName
  94. * @return
  95. */
  96. public boolean acquire(String lockName) {
  97. boolean success = false;
  98. try {
  99. client.create().withMode(CreateMode.EPHEMERAL).forPath(namespace + businessType + lockpath + "/" + lockName);
  100. success = true;
  101. } catch (Exception e) {
  102. log.error("acquire",e);
  103. }
  104. return success;
  105. }
  106.  
  107. /**
  108. * 释放锁
  109. *
  110. * @param lockName
  111. * @return
  112. */
  113. public boolean release(String lockName) {
  114. boolean success = false;
  115. try {
  116. client.delete().forPath(namespace + businessType + lockpath + "/" + lockName);
  117. success = true;
  118. } catch (Exception e) {
  119. log.error("release",e);
  120. }
  121. return success;
  122. }
  123.  
  124. /**
  125. * 重建目录
  126. *
  127. * @param region
  128. * @return
  129. */
  130. public boolean rebuildPath(String region) {
  131. if (!regions.contains(region) || region.equals(lockpath)) {
  132. return false;
  133. }
  134. boolean success = false;
  135. try {
  136. client.delete().deletingChildrenIfNeeded().forPath(namespace + businessType + "/" + region);
  137. client.create().forPath(namespace + businessType + "/" + region);
  138. success = true;
  139. } catch (Exception e) {
  140. log.error("rebuildPath",e);
  141. }
  142. return success;
  143. }
  144.  
  145. /**
  146. * 获取值
  147. *
  148. * @param region
  149. * @param key
  150. * @return
  151. */
  152. public byte[] get(String region, String key) {
  153. byte[] bytes = null;
  154. try {
  155. bytes = client.getData().forPath(namespace + businessType + "/" + region + "/" + key);
  156. } catch (Exception e) {
  157. if (!region.contains("Try")) {
  158. log.error(region + key, e);
  159. }
  160. }
  161. return bytes;
  162. }
  163.  
  164. /**
  165. * 获取值
  166. *
  167. * @param region
  168. * @param key
  169. * @param targetClass
  170. * @param <T>
  171. * @return
  172. */
  173. public <T> T get(String region, String key, Class<T> targetClass) {
  174. T obj = null;
  175. byte[] bytes = null;
  176. try {
  177. bytes = get(region, key);
  178. obj = (T) getObjectFromBytes(bytes, targetClass);
  179. } catch (Exception e) {
  180. if (!region.contains("Try")) {
  181. log.error(region + key, e);
  182. }
  183. }
  184. return obj;
  185. }
  186.  
  187. /**
  188. * 写入值
  189. *
  190. * @param region
  191. * @param key
  192. * @param bytes
  193. * @return
  194. */
  195. public boolean set(String region, String key, byte[] bytes) {
  196. boolean success = false;
  197. try {
  198. Stat stat = client.checkExists().forPath(namespace + businessType + "/" + region + "/" + key);
  199. if (stat != null) {
  200. client.delete().forPath(namespace + businessType + "/" + region + "/" + key);
  201. }
  202. client.create().forPath(namespace + businessType + "/" + region + "/" + key, bytes);
  203. success = true;
  204. } catch (Exception e) {
  205. log.error("set",e);
  206. }
  207. return success;
  208. }
  209.  
  210. /**
  211. * 写入值
  212. *
  213. * @param region
  214. * @param key
  215. * @param obj
  216. * @param <T>
  217. * @return
  218. */
  219. public <T> boolean set(String region, String key, T obj) {
  220. boolean success = false;
  221. try {
  222. set(region, key, getBytesFromObject(obj));
  223. success = true;
  224. } catch (Exception e) {
  225. log.error("set",e);
  226. }
  227. return success;
  228. }
  229.  
  230. /**
  231. * 移除
  232. *
  233. * @param region
  234. * @param key
  235. * @return
  236. */
  237. public boolean remove(String region, String key) {
  238. boolean success = false;
  239. try {
  240. client.delete().guaranteed().forPath(namespace + businessType + "/" + region + "/" + key);
  241. success = true;
  242. } catch (Exception e) {
  243. log.error("remove",e);
  244. }
  245. return success;
  246. }
  247.  
  248.  
  249. /**
  250. * 获取子节点
  251. *
  252. * @param region
  253. * @return
  254. */
  255. public List<String> getChildren(String region) {
  256. List<String> keys = new ArrayList<String>();
  257. try {
  258. keys = client.getChildren().forPath(namespace + businessType + "/" + region);
  259. } catch (Exception e) {
  260. log.error("getChildren",e);
  261. }
  262. return keys;
  263. }
  264. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement