如何使用Spring的JmsTemplate在AWS SQS消息上添加投递延迟。

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

how to add delivery delay on AWS SQS messages using Spring JmsTemplate

问题

嗨,我想实现类似于使用Java和JmsTemplate发送具有特定传递延迟的SQS消息的函数。\n实际上,我已经完成了发送没有延迟的消息的实现。\n像这样:\n\n\n @Override\n public boolean sendMessage(String queueName, String message) throws EventBrokerException {\n final String messageWithTransactionGuid = addTransactionGuidToMessage(message);\n jmsTemplate.convertAndSend(queueName, messageWithTransactionGuid);\n return isSuccess;\n }\n\n\n我阅读了一些文章,也许我需要做类似这样的事情?\n\n\n jmsTemplate.convertAndSend(queueName, messageWithTransactionGuid, new MessagePostProcessor() {\n @Override\n public Message postProcessMessage(Message message) throws JMSException {\n message.setIntProperty(/*指定字段名和延迟秒数*/);\n return message;\n }\n });\n \n但我没有看到任何示例,我阅读了源代码,也没有找到需要指定什么,请问有人可以帮忙吗?

英文:

Hi I want to implement the function like sending sqs messages with a specific delivery delay using java and JmsTemplate.
Actually, I finished the implementation of sending messages without delay.
like this:

    @Override
    public boolean sendMessage(String queueName, String message) throws EventBrokerException {
        final String messageWithTransactionGuid = addTransactionGuidToMessage(message);
        jmsTemplate.convertAndSend(queueName, messageWithTransactionGuid);
        return isSuccess;
    }

I read some articles, Maybe I need to do somthing like this???

    jmsTemplate.convertAndSend(queueName, messageWithTransactionGuid, new MessagePostProcessor() {
            @Override
            public Message postProcessMessage(Message message) throws JMSException {
                message.setIntProperty(/*specify field name and delay seconds*/);
                return message;
            }
        });

But I didn't see any examples, I read the sourcecode and didn't find out what to specify, can anybody help?

答案1

得分: 2

所以我所做的是一个简单的方法:

    private Boolean doSendMessage(String queueName, String message, long deliveryDelay){

        jmsTemplate.setDeliveryDelay(deliveryDelay); 
        jmsTemplate.convertAndSend(queueName, message);
        jmsTemplate.setDeliveryDelay(DEFAULT_MESSAGE_DELIVERY_DELAY);
        return isSuccess;
    }

这里的 DEFAULT_MESSAGE_DELIVERY_DELAY 是 -1。

我注入了 jmsTemplate,并设置了 setDeliveryDelay 和 resetDeliveryDelay。

英文:

So what I did is a simple way:

    private Boolean doSendMessage(String queueName, String message, long deliveryDelay){

        jmsTemplate.setDeliveryDelay(deliveryDelay); 
       jmsTemplate.convertAndSend(queueName, message);
        jmsTemplate.setDeliveryDelay(DEFAULT_MESSAGE_DELIVERY_DELAY);
        return isSuccess;
    }

Here DEFAULT_MESSAGE_DELIVERY_DELAY is -1.

I injected jsmTemplate, and setDeliveryDelay and resetDeliveryDelay

huangapple
  • 本文由 发表于 2020年3月17日 02:27:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/60711365.html
匿名

发表评论

匿名网友

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

确定