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

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

Introduction

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

Prototype

String getSessionId();

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();// ww  w.j a  v  a2 s .c o 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;
}