ZookeeperClient.java 3.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 * Copyright 2017, OpenSkywalking Organization All rights reserved.
 *
 * 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.
 *
 * Project repository: https://github.com/OpenSkywalking/skywalking
 */

P
pengys5 已提交
19 20 21
package org.skywalking.apm.collector.client.zookeeper;

import java.io.IOException;
22
import java.util.List;
P
pengys5 已提交
23 24
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
25
import org.apache.zookeeper.Watcher;
P
pengys5 已提交
26
import org.apache.zookeeper.ZooKeeper;
27
import org.apache.zookeeper.data.ACL;
P
pengys5 已提交
28 29 30 31 32 33 34 35 36 37 38 39 40 41
import org.apache.zookeeper.data.Stat;
import org.skywalking.apm.collector.core.client.Client;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author pengys5
 */
public class ZookeeperClient implements Client {

    private final Logger logger = LoggerFactory.getLogger(ZookeeperClient.class);

    private ZooKeeper zk;

42 43
    private final String hostPort;
    private final int sessionTimeout;
44
    private final Watcher watcher;
45

46
    public ZookeeperClient(String hostPort, int sessionTimeout, Watcher watcher) {
47 48
        this.hostPort = hostPort;
        this.sessionTimeout = sessionTimeout;
49
        this.watcher = watcher;
50 51
    }

P
pengys5 已提交
52 53
    @Override public void initialize() throws ZookeeperClientException {
        try {
54
            zk = new ZooKeeper(hostPort, sessionTimeout, watcher);
P
pengys5 已提交
55 56 57 58 59
        } catch (IOException e) {
            throw new ZookeeperClientException(e.getMessage(), e);
        }
    }

P
pengys5 已提交
60 61 62 63
    @Override public void shutdown() {

    }

64 65
    public void create(final String path, byte data[], List<ACL> acl,
        CreateMode createMode) throws ZookeeperClientException {
P
pengys5 已提交
66
        try {
67
            zk.create(path, data, acl, createMode);
P
pengys5 已提交
68 69 70 71 72
        } catch (KeeperException | InterruptedException e) {
            throw new ZookeeperClientException(e.getMessage(), e);
        }
    }

73
    public Stat exists(final String path, boolean watch) throws ZookeeperClientException {
P
pengys5 已提交
74
        try {
75
            return zk.exists(path, watch);
P
pengys5 已提交
76
        } catch (KeeperException | InterruptedException e) {
clevertension's avatar
clevertension 已提交
77 78 79 80 81 82 83 84
            throw new ZookeeperClientException(e.getMessage(), e);
        }
    }

    public void delete(final String path, int version) throws ZookeeperClientException {
        try {
            zk.delete(path, version);
        } catch (KeeperException | InterruptedException e) {
P
pengys5 已提交
85 86 87 88
            throw new ZookeeperClientException(e.getMessage(), e);
        }
    }

89
    public byte[] getData(String path, boolean watch, Stat stat) throws ZookeeperClientException {
P
pengys5 已提交
90
        try {
91
            return zk.getData(path, watch, stat);
P
pengys5 已提交
92 93 94 95 96
        } catch (KeeperException | InterruptedException e) {
            throw new ZookeeperClientException(e.getMessage(), e);
        }
    }

97
    public Stat setData(final String path, byte data[], int version) throws ZookeeperClientException {
P
pengys5 已提交
98
        try {
99
            return zk.setData(path, data, version);
P
pengys5 已提交
100 101 102 103
        } catch (KeeperException | InterruptedException e) {
            throw new ZookeeperClientException(e.getMessage(), e);
        }
    }
104 105 106 107 108 109 110 111

    public List<String> getChildren(final String path, boolean watch) throws ZookeeperClientException {
        try {
            return zk.getChildren(path, watch);
        } catch (KeeperException | InterruptedException e) {
            throw new ZookeeperClientException(e.getMessage(), e);
        }
    }
P
pengys5 已提交
112
}