连接到远程 JMX 代理 ActiveMQ Artemis 时出错

huangapple go评论66阅读模式
英文:

Error connecting to remote JMX broker ActiveMQ Artemis

问题

I have code that connects to the ActiveMQ Artemis broker console via JMX. It connects to localhost, but it cannot connect to a remote host, although the settings are the same

Here is the code to connect:

public static QueueControl getQueueControl(String queue, MBeanServerConnection mBeanServerConnection) {
    QueueControl queueControl = null;
    Queue dynamicQueue = null;
    try {
        dynamicQueue = (Queue) initialContext.lookup("dynamicQueues/" + queue);
    } catch (NamingException e) {
        e.printStackTrace();
    }
    MBeanServerConnection mbsc = mBeanServerConnection;
    ObjectName on = null;
    try {
        on = ObjectNameBuilder.DEFAULT.getQueueObjectName(SimpleString.toSimpleString(dynamicQueue.getQueueName()), SimpleString.toSimpleString(dynamicQueue.getQueueName()), RoutingType.ANYCAST);
    } catch (Exception e) {
        e.printStackTrace();
    }
    queueControl = MBeanServerInvocationHandler.newProxyInstance(mbsc, on, QueueControl.class, false);
    try {
        queueLength = queueControl.browse().length;
    } catch (Exception e) {
        e.printStackTrace();
    }
    i = queueLength;
    return queueControl;
}

It seems to me that the error is in the field:

on = ObjectNameBuilder.DEFAULT.getQueueObjectName(SimpleString.toSimpleString(dynamicQueue.getQueueName()), SimpleString.toSimpleString(dynamicQueue.getQueueName()), RoutingType.ANYCAST);

Because the Default value is:

public static final ObjectNameBuilder DEFAULT = new ObjectNameBuilder(ActiveMQDefaultConfiguration.getDefaultJmxDomain(), "localhost", true);

Here is the error that occurs:

javax.management.InstanceNotFoundException: org.apache.activemq.artemis:broker="localhost",component=addresses,address="queueTest",subcomponent=queues,routing-type="anycast",queue="queueTest"
 at java.management/com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.getMBean(DefaultMBeanServerInterceptor.java:1083)
...

It seems to me that the error is in the Default variable. And if in it, then what should I replace it with so that I can connect to both the local broker and the remote one?

I did the method as you described, but now there is no connection either to localhost or to a remote broker:

