英文:
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
是从消息中收集的用于浏览的字节数。这对于在队列控制上公开的browse
和list
管理方法很重要。超过此值的消息属性将显示为截断。默认值为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("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();
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("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"));
}
}
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论