如何使用 JMS 将计划的消息发送到 Azure 服务总线
How to send scheduled message to Azure Service Bus with JMS
Azure 服务总线能够发送预定消息。
使用此处描述的 AMQP 协议发送预定消息:https://docs.microsoft.com/en-us/azure/service-bus-messaging/service-bus-amqp-request-response#message-operations
Schedules messages.
Request
The request message must include the following application properties:
| Key | Value | Type | Required | Value Contents
| operation | string | Yes | com.microsoft:schedule-message
| com.microsoft:server-timeout | uint | No | Operation server timeout in milliseconds.|
我使用来自 Spring 框架的 java JmsTemplate 使用 Azure 服务总线。
如何映射消息 headers 发送预定消息?
@Test
public void sendMessageWithHeaders() {
jmsTemplate.send("test-topic-2", new MessageCreator() {
@Override
public Message createMessage(Session session) throws JMSException {
TextMessage textMessage = session.createTextMessage("test-123");
((JmsTextMessage) textMessage).setValidatePropertyNames(false);
textMessage.setStringProperty("operation", "com.microsoft:schedule-message");
textMessage.setIntProperty("com.microsoft:server-timeout", 100000);
return textMessage;
}
});
}
-生成序号消息
此代码有效:
Azure SB 使用未记录的消息注释 header x-opt-scheduled-enqueue-time
static final long ONE_MINUTE_IN_MILLIS=60000;//millisecs
@Test
public void sendMessageWithHeaders() {
jmsTemplate.send(queueName, new MessageCreator() {
@Override
public Message createMessage(Session session) throws JMSException {
TextMessage textMessage = session.createTextMessage("test-123");
((JmsTextMessage) textMessage).setValidatePropertyNames(false);
org.apache.qpid.proton.message.Message amqpMessage = ((AmqpJmsTextMessageFacade)((JmsTextMessage)textMessage).getFacade()).getAmqpMessage();
HashMap applicationPropertiesMap = new HashMap();
applicationPropertiesMap.put("operation", "com.microsoft:schedule-message");
applicationPropertiesMap.put("com.microsoft:server-timeout", 100000000);
amqpMessage.setApplicationProperties(new ApplicationProperties(applicationPropertiesMap));
Calendar date = Calendar.getInstance();
long t= date.getTimeInMillis();
Date afterAddingTenMins=new Date(t + (10 * ONE_MINUTE_IN_MILLIS));
amqpMessage.getMessageAnnotations().getValue().put(Symbol.valueOf("x-opt-scheduled-enqueue-time"), afterAddingTenMins);
return textMessage;
}
});
}
2021 年更新:
正如您在我对 的回答中看到的那样,您需要一种稍微不同的方法,因为 .getAmqpMessage()
不再可用:
public void sendDelayedMessage() {
final var now = ZonedDateTime.now();
jmsTemplate.send("test-queue", session -> {
final var tenMinutesFromNow = now.plusMinutes(10);
final var textMessage = session.createTextMessage("Hello Service Bus!");
((JmsTextMessage) textMessage).getFacade().setTracingAnnotation("x-opt-scheduled-enqueue-time", Date.from(tenMinutesFromNow.toInstant()));
return textMessage;
});
log.info("Sent at: " + now);
}
Azure 服务总线能够发送预定消息。 使用此处描述的 AMQP 协议发送预定消息:https://docs.microsoft.com/en-us/azure/service-bus-messaging/service-bus-amqp-request-response#message-operations
Schedules messages. Request
The request message must include the following application properties:
| Key | Value | Type | Required | Value Contents
| operation | string | Yes | com.microsoft:schedule-message
| com.microsoft:server-timeout | uint | No | Operation server timeout in milliseconds.|
我使用来自 Spring 框架的 java JmsTemplate 使用 Azure 服务总线。 如何映射消息 headers 发送预定消息?
@Test
public void sendMessageWithHeaders() {
jmsTemplate.send("test-topic-2", new MessageCreator() {
@Override
public Message createMessage(Session session) throws JMSException {
TextMessage textMessage = session.createTextMessage("test-123");
((JmsTextMessage) textMessage).setValidatePropertyNames(false);
textMessage.setStringProperty("operation", "com.microsoft:schedule-message");
textMessage.setIntProperty("com.microsoft:server-timeout", 100000);
return textMessage;
}
});
}
-生成序号消息
此代码有效:
Azure SB 使用未记录的消息注释 header x-opt-scheduled-enqueue-time
static final long ONE_MINUTE_IN_MILLIS=60000;//millisecs
@Test
public void sendMessageWithHeaders() {
jmsTemplate.send(queueName, new MessageCreator() {
@Override
public Message createMessage(Session session) throws JMSException {
TextMessage textMessage = session.createTextMessage("test-123");
((JmsTextMessage) textMessage).setValidatePropertyNames(false);
org.apache.qpid.proton.message.Message amqpMessage = ((AmqpJmsTextMessageFacade)((JmsTextMessage)textMessage).getFacade()).getAmqpMessage();
HashMap applicationPropertiesMap = new HashMap();
applicationPropertiesMap.put("operation", "com.microsoft:schedule-message");
applicationPropertiesMap.put("com.microsoft:server-timeout", 100000000);
amqpMessage.setApplicationProperties(new ApplicationProperties(applicationPropertiesMap));
Calendar date = Calendar.getInstance();
long t= date.getTimeInMillis();
Date afterAddingTenMins=new Date(t + (10 * ONE_MINUTE_IN_MILLIS));
amqpMessage.getMessageAnnotations().getValue().put(Symbol.valueOf("x-opt-scheduled-enqueue-time"), afterAddingTenMins);
return textMessage;
}
});
}
2021 年更新:
正如您在我对 .getAmqpMessage()
不再可用:
public void sendDelayedMessage() {
final var now = ZonedDateTime.now();
jmsTemplate.send("test-queue", session -> {
final var tenMinutesFromNow = now.plusMinutes(10);
final var textMessage = session.createTextMessage("Hello Service Bus!");
((JmsTextMessage) textMessage).getFacade().setTracingAnnotation("x-opt-scheduled-enqueue-time", Date.from(tenMinutesFromNow.toInstant()));
return textMessage;
});
log.info("Sent at: " + now);
}