Search in sources :

Example 1 with QueryExp

use of javax.management.QueryExp in project camel by apache.

the class ManagedRoute method reset.

public void reset(boolean includeProcessors) throws Exception {
    reset();
    // and now reset all processors for this route
    if (includeProcessors) {
        MBeanServer server = getContext().getManagementStrategy().getManagementAgent().getMBeanServer();
        if (server != null) {
            // get all the processor mbeans and sort them accordingly to their index
            String prefix = getContext().getManagementStrategy().getManagementAgent().getIncludeHostName() ? "*/" : "";
            ObjectName query = ObjectName.getInstance(jmxDomain + ":context=" + prefix + getContext().getManagementName() + ",type=processors,*");
            QueryExp queryExp = Query.match(new AttributeValueExp("RouteId"), new StringValueExp(getRouteId()));
            Set<ObjectName> names = server.queryNames(query, queryExp);
            for (ObjectName name : names) {
                server.invoke(name, "reset", null, null);
            }
        }
    }
}
Also used : QueryExp(javax.management.QueryExp) StringValueExp(javax.management.StringValueExp) AttributeValueExp(javax.management.AttributeValueExp) MBeanServer(javax.management.MBeanServer) ObjectName(javax.management.ObjectName)

Example 2 with QueryExp

use of javax.management.QueryExp in project jdk8u_jdk by JetBrains.

the class SupportedQueryTypesTest method run.

public void run(Map<String, Object> args) {
    int errorCount = 0;
    ObjectName on = null;
    ObjectName serverDelegateObjectName = null;
    JMXConnectorServer cs = null;
    JMXConnector cc = null;
    System.out.println("SupportedQueryTypesTest::run: Start");
    try {
        // JMX MbeanServer used inside single VM as if remote.
        MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
        JMXServiceURL url = new JMXServiceURL("rmi", null, 0);
        cs = JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbs);
        cs.start();
        JMXServiceURL addr = cs.getAddress();
        cc = JMXConnectorFactory.connect(addr);
        mbsc = cc.getMBeanServerConnection();
        // Create and register the ServerDelegate MBean on the remote MBeanServer
        String serverDelegateClassName = ServerDelegate.class.getName();
        serverDelegateObjectName = new ObjectName("defaultDomain:class=" + serverDelegateClassName);
        mbsc.createMBean(serverDelegateClassName, serverDelegateObjectName);
        // Retrieve the MBean class name
        mbeanClassName = (String) args.get("-mbeanClassName");
        on = new ObjectName("defaultDomain:class=" + mbeanClassName);
        // Create and register the MBean on the remote MBeanServer
        System.out.println("SupportedQueryTypesTest::run: CREATE " + mbeanClassName + " on the remote MBeanServer with name " + on);
        mbsc.createMBean(mbeanClassName, on);
        // Create a QueryFactory and setup which query we'll use.
        QueryFactory queries = new QueryFactory(mbeanClassName);
        queries.buildQueries();
        int maxIndex = queries.getSize();
        int minIndex = 1;
        // Create a reference Set<ObjectName> to check later on
        // the queryNames() results
        Set<ObjectName> referenceNameSet = new HashSet<ObjectName>();
        referenceNameSet.add(on);
        // Create a reference Set<ObjectInstance> to check later on
        // the queryMBeans() results
        ObjectInstance oi = new ObjectInstance(on, mbeanClassName);
        Set<ObjectInstance> referenceInstanceSet = new HashSet<ObjectInstance>();
        referenceInstanceSet.add(oi);
        // Perform the queryNames and queryMBeans requests
        for (int i = minIndex; i <= maxIndex; i++) {
            QueryExp query = queries.getQuery(i);
            System.out.println("----");
            System.out.println("SupportedQueryTypesTest::run: Query # " + i);
            System.out.println("query " + query);
            errorCount += doQueryNames(query, referenceNameSet);
            errorCount += doQueryMBeans(query, referenceInstanceSet);
        }
    } catch (Exception e) {
        Utils.printThrowable(e, true);
        errorCount++;
    } finally {
        // Do unregister the MBean
        try {
            if (mbsc.isRegistered(on)) {
                mbsc.unregisterMBean(on);
            }
            if (mbsc.isRegistered(serverDelegateObjectName)) {
                mbsc.unregisterMBean(serverDelegateObjectName);
            }
        } catch (Exception e) {
            Utils.printThrowable(e, true);
            errorCount++;
        }
        try {
            // Close JMX Connector Client
            cc.close();
            // Stop connertor server
            cs.stop();
        } catch (Exception e) {
            Utils.printThrowable(e, true);
            errorCount++;
        }
    }
    System.out.println("");
    System.out.println("SupportedQueryTypesTest::run: Done");
    // Handle result
    if (errorCount == 0) {
        System.out.println("SupportedQueryTypesTest::run: (OK)");
    } else {
        String message = "SupportedQueryTypesTest::run: (ERROR) Got " + +errorCount + " error(s)";
        System.out.println(message);
        throw new RuntimeException(message);
    }
}
Also used : JMXServiceURL(javax.management.remote.JMXServiceURL) QueryExp(javax.management.QueryExp) ObjectInstance(javax.management.ObjectInstance) ObjectName(javax.management.ObjectName) JMXConnectorServer(javax.management.remote.JMXConnectorServer) JMXConnector(javax.management.remote.JMXConnector) MBeanServer(javax.management.MBeanServer) HashSet(java.util.HashSet)

Example 3 with QueryExp

use of javax.management.QueryExp in project jdk8u_jdk by JetBrains.

the class QueryMatchTest method query.

private static int query(MBeanServer mbs, String pattern, String[][] data) throws Exception {
    int error = 0;
    System.out.println("\nAttribute Value Pattern = " + pattern + "\n");
    for (int i = 0; i < data.length; i++) {
        ObjectName on = new ObjectName("domain:type=Simple,pattern=" + ObjectName.quote(pattern) + ",name=" + i);
        Simple s = new Simple(data[i][0]);
        mbs.registerMBean(s, on);
        QueryExp q = Query.match(Query.attr("StringNumber"), Query.value(pattern));
        q.setMBeanServer(mbs);
        boolean r = q.apply(on);
        System.out.print("Attribute Value = " + mbs.getAttribute(on, "StringNumber"));
        if (r && "OK".equals(data[i][1])) {
            System.out.println(" OK");
        } else if (!r && "KO".equals(data[i][1])) {
            System.out.println(" KO");
        } else {
            System.out.println(" Error");
            error++;
        }
    }
    return error;
}
Also used : QueryExp(javax.management.QueryExp) ObjectName(javax.management.ObjectName)

Example 4 with QueryExp

use of javax.management.QueryExp in project jdk8u_jdk by JetBrains.

the class QuerySubstringTest method query.

private static int query(MBeanServer mbs, int type, String substring, String[][] data) throws Exception {
    int error = 0;
    String querySubString = null;
    switch(type) {
        case 1:
            querySubString = "InitialSubString";
            break;
        case 2:
            querySubString = "AnySubString";
            break;
        case 3:
            querySubString = "FinalSubString";
            break;
    }
    System.out.println("\n" + querySubString + " = " + substring + "\n");
    for (int i = 0; i < data.length; i++) {
        ObjectName on = new ObjectName("test:type=Simple,query=" + querySubString + ",name=" + i);
        Simple s = new Simple(data[i][0]);
        mbs.registerMBean(s, on);
        QueryExp q = null;
        switch(type) {
            case 1:
                q = Query.initialSubString(Query.attr("String"), Query.value(substring));
                break;
            case 2:
                q = Query.anySubString(Query.attr("String"), Query.value(substring));
                break;
            case 3:
                q = Query.finalSubString(Query.attr("String"), Query.value(substring));
                break;
        }
        q.setMBeanServer(mbs);
        boolean r = q.apply(on);
        System.out.print("Attribute Value = " + mbs.getAttribute(on, "String"));
        if (r && "OK".equals(data[i][type])) {
            System.out.println(" OK");
        } else if (!r && "KO".equals(data[i][type])) {
            System.out.println(" KO");
        } else {
            System.out.println(" Error");
            error++;
        }
    }
    return error;
}
Also used : QueryExp(javax.management.QueryExp) ObjectName(javax.management.ObjectName)

