Example usage for org.apache.thrift.transport TTransport write

List of usage examples for org.apache.thrift.transport TTransport write

Introduction

In this page you can find the example usage for org.apache.thrift.transport TTransport write.

Prototype

public abstract void write(byte[] buf, int off, int len) throws TTransportException;

Source Link

Document

Writes up to len bytes from the buffer.

Usage

From source file:org.apache.accumulo.monitor.ZooKeeperStatus.java

License:Apache License

@Override
public void run() {

    while (!stop) {

        TreeSet<ZooKeeperState> update = new TreeSet<>();

        String zookeepers[] = SiteConfiguration.getInstance().get(Property.INSTANCE_ZK_HOST).split(",");
        for (String keeper : zookeepers) {
            int clients = 0;
            String mode = "unknown";

            String[] parts = keeper.split(":");
            TTransport transport = null;
            try {
                HostAndPort addr;//  w  w  w .ja v a 2  s . co m
                if (parts.length > 1)
                    addr = HostAndPort.fromParts(parts[0], Integer.parseInt(parts[1]));
                else
                    addr = HostAndPort.fromParts(parts[0], 2181);

                transport = TTimeoutTransport.create(addr, 10 * 1000l);
                transport.write("stat\n".getBytes(UTF_8), 0, 5);
                StringBuilder response = new StringBuilder();
                try {
                    transport.flush();
                    byte[] buffer = new byte[1024 * 100];
                    int n = 0;
                    while ((n = transport.read(buffer, 0, buffer.length)) > 0) {
                        response.append(new String(buffer, 0, n, UTF_8));
                    }
                } catch (TTransportException ex) {
                    // happens at EOF
                }
                for (String line : response.toString().split("\n")) {
                    if (line.startsWith(" "))
                        clients++;
                    if (line.startsWith("Mode"))
                        mode = line.split(":")[1];
                }
                update.add(new ZooKeeperState(keeper, mode, clients));
            } catch (Exception ex) {
                log.info("Exception talking to zookeeper " + keeper, ex);
                update.add(new ZooKeeperState(keeper, "Down", -1));
            } finally {
                if (transport != null) {
                    try {
                        transport.close();
                    } catch (Exception ex) {
                        log.error("Exception", ex);
                    }
                }
            }
        }
        status = update;
        UtilWaitThread.sleep(5 * 1000);
    }
}

From source file:org.apache.accumulo.server.monitor.ZooKeeperStatus.java

License:Apache License

@Override
public void run() {

    while (!stop) {

        TreeSet<ZooKeeperState> update = new TreeSet<ZooKeeperState>();

        String zookeepers[] = ServerConfiguration.getSiteConfiguration().get(Property.INSTANCE_ZK_HOST)
                .split(",");
        for (String keeper : zookeepers) {
            int clients = 0;
            String mode = "unknown";

            String[] parts = keeper.split(":");
            TTransport transport = null;
            try {
                HostAndPort addr;/*  w w  w.  j  a  va  2  s. c  o  m*/
                if (parts.length > 1)
                    addr = HostAndPort.fromParts(parts[0], Integer.parseInt(parts[1]));
                else
                    addr = HostAndPort.fromParts(parts[0], 2181);

                transport = TTimeoutTransport.create(addr, 10 * 1000l);
                transport.write("stat\n".getBytes(), 0, 5);
                StringBuilder response = new StringBuilder();
                try {
                    transport.flush();
                    byte[] buffer = new byte[1024 * 100];
                    int n = 0;
                    while ((n = transport.read(buffer, 0, buffer.length)) > 0) {
                        response.append(new String(buffer, 0, n));
                    }
                } catch (TTransportException ex) {
                    // happens at EOF
                }
                for (String line : response.toString().split("\n")) {
                    if (line.startsWith(" "))
                        clients++;
                    if (line.startsWith("Mode"))
                        mode = line.split(":")[1];
                }
                update.add(new ZooKeeperState(keeper, mode, clients));
            } catch (Exception ex) {
                log.info("Exception talking to zookeeper " + keeper, ex);
                update.add(new ZooKeeperState(keeper, "Down", -1));
            } finally {
                if (transport != null) {
                    try {
                        transport.close();
                    } catch (Exception ex) {
                        log.error(ex, ex);
                    }
                }
            }
        }
        status = update;
        UtilWaitThread.sleep(1000);
    }
}