use of org.springframework.jmx.JmxException in project spring-framework by spring-projects.
the class MBeanClientInterceptorTests method testTestLazyConnectionToRemote.
@Test
public void testTestLazyConnectionToRemote() throws Exception {
assumeTrue(runTests);
Assume.group(TestGroup.JMXMP);
final int port = SocketUtils.findAvailableTcpPort();
JMXServiceURL url = new JMXServiceURL("service:jmx:jmxmp://localhost:" + port);
JMXConnectorServer connector = JMXConnectorServerFactory.newJMXConnectorServer(url, null, getServer());
MBeanProxyFactoryBean factory = new MBeanProxyFactoryBean();
factory.setServiceUrl(url.toString());
factory.setProxyInterface(IJmxTestBean.class);
factory.setObjectName(OBJECT_NAME);
factory.setConnectOnStartup(false);
factory.setRefreshOnConnectFailure(true);
// should skip connection to the server
factory.afterPropertiesSet();
IJmxTestBean bean = (IJmxTestBean) factory.getObject();
// now start the connector
try {
connector.start();
} catch (BindException ex) {
System.out.println("Skipping remainder of JMX LazyConnectionToRemote test because binding to local port [" + port + "] failed: " + ex.getMessage());
return;
}
// should now be able to access data via the lazy proxy
try {
assertEquals("Rob Harrop", bean.getName());
assertEquals(100, bean.getAge());
} finally {
connector.stop();
}
try {
bean.getName();
} catch (JmxException ex) {
// expected
}
connector = JMXConnectorServerFactory.newJMXConnectorServer(url, null, getServer());
connector.start();
// should now be able to access data via the lazy proxy
try {
assertEquals("Rob Harrop", bean.getName());
assertEquals(100, bean.getAge());
} finally {
connector.stop();
}
}
use of org.springframework.jmx.JmxException in project spring-framework by spring-projects.
the class ConnectorServerFactoryBean method afterPropertiesSet.
/**
* Start the connector server. If the {@code threaded} flag is set to {@code true},
* the {@code JMXConnectorServer} will be started in a separate thread.
* If the {@code daemon} flag is set to {@code true}, that thread will be
* started as a daemon thread.
* @throws JMException if a problem occurred when registering the connector server
* with the {@code MBeanServer}
* @throws IOException if there is a problem starting the connector server
*/
@Override
public void afterPropertiesSet() throws JMException, IOException {
if (this.server == null) {
this.server = JmxUtils.locateMBeanServer();
}
// Create the JMX service URL.
JMXServiceURL url = new JMXServiceURL(this.serviceUrl);
// Create the connector server now.
this.connectorServer = JMXConnectorServerFactory.newJMXConnectorServer(url, this.environment, this.server);
// Set the given MBeanServerForwarder, if any.
if (this.forwarder != null) {
this.connectorServer.setMBeanServerForwarder(this.forwarder);
}
// Do we want to register the connector with the MBean server?
if (this.objectName != null) {
doRegister(this.connectorServer, this.objectName);
}
try {
if (this.threaded) {
// Start the connector server asynchronously (in a separate thread).
Thread connectorThread = new Thread() {
@Override
public void run() {
try {
connectorServer.start();
} catch (IOException ex) {
throw new JmxException("Could not start JMX connector server after delay", ex);
}
}
};
connectorThread.setName("JMX Connector Thread [" + this.serviceUrl + "]");
connectorThread.setDaemon(this.daemon);
connectorThread.start();
} else {
// Start the connector server in the same thread.
this.connectorServer.start();
}
if (logger.isInfoEnabled()) {
logger.info("JMX connector server started: " + this.connectorServer);
}
} catch (IOException ex) {
// Unregister the connector server if startup failed.
unregisterBeans();
throw ex;
}
}
Aggregations