public static QueueControl getQueueControl(String queue, MBeanServerConnection mBeanServerConnection) {
    QueueControl queueControl = null;
    try {

        Queue dynamicQueue = (Queue) initialContext.lookup("dynamicQueues/" + queue);
        MBeanServerConnection mbsc = mBeanServerConnection;
        String brokerName = mbsc.queryNames(
                ObjectName.getInstance("org.apache.activemq.artemis:broker=*"),
                null).stream().findFirst().get().getKeyProperty("broker");

        ObjectNameBuilder myONB = ObjectNameBuilder.create(
                ActiveMQDefaultConfiguration.getDefaultJmxDomain(), brokerName, true);

        ObjectName on = myONB.getQueueObjectName(SimpleString.toSimpleString(dynamicQueue.getQueueName()), SimpleString.toSimpleString(dynamicQueue.getQueueName()), RoutingType.ANYCAST);
        queueControl = MBeanServerInvocationHandler.newProxyInstance(mbsc, on, QueueControl.class, false);
        queueLength = queueControl.browse().length;
        i = queueLength;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return queueControl;
}

Error stacktrace:

javax.management.InstanceNotFoundException: org.apache.activemq.artemis:broker="localhost",component=addresses,address="exampleQueue",subcomponent=queues,routing-type="anycast",queue="exampleQueue"
...

The error appears in this field:

queueLength = queueControl.browse().length;

And yes, I have tried to create an ObjectNameBuilder as you described, but it shows an error when creating:

'ObjectNameBuilder(java.lang.String, java.lang.String, boolean)' has private access in 'org.apache.activemq.artemis.api.core.management.ObjectNameBuilder'
英文:

I have code that connects to the ActiveMQ Artemis broker console via JMX. It connects to localhost, but it cannot connect to a remote host, although the settings are the same

Here is the code to connect:

public static QueueControl getQueueControl(String queue, MBeanServerConnection mBeanServerConnection) {
        QueueControl queueControl = null;
        Queue dynamicQueue = null;
        try {
            dynamicQueue = (Queue) initialContext.lookup("dynamicQueues/" + queue);
        } catch (NamingException e) {
            e.printStackTrace();
        }
        MBeanServerConnection mbsc = mBeanServerConnection;
        ObjectName on = null;
        try {
            on = ObjectNameBuilder.DEFAULT.getQueueObjectName(SimpleString.toSimpleString(dynamicQueue.getQueueName()), SimpleString.toSimpleString(dynamicQueue.getQueueName()), RoutingType.ANYCAST);
        } catch (Exception e) {
            e.printStackTrace();
        }
        queueControl = MBeanServerInvocationHandler.newProxyInstance(mbsc, on, QueueControl.class, false);
        try {
            queueLength = queueControl.browse().length;
        } catch (Exception e) {
            e.printStackTrace();
        }
        i = queueLength;
        return queueControl;
    }

It seems to me that the error is in the field:

on = ObjectNameBuilder.DEFAULT.getQueueObjectName(SimpleString.toSimpleString(dynamicQueue.getQueueName()), SimpleString.toSimpleString(dynamicQueue.getQueueName()), RoutingType.ANYCAST);

Because the Default value is:

public static final ObjectNameBuilder DEFAULT = new ObjectNameBuilder(ActiveMQDefaultConfiguration.getDefaultJmxDomain(), "localhost", true);

Here is the error that occurs:

javax.management.InstanceNotFoundException: org.apache.activemq.artemis:broker="localhost",component=addresses,address="queueTest",subcomponent=queues,routing-type="anycast",queue="queueTest"
 at java.management/com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.getMBean(DefaultMBeanServerInterceptor.java:1083)
 at java.management/com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.getClassLoaderFor(DefaultMBeanServerInterceptor.java:1421)
 at java.management/com.sun.jmx.mbeanserver.JmxMBeanServer.getClassLoaderFor(JmxMBeanServer.java:1322)
 at java.base/jdk.internal.reflect.GeneratedMethodAccessor21.invoke(Unknown Source)
 at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
 at java.base/java.lang.reflect.Method.invoke(Method.java:566)
 at org.apache.activemq.artemis.core.server.management.ArtemisMBeanServerBuilder$MBeanInvocationHandler.invoke(ArtemisMBeanServerBuilder.java:96)
 at com.sun.proxy.$Proxy13.getClassLoaderFor(Unknown Source)
 at java.management.rmi/javax.management.remote.rmi.RMIConnectionImpl$6.run(RMIConnectionImpl.java:1363)
 at java.base/java.security.AccessController.doPrivileged(Native Method)
 at java.management.rmi/javax.management.remote.rmi.RMIConnectionImpl.getClassLoaderFor(RMIConnectionImpl.java:1360)
 at java.management.rmi/javax.management.remote.rmi.RMIConnectionImpl.invoke(RMIConnectionImpl.java:811)
 at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
 at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
 at java.base/java.lang.reflect.Method.invoke(Method.java:566)
 at java.rmi/sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:359)
 at java.rmi/sun.rmi.transport.Transport$1.run(Transport.java:200)
 at java.rmi/sun.rmi.transport.Transport$1.run(Transport.java:197)
 at java.base/java.security.AccessController.doPrivileged(Native Method)
 at java.rmi/sun.rmi.transport.Transport.serviceCall(Transport.java:196)
 at java.rmi/sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:562)
 at java.rmi/sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:796)
 at java.rmi/sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.lambda$run$0(TCPTransport.java:677)
 at java.base/java.security.AccessController.doPrivileged(Native Method)
 at java.rmi/sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:676)
 at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
 at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
 at java.base/java.lang.Thread.run(Thread.java:834)
 at java.rmi/sun.rmi.transport.StreamRemoteCall.exceptionReceivedFromServer(StreamRemoteCall.java:303)
 at java.rmi/sun.rmi.transport.StreamRemoteCall.executeCall(StreamRemoteCall.java:279)
 at java.rmi/sun.rmi.server.UnicastRef.invoke(UnicastRef.java:164)
 at jdk.remoteref/jdk.jmx.remote.internal.rmi.PRef.invoke(Unknown Source)
 at java.management.rmi/javax.management.remote.rmi.RMIConnectionImpl_Stub.invoke(Unknown Source)
 at java.management.rmi/javax.management.remote.rmi.RMIConnector$RemoteMBeanServerConnection.invoke(RMIConnector.java:1021)

