如何使用JMS

时间:2017-08-11 09:35:03

标签: azure amqp azureservicebus jmstemplate

Azure Service Bus具有发送预定消息的功能。 使用此处描述的AMQP协议发送预定消息:https://docs.microsoft.com/en-us/azure/service-bus-messaging/service-bus-amqp-request-response#message-operations

  

安排消息。   请求

     

请求消息必须包含以下应用程序属性:

     

|钥匙|价值|输入|必需|价值内容

     

|操作|字符串|是的| com.microsoft:schedule-message

     

| com.microsoft:server-timeout | uint |没有|操作服务器超时(以毫秒为单位)

我使用Spring Framework中的Java JmsTemplate与Azure Service Bus一起工作。 如何映射邮件标头以发送预定邮件?

    @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;
        }
    });
}

- 产生序数消息

2 个答案:

答案 0 :(得分:1)

此代码工作:

Azure SB使用未记录的邮件注释标题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;
        }
    });
}

答案 1 :(得分:1)

2021 年更新: 正如您在我对 this question 的回答中所见,您需要一种稍微不同的方法,因为 .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);
}