ActiveMQ: Get list of connections through JMX?
Asked Answered
A

3

3

How do I get the list of the connections to the OpenWire connector of ActiveMQ? JConsole is able to list the connections, but I don't see which "view" I can use to get the list:

Example ObjectName of a connection: org.apache.activemq:BrokerName=localhost,Type=Connection,ConnectorName=openwire,Connection=toto

I tried "ConnectorViewMBean", but the operations on it don't allow me to list the connections:

ObjectName name = new ObjectName("org.apache.activemq:BrokerName=localhost,Type=Connection,ConnectorName=openwire"); 
mbsc.getMBeanInfo(name); 
ConnectorViewMBean view = JMX.newMBeanProxy(mbsc, name, ConnectorViewMBean.class);
Antimissile answered 15/9, 2011 at 13:36 Comment(0)
A
3

The solution was the usage of an expression:

ObjectName connectionNames = 
      new ObjectName("org.apache.activemq:BrokerName=localhost," + 
                     "Type=Connection,ConnectorName=openwire,Connection=*");

Set<ObjectName> names = mbsc.queryNames(connectionNames, null); 
for(ObjectName name : names) { 
   logger.error("Name: "+name.getCanonicalName()); 
} 
Antimissile answered 15/9, 2011 at 16:30 Comment(3)
Can anyone tell me how to format code correctly?? SHIFT + ENTER creates a new block code, ENTER too ...Antimissile
see stackoverflow.com/editing-help or use the toolbar buttons in the editorTrabue
This answer is correct for ActiveMQ 5.7.0 and before. The format of the ObjectName changed in ActiveMQ 5.8.0, as documented in the 5.8.0 release notes.Conflict
B
1

I'm using ActiveMQ 5.14.5, which uses a different ObjectName format for querying connections via JMX. The equivalent to Andrew's answer in this version of ActiveMQ is:

final JMXServiceURL url       = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://localhost:1099/jmxrmi");
final JMXConnector  connector = JMXConnectorFactory.connect(url, null);
connector.connect();

final ObjectName connectionName = new ObjectName(
    "org.apache.activemq:type=Broker," +
    "brokerName=localhost,"            +
    "connector=clientConnectors,"      +
    "connectorName=openwire,"          +
    "connectionViewType=clientId,"     +
    "connectionName=*"
);

final MBeanServerConnection mbsc  = connector.getMBeanServerConnection();
final Set<ObjectName>       names = mbsc.queryNames(connectionName, null);
for (final ObjectName name : names) {
    System.out.println(name.getCanonicalName());
}
Bare answered 1/7, 2020 at 19:46 Comment(0)
B
0

In the most recent versions of ActiveMQ (5.1x.x), you can use the BrokerViewMBean to get a map of transport connectors:

Map<String, String[]> env = new HashMap<>();
String[] creds = {brokerUsername, brokerPassword};
env.put(JMXConnector.CREDENTIALS, creds);

final String managementURL = "service:jmx:rmi:///jndi/rmi://localhost:1099/jmxrmi";
try (JMXConnector connector = JMXConnectorFactory.connect(new JMXServiceURL(managementURL, env)) {
   ObjectName on = new ObjectName("org.apache.activemq:type=Broker,brokerName=localhost");
   BrokerViewMBean broker = MBeanServerInvocationHandler.newProxyInstance(connector.getMBeanServerConnection(), on, BrokerViewMBean.class, false);
   Map<String, String> transportConnectors = broker.getTransportConnectors();
   // broker.getTransportConnectorsByType("tcp"); // openwire
   // broker.addConnector(String discoveryAddress);
   // broker.removeConnector(String connectorName);
 } catch (MalformedObjectNameException ex) {
    // log error
 } catch (IOException ex) {
    // log error
 } catch (Exception ex) {
    // log error
 }

Check also out ConnectorViewMBean.

However, while there are methods in BrokerViewMBean to get transport connectors, as demonstrated in the above code, there aren't any to get a list of network (a.k.a. broker-to-broker) connectors.

Bionomics answered 18/3, 2021 at 9:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.