我想向ActiveMQ Artemis队列发送消息,但我不明白如何在那里传递标头,例如:消息生命周期、消息类型和其他标头,而不是属性
以下是使用该方法通过JMX协议向队列发送消息的代码:
public static void sendMessage(QueueControl queueControl, JMXMessage jmxMessage, String user, String password) throws Exception {
boolean flag = false;
queueControl.sendMessage(jmxMessage.getJmxProperties(), Integer.parseInt(jmxMessage.getJmxHeaders().getJmxType()),jmxMessage.getJmxBody(),flag,user, password);
}
类JMXMessage:
public class JMXMessage implements Serializable {
private JMXHeaders jmxHeaders;
private Map<String, String> jmxProperties;
private String jmxBody;
public JMXMessage(JMXHeaders jmxHeaders,String jmxBody) {
this.jmxHeaders = jmxHeaders;
this.jmxBody = jmxBody;
}
public JMXHeaders getJmxHeaders() {
return jmxHeaders;
}
public void setJmxHeaders(JMXHeaders jmxHeaders) {
this.jmxHeaders = jmxHeaders;
}
public Map<String, String> getJmxProperties() {
return jmxProperties;
}
public void setJmxProperties(Map<String, String> jmxProperties) {
this.jmxProperties = jmxProperties;
}
public String getJmxBody() {
return jmxBody;
}
public void setJmxBody(String jmxBody) {
this.jmxBody = jmxBody;
}
@Override
public String toString() {
return "Headers" + "\n" + "{" + "\n" +
" MessageId=" + jmxHeaders.getJmxMessageId() + ";" + "\n" +
" Priority=" + jmxHeaders.getJmxPriority() + ";" + "\n" +
" Timestamp=" + jmxHeaders.getJmxTimestamp() + ";" + "\n" +
" Expires=" + jmxHeaders.getJmxExpires() + ";" + "\n" +
" Type=" + jmxHeaders.getJmxType() + ";" + "\n" +
" DeliveryMode=" + jmxHeaders.getJmxDeliveryMode() + ";" + "\n" +
" CorrelationId=" + jmxHeaders.getJmxCorrelationId() + ";" + "\n" +
" ReplyTo=" + jmxHeaders.getJmxReplyTo() + ";" + "\n" + "}" + "\n" + "\n" +
"Properties" + "\n" + "{" + "\n" +
" Properties=" + jmxProperties + ";" + "\n" + "}" + "\n" + "\n" +
"Body" + "\n" + "{" + "\n" +
jmxBody + ";" + "\n" + "}" + "\n" + "\n";
}
}
类JMXHeaders:
public class JMXHeaders implements Serializable {
private String jmxType;
private String jmxExpires;
private String jmxDeliveryMode;
private String jmxReplyTo;
private String jmxMessageId;
private String jmxTimestamp;
private String jmxCorrelationId;
private String jmxPriority;
public JMXHeaders(String jmxType, String jmxExpires, String jmxDeliveryMode, String jmxReplyTo, String jmxMessageId, String jmxTimestamp, String jmxCorrelationId, String jmxPriority) {
this.jmxType = jmxType;
this.jmxExpires = jmxExpires;
this.jmxDeliveryMode = jmxDeliveryMode;
this.jmxReplyTo = jmxReplyTo;
this.jmxMessageId = jmxMessageId;
this.jmxTimestamp = jmxTimestamp;
this.jmxCorrelationId = jmxCorrelationId;
this.jmxPriority = jmxPriority;
}
public JMXHeaders(String jmxType, String jmxExpires, String jmxDeliveryMode, String jmxReplyTo) {
this.jmxType = jmxType;
this.jmxExpires = jmxExpires;
this.jmxDeliveryMode = jmxDeliveryMode;
this.jmxReplyTo = jmxReplyTo;
}
public String getJmxExpires() {
return jmxExpires;
}
public void setJmxExpires(String jmxExpires) {
this.jmxExpires = jmxExpires;
}
public String getJmxMessageId() {
return jmxMessageId;
}
public void setJmxMessageId(String jmxMessageId) {
this.jmxMessageId = jmxMessageId;
}
public String getJmxTimestamp() {
return jmxTimestamp;
}
public void setJmxTimestamp(String jmxTimestamp) {
this.jmxTimestamp = jmxTimestamp;
}
public String getJmxCorrelationId() {
return jmxCorrelationId;
}
public void setJmxCorrelationId(String jmxCorrelationID) {
this.jmxCorrelationId = jmxCorrelationID;
}
public String getJmxPriority() {
return jmxPriority;
}
public void setJmxPriority(String jmxPriority) {
this.jmxPriority = jmxPriority;
}
public String getJmxType() {
return jmxType;
}
public void setJmxType(String jmxType) {
this.jmxType = jmxType;
}
public String getJmxDeliveryMode() {
return jmxDeliveryMode;
}
public void setJmxDeliveryMode(String jmxDeliveryMode) {
this.jmxDeliveryMode = jmxDeliveryMode;
}
public String getJmxReplyTo() {
return jmxReplyTo;
}
public void setJmxReplyTo(String jmxReplyTo) {
this.jmxReplyTo = jmxReplyTo;
}
@Override
public String toString() {
return "JMXHeaders{" +
"jmxType='" + jmxType + '\'' +
", jmxExpires='" + jmxExpires + '\'' +
", jmxDeliveryMode='" + jmxDeliveryMode + '\'' +
", jmxReplyTo='" + jmxReplyTo + '\'' +
", jmxMessageId='" + jmxMessageId + '\'' +
", jmxTimestamp='" + jmxTimestamp + '\'' +
", jmxCorrelationId='" + jmxCorrelationId + '\'' +
", jmxPriority='" + jmxPriority + '\'' +
'}';
}
}
这是用于发送消息的JMX库方法:
String sendMessage(@Parameter(name = "headers",desc = "The headers to add to the message") Map<String, String> var1, @Parameter(name = "type",desc = "A type for the message") int var2, @Parameter(name = "body",desc = "The body (byte[]) of the message encoded as a string using Base64") String var3, @Parameter(name = "durable",desc = "Whether the message is durable") boolean var4, @Parameter(name = "user",desc = "The user to authenticate with") String var5, @Parameter(name = "password",desc = "The users password to authenticate with") String var6) throws Exception;
所以不明白如何发送一个字节类型,消息生命周期300秒的消息,如果没有发送header的字段
消息的过期或“生存时间”不能通过标头或属性设置,因此无法通过sendMessage
JMX操作执行此操作。
如果要发送字节消息(即消息主体是byte[]
的消息),则需要将类型设置为4
并将主体的byte[]
编码为Base64String
。
需要明确的是,JMX用于管理而不是消息传递。因此,您应该使用消息传递协议而不是JMX发送消息。尽管有一种发送消息的管理方法,但它是非常基本的,除了最基本的用例(例如,查看代理是否正常工作的基本测试)之外,它不适用于任何东西。