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

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

Introduction

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

Prototype

@Override
public void destroy() throws IOException 

Source Link

Document

Closes the underlying JMXConnector .

Usage

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

protected static <T> T withConnection(JmxInstance instance, JmxAction<T> action) {
    try {//w  w  w .j ava2s . 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);
    }
}