It seems to me that the error is in the Default variable. And if in it, then what should I replace it with so that I can connect to both the local broker and the remote one?

I did the method as you described, but now there is no connection either to localhost or to a remote broker:

public static QueueControl getQueueControl(String queue, MBeanServerConnection mBeanServerConnection) {
    QueueControl queueControl = null;
    try {

        Queue dynamicQueue = (Queue) initialContext.lookup("dynamicQueues/" + queue);
        MBeanServerConnection mbsc = mBeanServerConnection;
        String brokerName = mbsc.queryNames(
                ObjectName.getInstance("org.apache.activemq.artemis:broker=*"),
                null).stream().findFirst().get().getKeyProperty("broker");

        ObjectNameBuilder myONB = ObjectNameBuilder.create(
                ActiveMQDefaultConfiguration.getDefaultJmxDomain(), brokerName, true);

        ObjectName on = myONB.getQueueObjectName(SimpleString.toSimpleString(dynamicQueue.getQueueName()), SimpleString.toSimpleString(dynamicQueue.getQueueName()), RoutingType.ANYCAST);
        queueControl = MBeanServerInvocationHandler.newProxyInstance(mbsc, on, QueueControl.class, false);
        queueLength = queueControl.browse().length;
        i = queueLength;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return queueControl;
}

Error stacktrace:

javax.management.InstanceNotFoundException: org.apache.activemq.artemis:broker="\"localhost\"",component=addresses,address="exampleQueue",subcomponent=queues,routing-type="anycast",queue="exampleQueue"
at java.management/com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.getMBean(DefaultMBeanServerInterceptor.java:1083)
at java.management/com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.getClassLoaderFor(DefaultMBeanServerInterceptor.java:1421)
at java.management/com.sun.jmx.mbeanserver.JmxMBeanServer.getClassLoaderFor(JmxMBeanServer.java:1322)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at org.apache.activemq.artemis.core.server.management.ArtemisMBeanServerBuilder$MBeanInvocationHandler.invoke(ArtemisMBeanServerBuilder.java:96)
at com.sun.proxy.$Proxy30.getClassLoaderFor(Unknown Source)
at java.management.rmi/javax.management.remote.rmi.RMIConnectionImpl$6.run(RMIConnectionImpl.java:1363)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at java.management.rmi/javax.management.remote.rmi.RMIConnectionImpl.getClassLoaderFor(RMIConnectionImpl.java:1360)
at java.management.rmi/javax.management.remote.rmi.RMIConnectionImpl.invoke(RMIConnectionImpl.java:811)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at java.rmi/sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:359)
at java.rmi/sun.rmi.transport.Transport$1.run(Transport.java:200)
at java.rmi/sun.rmi.transport.Transport$1.run(Transport.java:197)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at java.rmi/sun.rmi.transport.Transport.serviceCall(Transport.java:196)
at java.rmi/sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:562)
at java.rmi/sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:796)
at java.rmi/sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.lambda$run$0(TCPTransport.java:677)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at java.rmi/sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:676)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
at java.base/java.lang.Thread.run(Thread.java:834)
at java.rmi/sun.rmi.transport.StreamRemoteCall.exceptionReceivedFromServer(StreamRemoteCall.java:303)
at java.rmi/sun.rmi.transport.StreamRemoteCall.executeCall(StreamRemoteCall.java:279)
at java.rmi/sun.rmi.server.UnicastRef.invoke(UnicastRef.java:164)
at jdk.remoteref/jdk.jmx.remote.internal.rmi.PRef.invoke(Unknown Source)
at java.management.rmi/javax.management.remote.rmi.RMIConnectionImpl_Stub.invoke(Unknown Source)
at java.management.rmi/javax.management.remote.rmi.RMIConnector$RemoteMBeanServerConnection.invoke(RMIConnector.java:1021)
at java.management/javax.management.MBeanServerInvocationHandler.invoke(MBeanServerInvocationHandler.java:298)
at com.sun.proxy.$Proxy7.browse(Unknown Source)

