Search code examples
javaspring-bootjmxactivemq-artemis

How do I connect to AMQ7 via JMX, programmatically to get broker metrics with MBean?


Our team just started testing AMQ7 and the application I implemented no longer connects to the broker since we migrated from AMQ6 to AMQ7. After reading over documentation I noticed the uri string is completely different then before where it doesn't need the service or rmi embedded in the uri to connect. Below is the code I'm using to connect but causing the errors.

java.net.MalformedURLException: Service URL must start with service:jmx:
      at java.management/javax.management.remote.JMXServiceURL.<init>(JMXServiceURL.java:169) ~[na:na]
      at com.ups.aaa.doss.MsgAudtingRunner.run(MsgAudtingRunner.java:110) ~[classes/:na]
      at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
      at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:na]
      at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
      at java.base/java.lang.reflect.Method.invoke(Method.java:567) ~[na:na]
      at org.springframework.scheduling.support.ScheduledMethodRunnable.run(ScheduledMethodRunnable.java:84) ~[spring-context-5.0.9.RELEASE.jar:5.0.9.RELEASE]
      at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54) ~[spring-context-5.0.9.RELEASE.jar:5.0.9.RELEASE]
      at org.springframework.scheduling.concurrent.ReschedulingRunnable.run(ReschedulingRunnable.java:93) ~[spring-context-5.0.9.RELEASE.jar:5.0.9.RELEASE]
      at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515) ~[na:na]
      at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264) ~[na:na]
      at java.base/java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:304) ~[na:na]
      at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128) ~[na:na]
      at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628) ~[na:na]
      at java.base/java.lang.Thread.run(Thread.java:830) ~[na:na]

This is the code which the error happens at:

JMXServiceURL url = new JMXServiceURL(brkerInfo.getValue());
JMXConnector connector = JMXConnectorFactory.connect(url, environment);

Is there another Class I need to reference instead of JMXServiceURL which I know expects the uri to have service as a prefix.


Solution

  • Check out the example that ships with the broker in examples/features/standard/jmx (also available in GitHub). It demonstrates how to connect to the broker programmatically using JMX. It does, in fact, use JMXServiceURL and JMXConnectorFactory and the URL does use "service" and "rmi", e.g.:

    service:jmx:rmi:///jndi/rmi://localhost:1099/jmxrmi