Example usage for org.apache.zookeeper.server ConnectionMXBean getEphemeralNodes

List of usage examples for org.apache.zookeeper.server ConnectionMXBean getEphemeralNodes

Introduction

In this page you can find the example usage for org.apache.zookeeper.server ConnectionMXBean getEphemeralNodes.

Prototype

String[] getEphemeralNodes();

Source Link

Usage

From source file:org.apache.twill.yarn.SessionExpireTestRun.java

License:Apache License

private boolean expireAppMasterZKSession(TwillController controller, long timeout, TimeUnit timeoutUnit) {
    MBeanServer mbeanServer = MBeanRegistry.getInstance().getPlatformMBeanServer();
    QueryExp query = Query.isInstanceOf(new StringValueExp(ConnectionMXBean.class.getName()));

    Stopwatch stopwatch = new Stopwatch();
    stopwatch.start();/* w  ww  . jav a2  s  . co m*/
    do {
        // Find the AM session and expire it
        Set<ObjectName> connectionBeans = mbeanServer.queryNames(ObjectName.WILDCARD, query);
        for (ObjectName objectName : connectionBeans) {

            ConnectionMXBean connectionBean = MBeanServerInvocationHandler.newProxyInstance(mbeanServer,
                    objectName, ConnectionMXBean.class, false);
            for (String node : connectionBean.getEphemeralNodes()) {
                if (node.endsWith("/instances/" + controller.getRunId().getId())) {
                    // This is the AM, expire the session.
                    LOG.info("Kill AM session {}", connectionBean.getSessionId());
                    connectionBean.terminateSession();
                    return true;
                }
            }
        }
        Uninterruptibles.sleepUninterruptibly(100, TimeUnit.MILLISECONDS);
    } while (stopwatch.elapsedTime(timeoutUnit) < timeout);

    return false;
}