Timothy Bish 2013-11-11 12:05:52 -05:00
parent 6552287221
commit b0b3a169ce
1 changed files with 100 additions and 122 deletions

View File

@ -45,8 +45,6 @@ import org.slf4j.LoggerFactory;
/** /**
* A {@link BrokerFacade} which uses a JMX-Connection to communicate with a * A {@link BrokerFacade} which uses a JMX-Connection to communicate with a
* broker * broker
*
*
*/ */
public class RemoteJMXBrokerFacade extends BrokerFacadeSupport { public class RemoteJMXBrokerFacade extends BrokerFacadeSupport {
@ -75,30 +73,21 @@ public class RemoteJMXBrokerFacade extends BrokerFacadeSupport {
closeConnection(); closeConnection();
} }
private ObjectName getBrokerObjectName(MBeanServerConnection connection) @Override
throws IOException, MalformedObjectNameException { public BrokerViewMBean getBrokerAdmin() throws Exception {
MBeanServerConnection connection = getMBeanServerConnection();
Set<ObjectName> brokers = findBrokers(connection); Set<ObjectName> brokers = findBrokers(connection);
if (brokers.size() == 0) { if (brokers.size() == 0) {
throw new IOException("No broker could be found in the JMX."); throw new IOException("No broker could be found in the JMX.");
} }
ObjectName name = brokers.iterator().next(); ObjectName name = brokers.iterator().next();
return name; BrokerViewMBean mbean = MBeanServerInvocationHandler.newProxyInstance(connection, name, BrokerViewMBean.class, true);
}
public BrokerViewMBean getBrokerAdmin() throws Exception {
MBeanServerConnection connection = getMBeanServerConnection();
Set brokers = findBrokers(connection);
if (brokers.size() == 0) {
throw new IOException("No broker could be found in the JMX.");
}
ObjectName name = (ObjectName)brokers.iterator().next();
BrokerViewMBean mbean = (BrokerViewMBean)MBeanServerInvocationHandler.newProxyInstance(connection, name, BrokerViewMBean.class, true);
return mbean; return mbean;
} }
public String getBrokerName() throws Exception, @Override
MalformedObjectNameException { public String getBrokerName() throws Exception, MalformedObjectNameException {
return getBrokerAdmin().getBrokerName(); return getBrokerAdmin().getBrokerName();
} }
@ -110,7 +99,6 @@ public class RemoteJMXBrokerFacade extends BrokerFacadeSupport {
synchronized (this) { synchronized (this) {
closeConnection(); closeConnection();
LOG.debug("Creating a new JMX-Connection to the broker"); LOG.debug("Creating a new JMX-Connection to the broker");
this.connector = createConnection(); this.connector = createConnection();
return this.connector.getMBeanServerConnection(); return this.connector.getMBeanServerConnection();
@ -135,9 +123,7 @@ public class RemoteJMXBrokerFacade extends BrokerFacadeSupport {
Map<String, Object> env = new HashMap<String, Object>(); Map<String, Object> env = new HashMap<String, Object>();
if (this.configuration.getJmxUser() != null) { if (this.configuration.getJmxUser() != null) {
env.put("jmx.remote.credentials", new String[] { env.put("jmx.remote.credentials", new String[] { this.configuration.getJmxUser(), this.configuration.getJmxPassword() });
this.configuration.getJmxUser(),
this.configuration.getJmxPassword() });
} }
Collection<JMXServiceURL> jmxUrls = this.configuration.getJmxUrls(); Collection<JMXServiceURL> jmxUrls = this.configuration.getJmxUrls();
@ -146,8 +132,7 @@ public class RemoteJMXBrokerFacade extends BrokerFacadeSupport {
try { try {
JMXConnector connector = JMXConnectorFactory.connect(url, env); JMXConnector connector = JMXConnectorFactory.connect(url, env);
connector.connect(); connector.connect();
MBeanServerConnection connection = connector MBeanServerConnection connection = connector.getMBeanServerConnection();
.getMBeanServerConnection();
Set<ObjectName> brokers = findBrokers(connection); Set<ObjectName> brokers = findBrokers(connection);
if (brokers.size() > 0) { if (brokers.size() > 0) {
@ -166,19 +151,16 @@ public class RemoteJMXBrokerFacade extends BrokerFacadeSupport {
throw new RuntimeException(exception); throw new RuntimeException(exception);
} }
} }
throw new IllegalStateException("No broker is found at any of the " throw new IllegalStateException("No broker is found at any of the " + jmxUrls.size() + " configured urls");
+ jmxUrls.size() + " configured urls");
} }
protected synchronized void closeConnection() { protected synchronized void closeConnection() {
if (connector != null) { if (connector != null) {
try { try {
LOG.debug("Closing a connection to a broker (" + connector.getConnectionId() + ")"); LOG.debug("Closing a connection to a broker (" + connector.getConnectionId() + ")");
connector.close(); connector.close();
} catch (IOException e) { } catch (IOException e) {
// Ignore the exception, since it most likly won't matter // Ignore the exception, since it most likly won't matter anymore
// anymore
} }
} }
} }
@ -193,39 +175,37 @@ public class RemoteJMXBrokerFacade extends BrokerFacadeSupport {
* @throws IOException * @throws IOException
* @throws MalformedObjectNameException * @throws MalformedObjectNameException
*/ */
@SuppressWarnings("unchecked") protected Set<ObjectName> findBrokers(MBeanServerConnection connection) throws IOException, MalformedObjectNameException {
protected Set<ObjectName> findBrokers(MBeanServerConnection connection)
throws IOException, MalformedObjectNameException {
ObjectName name; ObjectName name;
if (this.brokerName == null) { if (this.brokerName == null) {
name = new ObjectName("org.apache.activemq:type=Broker,brokerName=*"); name = new ObjectName("org.apache.activemq:type=Broker,brokerName=*");
} else { } else {
name = new ObjectName("org.apache.activemq:brokerName=" name = new ObjectName("org.apache.activemq:type=Broker,brokerName=" + this.brokerName);
+ this.brokerName + ",Type=broker");
} }
Set<ObjectName> brokers = connection.queryNames(name, null); Set<ObjectName> brokers = connection.queryNames(name, null);
Set<ObjectName> masterBrokers = new HashSet<ObjectName>(); Set<ObjectName> masterBrokers = new HashSet<ObjectName>();
for (ObjectName objectName : brokers) { for (ObjectName objectName : brokers) {
BrokerViewMBean mbean = (BrokerViewMBean)MBeanServerInvocationHandler.newProxyInstance(connection, objectName, BrokerViewMBean.class, true); BrokerViewMBean mbean = MBeanServerInvocationHandler.newProxyInstance(connection, objectName, BrokerViewMBean.class, true);
if (!mbean.isSlave()) masterBrokers.add(objectName); if (!mbean.isSlave())
masterBrokers.add(objectName);
} }
return masterBrokers; return masterBrokers;
} }
@Override
public void purgeQueue(ActiveMQDestination destination) throws Exception { public void purgeQueue(ActiveMQDestination destination) throws Exception {
QueueViewMBean queue = getQueue(destination.getPhysicalName()); QueueViewMBean queue = getQueue(destination.getPhysicalName());
queue.purge(); queue.purge();
} }
@Override
public ManagementContext getManagementContext() { public ManagementContext getManagementContext() {
throw new IllegalStateException("not supported"); throw new IllegalStateException("not supported");
} }
@Override
@SuppressWarnings("unchecked") protected <T> Collection<T> getManagedObjects(ObjectName[] names, Class<T> type) {
protected <T> Collection<T> getManagedObjects(ObjectName[] names,
Class<T> type) {
MBeanServerConnection connection; MBeanServerConnection connection;
try { try {
connection = getMBeanServerConnection(); connection = getMBeanServerConnection();
@ -237,8 +217,7 @@ public class RemoteJMXBrokerFacade extends BrokerFacadeSupport {
if (connection != null) { if (connection != null) {
for (int i = 0; i < names.length; i++) { for (int i = 0; i < names.length; i++) {
ObjectName name = names[i]; ObjectName name = names[i];
T value = (T) MBeanServerInvocationHandler.newProxyInstance( T value = MBeanServerInvocationHandler.newProxyInstance(connection, name, type, true);
connection, name, type, true);
if (value != null) { if (value != null) {
answer.add(value); answer.add(value);
} }
@ -253,8 +232,7 @@ public class RemoteJMXBrokerFacade extends BrokerFacadeSupport {
} }
@Override @Override
public Object newProxyInstance(ObjectName objectName, Class interfaceClass,boolean notificationBroadcaster) throws Exception { public Object newProxyInstance(ObjectName objectName, Class interfaceClass, boolean notificationBroadcaster) throws Exception {
return MBeanServerInvocationHandler.newProxyInstance(getMBeanServerConnection(), objectName, interfaceClass, notificationBroadcaster); return MBeanServerInvocationHandler.newProxyInstance(getMBeanServerConnection(), objectName, interfaceClass, notificationBroadcaster);
} }
} }