Example usage for org.springframework.jmx.support MBeanServerConnectionFactoryBean MBeanServerConnectionFactoryBean

List of usage examples for org.springframework.jmx.support MBeanServerConnectionFactoryBean MBeanServerConnectionFactoryBean

Introduction

In this page you can find the example usage for org.springframework.jmx.support MBeanServerConnectionFactoryBean MBeanServerConnectionFactoryBean.

Prototype

MBeanServerConnectionFactoryBean

Source Link

Usage

From source file:org.xmatthew.spy2servers.component.spy.jmx.JmxSpySupportComponent.java

public void startup() {

    bean = new MBeanServerConnectionFactoryBean();
    try {/*from w  ww. java2s  .  c om*/
        bean.setServiceUrl(getServerUrl());
    } catch (MalformedURLException e) {
        throw new RuntimeException(e.getMessage(), e);
    }

    runing = true;
    setStatusRun();
    startJmxConnection();

    ObjectInstance objInstance;
    while (runing) {
        try {
            // begin to get MBeans
            Set mbeans = mbsc.queryMBeans(null, null);
            if (mbeans != null && mbeans.size() > 0) {
                Iterator iter = mbeans.iterator();
                while (iter.hasNext()) {
                    objInstance = (ObjectInstance) iter.next();
                    if (!getNameFilterQueryExp().apply(objInstance.getObjectName())) {
                        continue;
                    }
                    inspectMBean(objInstance, mbsc);
                }
            }

            //every repeat will call back MBeanServerConnection
            mscOnInterval(mbsc);

        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
            if (e instanceof IOException) {
                try {
                    bean.destroy();
                } catch (Exception e1) {
                    LOGGER.error(e1.getMessage(), e1);
                } finally {
                    bean = new MBeanServerConnectionFactoryBean();
                    try {
                        bean.setServiceUrl(getServerUrl());
                    } catch (MalformedURLException e1) {
                    }
                    isConnectionEstablished = false;
                    reStartJmxConnection();
                }
            }
        }

        try {
            Thread.sleep(detectInterval);
        } catch (InterruptedException e) {
            LOGGER.error(e.getMessage(), e);
        }
    }
}

From source file:com.haulmont.cuba.web.jmx.JmxConnectionHelper.java

protected static <T> T withConnection(JmxInstance instance, JmxAction<T> action) {
    try {//from   w w  w .  j  a  v a2 s .c  o  m
        if (Objects.equals(instance.getId(), LOCAL_JMX_INSTANCE_ID)) {
            return action.perform(instance, getLocalConnection());
        } else {
            MBeanServerConnectionFactoryBean factoryBean = new MBeanServerConnectionFactoryBean();
            String address = instance.getAddress();
            if (!address.startsWith("service:")) {
                address = "service:jmx:rmi:///jndi/rmi://" + address + "/jmxrmi";
            }
            factoryBean.setServiceUrl(address);

            String username = instance.getLogin();
            if (StringUtils.isNotEmpty(username)) {
                Properties properties = new Properties();
                properties.put("jmx.remote.credentials", new String[] { username, instance.getPassword() });
                factoryBean.setEnvironment(properties);
            }

            factoryBean.afterPropertiesSet();

            MBeanServerConnection connection = factoryBean.getObject();
            T result;
            try {
                result = action.perform(instance, connection);
            } finally {
                try {
                    factoryBean.destroy();
                } catch (Exception ignored) {
                }
            }
            return result;
        }
    } catch (Exception e) {
        throw new JmxControlException(e);
    }
}

From source file:com.newlandframework.rpc.jmx.ThreadPoolMonitorProvider.java

public static void monitor(ThreadPoolStatus status) throws IOException, MalformedObjectNameException,
        ReflectionException, MBeanException, InstanceNotFoundException {
    MBeanServerConnectionFactoryBean mBeanServerConnectionFactoryBean = new MBeanServerConnectionFactoryBean();
    mBeanServerConnectionFactoryBean.setServiceUrl(url);
    mBeanServerConnectionFactoryBean.afterPropertiesSet();
    MBeanServerConnection connection = mBeanServerConnectionFactoryBean.getObject();
    ObjectName objectName = new ObjectName(
            "com.newlandframework.rpc.jmx:name=threadPoolStatus,type=ThreadPoolStatus");

    connection.invoke(objectName, jmxPoolSizeMethod, new Object[] { status.getPoolSize() },
            new String[] { int.class.getName() });
    connection.invoke(objectName, jmxActiveCountMethod, new Object[] { status.getActiveCount() },
            new String[] { int.class.getName() });
    connection.invoke(objectName, jmxCorePoolSizeMethod, new Object[] { status.getCorePoolSize() },
            new String[] { int.class.getName() });
    connection.invoke(objectName, jmxMaximumPoolSizeMethod, new Object[] { status.getMaximumPoolSize() },
            new String[] { int.class.getName() });
    connection.invoke(objectName, jmxLargestPoolSizeMethod, new Object[] { status.getLargestPoolSize() },
            new String[] { int.class.getName() });
    connection.invoke(objectName, jmxTaskCountMethod, new Object[] { status.getTaskCount() },
            new String[] { long.class.getName() });
    connection.invoke(objectName, jmxCompletedTaskCountMethod, new Object[] { status.getCompletedTaskCount() },
            new String[] { long.class.getName() });
}

From source file:com.dianping.cache.service.impl.ServiceMonitorServiceImpl.java

@SuppressWarnings({ "rawtypes", "unchecked" })
private MBeanServerConnFactoryExtend createMBeanConnFactory(String clientIp) throws IOException {
    MBeanServerConnectionFactoryBean factory = new MBeanServerConnectionFactoryBean();
    String jmxHost = clientIp.contains(":") ? clientIp : clientIp + ":3397";
    factory.setServiceUrl("service:jmx:rmi:///jndi/rmi://" + jmxHost + "/HawkMBeanServer");
    factory.setConnectOnStartup(false);/*  www .j a  va 2s  .c  om*/
    Map environment = new HashMap();
    environment.put("jmx.remote.credentials", new String[] { this.jmxServerUser, this.jmxServerPasswd });
    factory.setEnvironmentMap(environment);
    factory.afterPropertiesSet();
    return new MBeanServerConnFactoryExtend(factory);
}

From source file:org.springframework.batch.sample.launch.RemoteLauncherTests.java

private static boolean isConnected() throws Exception {
    boolean connected = false;

    if (!JobRegistryBackgroundJobRunner.getErrors().isEmpty()) {
        throw JobRegistryBackgroundJobRunner.getErrors().get(0);
    }/* w w  w .  j a  v  a  2  s.  c  o  m*/

    if (launcher == null) {
        MBeanServerConnectionFactoryBean connectionFactory = new MBeanServerConnectionFactoryBean();

        try {
            launcher = (JobOperator) getMBean(connectionFactory, "spring:service=batch,bean=jobOperator",
                    JobOperator.class);
            loader = (JobLoader) getMBean(connectionFactory, "spring:service=batch,bean=jobLoader",
                    JobLoader.class);
        } catch (MBeanServerNotFoundException e) {
            return false;
        }
    }

    try {
        launcher.getJobNames();
        connected = loader.getConfigurations().size() > 0;
        logger.info("Configurations loaded: " + loader.getConfigurations());
    } catch (InvalidInvocationException e) {
        // ignore
    }
    return connected;
}