提问者:小点点

在ActiveMQ Artemis中通过JMX连接到代理时引发JMSException错误


我有一个使用某种登录/密码授权通过JMX协议连接到ActiveMQ Artemis代理的代码

 public static MBeanServerConnection connectBroker(String brokerUrl, String user, String password) {

        QueueConnection connection = null;
        try {
            QueueConnectionFactory cf = new ActiveMQQueueConnectionFactory(brokerUrl);
            connection = cf.createQueueConnection();
            connection.start();
            HashMap env = new HashMap();
            String[] creds = {user, password};
            env.put(JMXConnector.CREDENTIALS, creds);
            JMXConnector connector = JMXConnectorFactory.connect(new JMXServiceURL(BuildGet.JMX_URL),env);

            return connector.getMBeanServerConnection();
        } catch (JMSException | IOException e) {
            throw new RuntimeException(e);
        }
    }

此代码给出了这些错误:

Caused by: java.lang.RuntimeException: javax.jms.JMSException: Failed to create session factory
    at jmxClientUIPConsole.BuildGet.connectBroker(BuildGet.java:81)
    at jmxClientUIPConsole.gui.DConsoleFrameController.connectBroker(DConsoleFrameController.java:41)
    ... 56 more
Caused by: javax.jms.JMSException: Failed to create session factory
    at org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory.createConnectionInternal(ActiveMQConnectionFactory.java:867)
    at org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory.createQueueConnection(ActiveMQConnectionFactory.java:335)
    at org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory.createQueueConnection(ActiveMQConnectionFactory.java:331)
    at jmxClientUIPConsole.BuildGet.connectBroker(BuildGet.java:72)
    ... 57 more
Caused by: ActiveMQNotConnectedException[errorType=NOT_CONNECTED message=AMQ219007: Cannot connect to server(s). Tried with all available servers.]
    at org.apache.activemq.artemis.core.client.impl.ServerLocatorImpl.createSessionFactory(ServerLocatorImpl.java:701)
    at org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory.createConnectionInternal(ActiveMQConnectionFactory.java:865)
    ... 60 more

但是,如果您在未经授权的情况下制作方法,那么一切都有效:

public static MBeanServerConnection connectBroker(String brokerUrl) {

        QueueConnection connection = null;
        try {
            QueueConnectionFactory cf = new ActiveMQQueueConnectionFactory(brokerUrl);
            connection = cf.createQueueConnection();
            connection.start();
            JMXConnector connector = JMXConnectorFactory.connect(new JMXServiceURL(BuildGet.JMX_URL));

            return connector.getMBeanServerConnection();
        } catch (JMSException | IOException e) {
            throw new RuntimeException(e);
        }
    }

这是我的管理. xml

<management-context xmlns="http://activemq.apache.org/schema">
   <connector connector-port="13682" connector-host="192.168.1.135"/>
   <authorisation>
      <allowlist>
         <entry domain="hawtio"/>
      </allowlist>
      <default-access>
         <access method="list*" roles="view,update,amq,guest,test"/>
         <access method="get*" roles="view,update,amq,guest,test"/>
         <access method="is*" roles="view,update,amq,guest,test"/>
         <access method="set*" roles="update,amq,guest,test"/>
         <access method="*" roles="amq,guest,test"/>
      </default-access>
      <role-access>
         <match domain="org.apache.activemq.artemis">
            <access method="list*" roles="view,update,amq,guest,test"/>
            <access method="get*" roles="view,update,amq,guest,test"/>
            <access method="is*" roles="view,update,amq,guest,test"/>
            <access method="set*" roles="update,amq,guest,test"/>
            <access method="*" roles="amq,guest,test"/>
         </match>
      </role-access>
   </authorisation>
</management-context>

我将登录amq和密码amq传递给方法。也许我传递了错误的登录名和密码?为什么连接代理时会出错?

现在方法如下所示:

public static MBeanServerConnection connectBroker(String brokerUrl, String user, String password) {
    try {
        Map<String, String[]> env = new HashMap();
        String[] creds = {user, password};
        env.put(JMXConnector.CREDENTIALS, creds);
        JMXConnector connector = JMXConnectorFactory.connect(new JMXServiceURL("service:jmx:rmi:///jndi/rmi://" + brokerUrl + ":13682/jmxrmi"), env);
        return connector.getMBeanServerConnection();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

但是如果我将登录名和密码留空,那么由于某种原因它可以连接,尽管我将这些授权字段留空用于测试。为什么字段数据为空时不会抛出错误?


共1个答案

匿名用户

stack-trace表示异常来自您对javax. jms.QueueConnectionFactory.createQueueConnection()的调用,因此它似乎与您的JMX连接没有任何关系。异常甚至在到达JMXConnectorFactory.connect()之前就被抛出了。

需要注意的是,这个ConnectBroker方法每次调用时都会泄漏一个JMS连接(假设它是成功的)。这是因为您创建了一个JMS连接,但您没有关闭它或从方法中返回它。我建议从该方法中完全删除JMS连接,因为它没有真正的用途。