如何将CompositeData[]类对象转换为Artemis JMX中的Message类对象?

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

How to translate CompositeData[] class objects into Message class objects in Artemis JMX?

问题

我正在使用JMX连接到Artemis队列,通过QueueControl类和其browse方法,它返回一个CompositeData数组,但我无法使用get方法获取单独的标头,因为没有标头。请告诉我如何通过JMX在Artemis上获取特定字段的标头而不是整个标头主体。

我还尝试了listMessage方法,但它提供了不完整的标头列表,不显示消息的主体。

英文:

I'm using JMX to connect to an Artemis queue via the QueueControl class and its browse method, which returns a CompositeData array, but I can't get the individual headers with the get methods as there aren't any. Please tell me how to get not the whole body of the headers, but certain fields via JMX on Artemis

I also tried the listMessage method, but it gives an incomplete list of headers and does not display the body of the message

答案1

得分: 0

获取CompositeData中的消息详细信息的关键是CompositeDataConstants类。

如果我发送了这样一条消息:

      ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://localhost:61616");
      Connection connection = connectionFactory.createConnection();
      Session session = connection.createSession();
      MessageProducer messageProducer = session.createProducer(session.createQueue("myQueue"));
      TextMessage message = session.createTextMessage();
      message.setJMSReplyTo(session.createQueue("myReplyTo"));
      message.setText("myText");
      message.setStringProperty("myKey1", "myValue1");
      message.setStringProperty("myKey2", "myValue2");
      messageProducer.send(message);
      connection.close();

你可以浏览队列并反序列化消息数据,例如:

      QueueControl queueControl = ...
      CompositeData[] messages = queueControl.browse();
      for (CompositeData message : messages) {
         System.out.println("JMSMessageID: " + message.get(CompositeDataConstants.USER_ID));
         System.out.println("Destination: " + message.get(CompositeDataConstants.ADDRESS));
         System.out.println("Text: " + message.get(CompositeDataConstants.TEXT_BODY));
         System.out.println("DeliveryMode: " + ((Boolean) message.get(CompositeDataConstants.DURABLE) ? "persistent" : "non-persistent"));
         TabularData stringProps = (TabularData) message.get(CompositeDataConstants.STRING_PROPERTIES);
         for (CompositeData o : (Collection<CompositeData>) stringProps.values()) {
            System.out.println(o.get("key") + ": " + o.get("value"));
         }
      }

这将打印类似于以下内容:

JMSMessageID: ID:41171b0e-dfae-11ed-9217-3ce1a1d12939
Destination: myQueue
Text: myText
DeliveryMode: persistent
JMSReplyTo: queue://myReplyTo
myKey1: myValue1
myKey2: myValue2
__AMQ_CID: 41123909-dfae-11ed-9217-3ce1a1d12939

如果你不想直接使用常量,可以在这里找到它们的相应值。

请注意,由于management-message-attribute-size-limit,返回的数据可能会被截断。文档解释了这个设置:

> management-message-attribute-size-limit是从消息中收集的用于浏览的字节数。这对于在队列控制上公开的browselist管理方法很重要。超过此值的消息属性将显示为截断。默认值为256。使用-1来关闭此限制。请注意,需要为在给定时刻可见的所有消息分配内存。将此值设置得太高可能会影响浏览器的稳定性,因为可能需要大量内存来浏览许多消息。

英文:

The key to getting the message details out of the CompositeData is the CompositeDataConstants class.

If I sent a message like this:

      ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(&quot;tcp://localhost:61616&quot;);
      Connection connection = connectionFactory.createConnection();
      Session session = connection.createSession();
      MessageProducer messageProducer = session.createProducer(session.createQueue(&quot;myQueue&quot;));
      TextMessage message = session.createTextMessage();
      message.setJMSReplyTo(session.createQueue(&quot;myReplyTo&quot;));
      message.setText(&quot;myText&quot;);
      message.setStringProperty(&quot;myKey1&quot;, &quot;myValue1&quot;);
      message.setStringProperty(&quot;myKey2&quot;, &quot;myValue2&quot;);
      messageProducer.send(message);
      connection.close();

You could browse the queue and deserialize the message data, e.g.:

      QueueControl queueControl = ...
      CompositeData[] messages = queueControl.browse();
      for (CompositeData message : messages) {
         System.out.println(&quot;JMSMessageID: &quot; + message.get(CompositeDataConstants.USER_ID));
         System.out.println(&quot;Destination: &quot; + message.get(CompositeDataConstants.ADDRESS));
         System.out.println(&quot;Text: &quot; + message.get(CompositeDataConstants.TEXT_BODY));
         System.out.println(&quot;DeliveryMode: &quot; + ((Boolean) message.get(CompositeDataConstants.DURABLE) ? &quot;persistent&quot; : &quot;non-persistent&quot;));
         TabularData stringProps = (TabularData) message.get(CompositeDataConstants.STRING_PROPERTIES);
         for (CompositeData o : (Collection&lt;CompositeData&gt;) stringProps.values()) {
            System.out.println(o.get(&quot;key&quot;) + &quot;: &quot; + o.get(&quot;value&quot;));
         }
      }

Which would print something like this:

JMSMessageID: ID:41171b0e-dfae-11ed-9217-3ce1a1d12939
Destination: myQueue
Text: myText
DeliveryMode: persistent
JMSReplyTo: queue://myReplyTo
myKey1: myValue1
myKey2: myValue2
__AMQ_CID: 41123909-dfae-11ed-9217-3ce1a1d12939

If you don't want to use the constants directly you can find their respective values here.

Keep in mind that the returned data may be truncated due to the management-message-attribute-size-limit. The documentation explains this setting:

> management-message-attribute-size-limit is the number of bytes collected from the message for browse. This is relevant for the browse and list management methods exposed on the queue control. Message attributes longer than this value appear truncated. Default is 256. Use -1 to switch this limit off. Note that memory needs to be allocated for all messages that are visible at a given moment. Setting this value too high may impact the browser stability due to the large amount of memory that may be required to browse through many messages.

huangapple
  • 本文由 发表于 2023年4月20日 03:37:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/76058228.html
匿名

发表评论

匿名网友

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

确定