The error appears in this field:

queueLength = queueControl.browse().length;

And yes, I have tried to create an ObjectNameBuilder as you described, but it shows an error when creating:

'ObjectNameBuilder(java.lang.String, java.lang.String, boolean)' has private access in 'org.apache.activemq.artemis.api.core.management.ObjectNameBuilder'

答案1

得分: 2

`ObjectNameBuilder`必须使用与broker.xml文件中定义的经纪人名称进行初始化,即如果broker.xml文件中的名称为`<name>0.0.0.0</name>`,则创建以下`ObjectNameBuilder`:

```java
ObjectNameBuilder myONB = ObjectNameBuilder.create(
   ActiveMQDefaultConfiguration.getDefaultJmxDomain(), "<name>0.0.0.0</name>", true);

或者,您可以执行查询以获取经纪人名称,例如:

String brokerName = mbsc.queryNames(
   ObjectName.getInstance("org.apache.activemq.artemis:broker=*"),
   null).stream().findFirst().get().getKeyProperty("broker").replace("\\\"", "");

ObjectNameBuilder myONB = ObjectNameBuilder.create(
   ActiveMQDefaultConfiguration.getDefaultJmxDomain(), brokerName, true);

然后使用它创建一个ObjectName:

myONB.getQueueObjectName(SimpleString.toSimpleString(dynamicQueue.getQueueName()), SimpleString.toSimpleString(dynamicQueue.getQueueName()), RoutingType.ANYCAST);

<details>
<summary>英文:</summary>

The `ObjectNameBuilder` must be initialized with the same broker name defined in the broker.xml file, i.e. if the name in the broker.xml file is `&lt;name&gt;0.0.0.0&lt;/name&gt;` create the following ObjectNameBuilder:

ObjectNameBuilder myONB = ObjectNameBuilder.create(
ActiveMQDefaultConfiguration.getDefaultJmxDomain(), "0.0.0.0", true);


alternatively, you could get the broker name executing a query, i.e.:

String brokerName = mbsc.queryNames(
ObjectName.getInstance("org.apache.activemq.artemis:broker=*"),
null).stream().findFirst().get().getKeyProperty("broker").replace("&quot;", "");

ObjectNameBuilder myONB = ObjectNameBuilder.create(
ActiveMQDefaultConfiguration.getDefaultJmxDomain(), brokerName, true);


and use it to create an ObjectName:

myONB.getQueueObjectName(SimpleString.toSimpleString(dynamicQueue.getQueueName()), SimpleString.toSimpleString(dynamicQueue.getQueueName()), RoutingType.ANYCAST);


</details>



huangapple
  • 本文由 发表于 2023年6月5日 17:53:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76405248.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定