Example 5 with QueryExp

use of javax.management.QueryExp in project geode by apache.

the class MBeanProcessController method invokeOperationOnTargetMBean.

/**
   * Connects to the process and use its MBean to stop it.
   * 
   * @param namePattern the name pattern of the MBean to use for stopping
   * @param pidAttribute the name of the MBean attribute with the process id to compare against
   * @param methodName the name of the MBean operation to invoke
   * @param attributes the names of the MBean attributes to compare with expected values
   * @param values the expected values of the specified MBean attributes
   *
   * @throws ConnectionFailedException if there was a failure to connect to the local JMX connector
   *         in the process
   * @throws IOException if a communication problem occurred when talking to the MBean server
   * @throws MBeanInvocationFailedException if failed to invoke stop on the MBean for any reason
   */
private Object invokeOperationOnTargetMBean(final ObjectName namePattern, final String pidAttribute, final String methodName, final String[] attributes, final Object[] values) throws ConnectionFailedException, IOException, MBeanInvocationFailedException {
    ObjectName objectName = namePattern;
    connect();
    try {
        final QueryExp constraint = buildQueryExp(pidAttribute, attributes, values);
        final Set<ObjectName> mbeanNames = this.server.queryNames(namePattern, constraint);
        if (mbeanNames.isEmpty()) {
            throw new MBeanInvocationFailedException("Failed to find mbean matching '" + namePattern + "' with attribute '" + pidAttribute + "' of value '" + this.pid + "'");
        }
        if (mbeanNames.size() > 1) {
            throw new MBeanInvocationFailedException("Found more than one mbean matching '" + namePattern + "' with attribute '" + pidAttribute + "' of value '" + this.pid + "'");
        }
        objectName = mbeanNames.iterator().next();
        return invoke(objectName, methodName);
    } catch (InstanceNotFoundException e) {
        throw new MBeanInvocationFailedException("Failed to invoke " + methodName + " on " + objectName, e);
    } catch (MBeanException e) {
        throw new MBeanInvocationFailedException("Failed to invoke " + methodName + " on " + objectName, e);
    } catch (ReflectionException e) {
        throw new MBeanInvocationFailedException("Failed to invoke " + methodName + " on " + objectName, e);
    } finally {
        disconnect();
    }
}
Also used : ReflectionException(javax.management.ReflectionException) QueryExp(javax.management.QueryExp) InstanceNotFoundException(javax.management.InstanceNotFoundException) MBeanException(javax.management.MBeanException) ObjectName(javax.management.ObjectName)

Aggregations

QueryExp (javax.management.QueryExp)21 ObjectName (javax.management.ObjectName)18 MBeanServer (javax.management.MBeanServer)7 Test (org.junit.Test)5 AttributeValueExp (javax.management.AttributeValueExp)3 StringValueExp (javax.management.StringValueExp)3 UnitTest (org.apache.geode.test.junit.categories.UnitTest)3 InstanceNotFoundException (javax.management.InstanceNotFoundException)2 MBeanException (javax.management.MBeanException)2 MalformedObjectNameException (javax.management.MalformedObjectNameException)2 ObjectInstance (javax.management.ObjectInstance)2 ReflectionException (javax.management.ReflectionException)2 JMXConnector (javax.management.remote.JMXConnector)2 JMXServiceURL (javax.management.remote.JMXServiceURL)2 JmxReporter (com.codahale.metrics.JmxReporter)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 ObjectInputStream (java.io.ObjectInputStream)1 ObjectOutputStream (java.io.ObjectOutputStream)1 URI (java.net.URI)1