标签:
/*** @param client client instance* @param mode creation/protection mode* @param basePath the base path for the node* @param data data for the node*/public PersistentEphemeralNode(CuratorFramework client, Mode mode, String basePath, byte[] initData)
public class PersistentEphemeralNodeExample{private static final String PATH = "/example/ephemeralNode";private static final String PATH2 = "/example/node";public static void main(String[] args) throws Exception{CuratorFramework client = CuratorFrameworkFactory.newClient("127.0.0.1:2181", new ExponentialBackoffRetry(1000, 3));client.getConnectionStateListenable().addListener(new ConnectionStateListener(){@Overridepublic void stateChanged(CuratorFramework client, ConnectionState newState){System.out.println("连接状态:" + newState.name());}});client.start();PersistentEphemeralNode node = new PersistentEphemeralNode(client, Mode.EPHEMERAL, PATH, "临时节点".getBytes());node.start();node.waitForInitialCreate(3, TimeUnit.SECONDS);String actualPath = node.getActualPath();System.out.println("临时节点路径:" + actualPath + " | 值: " + new String(client.getData().forPath(actualPath)));client.create().forPath(PATH2, "持久化节点".getBytes());System.out.println("持久化节点路径: " + PATH2 + " | 值: " + new String(client.getData().forPath(PATH2)));KillSession.kill(client.getZookeeperClient().getZooKeeper(), "127.0.0.1:2181");System.out.println("临时节点路径:" + actualPath + " | 是否存在: " + (client.checkExists().forPath(actualPath) != null));System.out.println("持久化节点路径: " + PATH2 + " | 值: " + new String(client.getData().forPath(PATH2)));CloseableUtils.closeQuietly(node);CloseableUtils.closeQuietly(client);}}
连接状态:CONNECTED临时节点路径:/example/ephemeralNode | 值: 临时节点持久化节点路径: /example/node | 值: 持久化节点连接状态:SUSPENDED连接状态:LOST连接状态:RECONNECTED临时节点路径:/example/ephemeralNode | 是否存在: true持久化节点路径: /example/node | 值: 持久化节点
标签:
原文地址:http://www.cnblogs.com/LiZhiW/p/4942730.html