Commit d102b4c5 authored by Yann Mombrun's avatar Yann Mombrun

Work with jmxconnector instead of already opened connection.

This will be the stable version (previous commit has not been deployed)

git-svn-id: svn://svn.forge.objectweb.org/svnroot/weblab/trunk/WebLabApplications/bundle@4598 a803c184-d8de-4a0e-a52b-d3eef88212b6
parent 1ba4abc3
......@@ -17,12 +17,14 @@
*/
package org.ow2.weblab.bundle.server;
import java.io.IOException;
import java.util.logging.Level;
import javax.management.MBeanServerConnection;
import javax.management.MBeanServerInvocationHandler;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
import javax.management.remote.JMXConnector;
import org.apache.activemq.broker.jmx.BrokerViewMBean;
import org.apache.activemq.broker.jmx.QueueViewMBean;
......@@ -61,7 +63,7 @@ public class ActiveMQServer extends AbstractApplicationServer {
@Override
public MBeanServerConnection getMBeanServerConnection() {
public JMXConnector getJMXConnector() {
return ProcessUtils.createJMXclient("localhost", this.getJmxPort(), "", "", "jmxrmi", 2000, false, this.logger);
}
......@@ -123,15 +125,19 @@ public class ActiveMQServer extends AbstractApplicationServer {
private void listQueuesStatus() {
final MBeanServerConnection connection = this.getMBeanServerConnection();
if (connection == null) {
this.logger.info("Unable to connect to " + this.getName() + " to retrieve list of queues.");
return;
}
for (final ObjectName queueName : this.listQueues(connection)) {
final QueueViewMBean queueMbean = MBeanServerInvocationHandler.newProxyInstance(connection, queueName, QueueViewMBean.class, true);
this.logger.info("\tQueue: " + queueMbean.getName() + " has currently " + queueMbean.getQueueSize() + " enqueued message(s). Since it's creation, a total of "
+ queueMbean.getEnqueueCount() + " message(s) have been enqueued.");
try (final JMXConnector connector = this.getJMXConnector()) {
MBeanServerConnection connection = connector.getMBeanServerConnection();
if (connection == null) {
this.logger.info("Unable to connect to " + this.getName() + " to retrieve list of queues.");
return;
}
for (final ObjectName queueName : this.listQueues(connection)) {
final QueueViewMBean queueMbean = MBeanServerInvocationHandler.newProxyInstance(connection, queueName, QueueViewMBean.class, true);
this.logger.info("\tQueue: " + queueMbean.getName() + " has currently " + queueMbean.getQueueSize() + " enqueued message(s). Since it's creation, a total of "
+ queueMbean.getEnqueueCount() + " message(s) have been enqueued.");
}
} catch (final IOException ioe) {
this.logger.log(Level.WARNING, "Unable to list queues due to a connection issue.", ioe);
}
}
......@@ -141,27 +147,28 @@ public class ActiveMQServer extends AbstractApplicationServer {
* The name of the queue to purge
*/
public void purge(final String queue) {
final MBeanServerConnection connection = this.getMBeanServerConnection();
if (connection == null) {
this.logger.info("Unable to connect to " + this.getName() + " to retrieve list of queues.");
return;
}
boolean queueFound = false;
for (final ObjectName queueName : this.listQueues(connection)) {
final QueueViewMBean queueMbean = MBeanServerInvocationHandler.newProxyInstance(connection, queueName, QueueViewMBean.class, true);
if (queue.equals(queueMbean.getName())) {
queueFound = true;
if (queueMbean.getQueueSize() > 0) {
this.logger.info("Purging queue " + queue + ".");
try {
try (final JMXConnector connector = this.getJMXConnector()) {
MBeanServerConnection connection = connector.getMBeanServerConnection();
if (connection == null) {
this.logger.info("Unable to connect to " + this.getName() + " to retrieve list of queues.");
return;
}
for (final ObjectName queueName : this.listQueues(connection)) {
final QueueViewMBean queueMbean = MBeanServerInvocationHandler.newProxyInstance(connection, queueName, QueueViewMBean.class, true);
if (queue.equals(queueMbean.getName())) {
queueFound = true;
if (queueMbean.getQueueSize() > 0) {
this.logger.info("Purging queue " + queue + ".");
queueMbean.purge();
} catch (final Exception e) {
this.logger.log(Level.WARNING, "An error occurred purging queue " + queue + ".", e);
} else {
this.logger.info("Queue " + queue + " is empty. Nothing to purge.");
}
} else {
this.logger.info("Queue " + queue + " is empty. Nothing to purge.");
}
}
} catch (final Exception e) {
queueFound = true; // To prevent from another log message after
this.logger.log(Level.WARNING, "An error occurred purging queue " + queue + ".", e);
}
if (!queueFound) {
......
......@@ -17,7 +17,7 @@
*/
package org.ow2.weblab.bundle.server;
import javax.management.MBeanServerConnection;
import javax.management.remote.JMXConnector;
import org.ow2.weblab.bundle.utils.ProcessUtils;
......@@ -75,7 +75,7 @@ public abstract class Bus extends AbstractApplicationServer {
}
@Override
public MBeanServerConnection getMBeanServerConnection() {
public JMXConnector getJMXConnector() {
return null;
}
......
......@@ -22,7 +22,7 @@ import java.net.URISyntaxException;
import java.net.URL;
import java.util.logging.Level;
import javax.management.MBeanServerConnection;
import javax.management.remote.JMXConnector;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
......@@ -117,7 +117,7 @@ public class Fuseki extends WebLabServer {
@Override
public MBeanServerConnection getMBeanServerConnection() {
public JMXConnector getJMXConnector() {
return null;
}
......
......@@ -29,7 +29,7 @@ import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import javax.management.MBeanServerConnection;
import javax.management.remote.JMXConnector;
import javax.xml.bind.JAXBContext;
import javax.xml.transform.stream.StreamSource;
......@@ -245,7 +245,7 @@ public class Heritrix extends WebLabServer {
@Override
public MBeanServerConnection getMBeanServerConnection() {
public JMXConnector getJMXConnector() {
return null;
}
......
......@@ -169,7 +169,7 @@ public class Karaf extends Bus {
@Override
public MBeanServerConnection getMBeanServerConnection() {
public JMXConnector getJMXConnector() {
return ProcessUtils.createJMXclient(this.jmxHost, this.getJmxPort(), this.jmxUser, this.jmxPassword, this.jmxName, 2000, false, this.logger);
}
......
......@@ -31,7 +31,7 @@ import java.util.function.Predicate;
import java.util.logging.Level;
import java.util.stream.Stream;
import javax.management.MBeanServerConnection;
import javax.management.remote.JMXConnector;
import org.apache.http.client.HttpClient;
import org.apache.solr.client.solrj.impl.CloudSolrClient;
......@@ -127,7 +127,7 @@ public class SolrServer extends AbstractApplicationServer {
@Override
public MBeanServerConnection getMBeanServerConnection() {
public JMXConnector getJMXConnector() {
return null;
}
......
......@@ -26,9 +26,9 @@ import java.util.logging.Level;
import java.util.logging.Logger;
import javax.management.AttributeList;
import javax.management.MBeanServerConnection;
import javax.management.ObjectInstance;
import javax.management.ObjectName;
import javax.management.remote.JMXConnector;
import org.ow2.weblab.bundle.server.WebLabServer.State;
import org.ow2.weblab.bundle.utils.ProcessUtils;
......@@ -49,7 +49,7 @@ public class TomcatJMXClient {
/**
* MBean connection on Tomcat
*/
public MBeanServerConnection connection;
public JMXConnector connection;
/**
......@@ -91,18 +91,18 @@ public class TomcatJMXClient {
if (this.logger.isLoggable(Level.FINEST)) {
this.logger.finest("Querying all Mbeans on remote jmx server.");
}
final Set<ObjectInstance> objects = this.connection.queryMBeans(null, null);
final Set<ObjectInstance> objects = this.connection.getMBeanServerConnection().queryMBeans(null, null);
// list all beans
for (final ObjectInstance oi : objects) {
// select manager ones
if ("Manager".equals(oi.getObjectName().getKeyProperty("type"))) {
final Set<ObjectInstance> ois = this.connection.queryMBeans(oi.getObjectName(), null);
final Set<ObjectInstance> ois = this.connection.getMBeanServerConnection().queryMBeans(oi.getObjectName(), null);
// check state for each context
for (final ObjectInstance o : ois) {
final String context = o.getObjectName().getKeyProperty("context");
try {
final Object status = this.connection.getAttribute(o.getObjectName(), "stateName");
final Object status = this.connection.getMBeanServerConnection().getAttribute(o.getObjectName(), "stateName");
webapps.put(context, status.toString());
if (this.logger.isLoggable(Level.FINEST)) {
this.logger.finest("Context " + context + " is " + status);
......@@ -164,7 +164,7 @@ public class TomcatJMXClient {
State status = State.UNDEFINED;
try {
status = State.valueOf(this.connection.getAttribute(ObjectName.getInstance("Catalina:type=Server"), "stateName").toString());
status = State.valueOf(this.connection.getMBeanServerConnection().getAttribute(ObjectName.getInstance("Catalina:type=Server"), "stateName").toString());
} catch (final Exception e) {
if (this.logger.isLoggable(Level.FINEST)) {
this.logger.log(Level.FINEST, e.getLocalizedMessage(), e);
......@@ -180,7 +180,7 @@ public class TomcatJMXClient {
*/
public void showStatistics() {
try {
final Set<ObjectInstance> set = this.connection.queryMBeans(new ObjectName("org.apache.cxf:*"), null);
final Set<ObjectInstance> set = this.connection.getMBeanServerConnection().queryMBeans(new ObjectName("org.apache.cxf:*"), null);
final String[] attributes = new String[] { "AvgResponseTime", "MaxResponseTime", "MinResponseTime", "NumInvocations", "NumLogicalRuntimeFaults", "NumRuntimeFaults",
"NumCheckedApplicationFaults", "NumUnCheckedApplicationFaults" };
this.logger
......@@ -189,7 +189,7 @@ public class TomcatJMXClient {
final String operation = oi.getObjectName().getKeyProperty("operation");
if ("org.apache.cxf.management.counters.ResponseTimeCounter".equals(oi.getClassName()) && (operation != null)) {
String line = oi.getObjectName().getKeyProperty("port") + "," + operation;
final AttributeList atl = this.connection.getAttributes(oi.getObjectName(), attributes);
final AttributeList atl = this.connection.getMBeanServerConnection().getAttributes(oi.getObjectName(), attributes);
for (int i = 0; i < attributes.length; i++) {
String value = atl.get(i).toString().trim();
value = value.split("=")[1].trim();
......@@ -220,7 +220,7 @@ public class TomcatJMXClient {
try {
final ObjectName on = ObjectName.getInstance("Catalina:j2eeType=WebModule,name=//localhost/" + webapp + ",J2EEApplication=none,J2EEServer=none");
final Object value = this.connection.invoke(on, command, new Object[0], new String[] {});
final Object value = this.connection.getMBeanServerConnection().invoke(on, command, new Object[0], new String[] {});
if (value != null) {
if (value.getClass().isArray()) {
result = Utils.arrayToString(value);
......@@ -238,5 +238,12 @@ public class TomcatJMXClient {
return result;
}
@Override
protected void finalize() throws Throwable {
this.connection.close();
super.finalize();
}
}
......@@ -22,7 +22,7 @@ import java.io.IOException;
import java.util.Map;
import java.util.logging.Level;
import javax.management.MBeanServerConnection;
import javax.management.remote.JMXConnector;
import org.ow2.weblab.bundle.utils.PathUtils;
import org.ow2.weblab.bundle.utils.ProcessUtils;
......@@ -230,7 +230,7 @@ public class TomcatServer extends WebLabServer {
}
@Override
public MBeanServerConnection getMBeanServerConnection() {
public JMXConnector getJMXConnector() {
return ProcessUtils.createJMXclient("localhost",getJmxPort(),"","","jmxrmi", 2000, false, this.logger);
}
......
......@@ -25,6 +25,7 @@ import java.util.logging.Level;
import java.util.logging.Logger;
import javax.management.MBeanServerConnection;
import javax.management.remote.JMXConnector;
import org.ow2.weblab.bundle.Launcher;
import org.ow2.weblab.bundle.utils.PathUtils;
......@@ -291,9 +292,9 @@ public abstract class WebLabServer {
*/
public int getPid() {
int pid = -1;
if (this.isEnabled() && this.getMBeanServerConnection() != null) {
try {
final MBeanServerConnection connection = this.getMBeanServerConnection();
if (this.isEnabled() && (this.getJMXConnector() != null)) {
try (JMXConnector connector = this.getJMXConnector()) {
final MBeanServerConnection connection = connector.getMBeanServerConnection();
final RuntimeMXBean bean = ManagementFactory.newPlatformMXBeanProxy(connection, ManagementFactory.RUNTIME_MXBEAN_NAME, RuntimeMXBean.class);
final String name_l = bean.getName();
final String[] ids = name_l.split("@");
......@@ -315,8 +316,8 @@ public abstract class WebLabServer {
public MemoryMXBean monitorMemory() {
MemoryMXBean memory = null;
if (this.isEnabled()) {
try {
final MBeanServerConnection connection = this.getMBeanServerConnection();
try (JMXConnector connector = this.getJMXConnector()) {
final MBeanServerConnection connection = connector.getMBeanServerConnection();
if (connection != null) {
memory = ManagementFactory.newPlatformMXBeanProxy(connection, ManagementFactory.MEMORY_MXBEAN_NAME, MemoryMXBean.class);
} else {
......@@ -359,11 +360,11 @@ public abstract class WebLabServer {
/**
* Retrieve MBeansServerConnection for java this server
* Retrieve JMXConnector for java this server
*
* @return MBeansServerConnection for java this server
* @return getJMXConnector for java this server
*/
public abstract MBeanServerConnection getMBeanServerConnection();
public abstract JMXConnector getJMXConnector();
public String supportedCommands() {
......@@ -383,7 +384,7 @@ public abstract class WebLabServer {
* @param cleanablePathsAfterStop
* the cleanablePathsAfterStop to set
*/
public void setCleanablePathsAfterStop(List<String> cleanablePathsAfterStop) {
public void setCleanablePathsAfterStop(final List<String> cleanablePathsAfterStop) {
this.cleanablePathsAfterStop = cleanablePathsAfterStop;
}
......
......@@ -17,7 +17,7 @@
*/
package org.ow2.weblab.bundle.server;
import javax.management.MBeanServerConnection;
import javax.management.remote.JMXConnector;
import org.ow2.weblab.bundle.utils.PathUtils;
import org.ow2.weblab.bundle.utils.Utils;
......@@ -59,7 +59,7 @@ public class ZooKeeperServer extends AbstractApplicationServer {
@Override
public MBeanServerConnection getMBeanServerConnection() {
public JMXConnector getJMXConnector() {
return null;
}
......
......@@ -34,7 +34,6 @@ import java.util.Map.Entry;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.management.MBeanServerConnection;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;
......@@ -605,21 +604,20 @@ public class ProcessUtils {
* The logger used inside
* @return a MBeanServerConnection
*/
public static MBeanServerConnection createJMXclient(final String host, final int port, final String login, final String password, final String path, final int timeout, final boolean log,
public static JMXConnector createJMXclient(final String host, final int port, final String login, final String password, final String path, final int timeout, final boolean log,
final Logger logger) {
return ProcessUtils.tryToExecuteDuring(logger, timeout, new Executor<MBeanServerConnection>(log) {
return ProcessUtils.tryToExecuteDuring(logger, timeout, new Executor<JMXConnector>(log) {
@Override
public MBeanServerConnection execute() throws Exception {
public JMXConnector execute() throws Exception {
final JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://" + host + ":" + port + "/" + path);
final Hashtable<String, Object> env = new Hashtable<>();
env.put("java.naming.factory.initial", "com.sun.jndi.rmi.registry.RegistryContextFactory");
if ((password != null) && !password.equals("")) {
env.put(JMXConnector.CREDENTIALS, new String[] { login, password });
}
final JMXConnector jmxc = JMXConnectorFactory.connect(url, env);
return jmxc.getMBeanServerConnection();
return JMXConnectorFactory.connect(url, env);
}
});
}
......@@ -636,7 +634,7 @@ public class ProcessUtils {
* The logger used inside
* @return a MbeanServerConnection from a WebLabServer
*/
public static MBeanServerConnection createJMXclient(final WebLabServer server, final boolean log, final Logger logger) {
public static JMXConnector createJMXclient(final WebLabServer server, final boolean log, final Logger logger) {
return ProcessUtils.createJMXclient("localhost", server.getJmxPort(), "", "", "jmxrmi", server.getTimeout(), log, logger);
}
......
/**
* WEBLAB: Service oriented integration platform for media mining and intelligence applications
*
* Copyright (C) 2004 - 2016 Airbus Defence and Space
* Copyright (C) 2004 - 2017 Airbus Defence and Space
*
* This library is free software; you can redistribute it and/or modify it under the terms of
* the GNU Lesser General Public License as published by the Free Software Foundation; either
......@@ -17,65 +17,77 @@
*/
package org.ow2.weblab.bundle.server;
import javax.management.MBeanServerConnection;
import javax.management.remote.JMXConnector;
public class DummyBus extends Bus {
public class DummyBus extends Bus{
@Override
public Process execute(String command) {
return null;
}
@Override
public String getProcessIdentificationClue() {
return null;
}
@Override
public Process start() {
return null;
}
@Override
public void stop() {
// Nothing
}
@Override
public boolean isServerFullyStarted(boolean showDetails) {
return false;
}
@Override
public MBeanServerConnection getMBeanServerConnection() {
public JMXConnector getJMXConnector() {
return null;
}
@Override
public boolean startChain(String chain) {
return false;
}
@Override
public boolean stopChain(String chain) {
return false;
}
@Override
public void listChains() {
// Nothing done
}
@Override
public String statusChain(String chain) {
return null;
}
@Override
protected String[] getStopScript() {
return null;
}
@Override
protected String[] getStartScript() {
return null;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment