无法将管理 API 与 JMS 一起使用来更改地址设置

问题描述 投票:0回答:1

我对 JMS 和 Artemis 的世界还很陌生,我正在努力利用管理 API 向我的独立代理发送 JMS 消息。

我使用的是 Artemis 2.38

我想在不使用broker.xml的情况下更新地址设置,但我总是得到空作为回复,并且当我去测试它们时,设置显然没有改变。

我在这里关注了一篇类似的文章,并能够想出这段代码。 这里

try (JMSContext context = activeMQConnectionFactory.createContext(JMSContext.AUTO_ACKNOWLEDGE)) {
            //You MUST explicitly create this queue for management operations.
            Queue managementQueue = context.createQueue("activemq.management");

            Message message = context.createMessage();
            JMSManagementHelper.putOperationInvocation(
                message,
                ResourceNames.BROKER,    // Targetted Resource
                "addAddressSettings",    // Operation to invoke
                "ri.trips1",             // Address match
                null,                    // Dead letter address (DLA)
                null,                    // Expiry address
                -1L,                     // Expiry delay
                true,                    // Last value queue
                7,                       // Max delivery attempts
                -1L,                     // Max size bytes
                0,                       // Page size bytes
                -1,                      // Page max cache size
                1000L,                   // Redelivery delay
                1.0,                     // Redelivery multiplier
                -1L,                     // Max redelivery delay
                -1L,                     // Slow consumer threshold
                false,                   // Slow consumer policy
                null,                    // Slow consumer notification interval
                -1L,                     // Min large message size
                -1L,                     // Consumer window size
                null,                    // Auto-create queues
                true,                    // Auto-create addresses
                false,                   // Auto-delete queues
                false,                   // Auto-delete addresses
                true                     // Auto-delete created queues
            );
            context.createProducer().send(managementQueue, message);

            Message response = context.createConsumer(managementQueue).receive(5000);
            if (response != null && JMSManagementHelper.hasOperationSucceeded(response)) {
                System.out.println("Address settings applied successfully.");
            } else {
                System.err.println("Failed to apply address settings");
            }
        } catch (JMSException e) {
            e.printStackTrace();
        }

这里的代码块总是解析失败,因为响应始终为空。然而,它正在创建地址“activemq.management”就好。

我使用的是 Artemis 2.38

任何对此的帮助将不胜感激!

activemq-artemis
1个回答
0
投票

这里的问题是您发送请求和接收回复的方式。没有任何东西可以将请求与响应关联起来,因此您实际上没有收到响应。您可以使用

QueueRequestor
,如 ActiveMQ Artemis 的 管理示例 中所示,或者您可以坚持使用
JMSContext
并自己关联响应,例如:

try (JMSContext context = activeMQConnectionFactory.createContext(JMSContext.AUTO_ACKNOWLEDGE)) {
   // You MUST explicitly create this queue for management operations.
   Queue managementQueue = context.createQueue("activemq.management");

   Message message = context.createMessage();
   JMSManagementHelper.putOperationInvocation(message, ResourceNames.BROKER, ...);
   Queue responseQueue = context.createTemporaryQueue();
   message.setJMSReplyTo(responseQueue);
   context.createProducer().send(managementQueue, message);
   Message response = context.createConsumer(responseQueue).receive(5000);
   if (response != null && JMSManagementHelper.hasOperationSucceeded(response)) {
      System.out.println("Address settings applied successfully.");
   } else {
      System.err.println("Failed to apply address settings");
   }
} catch (JMSException e) {
   e.printStackTrace();
}

另外,值得注意的是,您正在使用的

addAddressSettings
方法已被弃用。我鼓励您改用这个,它需要灵活的 JSON 输入,例如

try (JMSContext context = activeMQConnectionFactory.createContext(JMSContext.AUTO_ACKNOWLEDGE)) { //You MUST explicitly create this queue for management operations. Queue managementQueue = context.createQueue("activemq.management"); Message message = context.createMessage(); String json = """ { "maxDeliveryAttempts": "7", "maxSizeBytes": "-1", "pageSizeBytes": "0", "redeliveryDelay": "1000" } """; JMSManagementHelper.putOperationInvocation(message, ResourceNames.BROKER, "addAddressSettings", "ri.trips1", json); Queue responseQueue = context.createTemporaryQueue(); message.setJMSReplyTo(responseQueue); context.createProducer().send(managementQueue, message); Message response = context.createConsumer(responseQueue).receive(5000); if (response != null && JMSManagementHelper.hasOperationSucceeded(response)) { System.out.println("Address settings applied successfully."); } else { System.err.println("Failed to apply address settings"); } } catch (JMSException e) { e.printStackTrace(); }
    
© www.soinside.com 2019 - 2024. All rights reserved.