【问题标题】:Could somebody provide en example of spring-integration-aws SQS usage?有人可以提供 spring-integration-aws SQS 用法的示例吗?
【发布时间】:2017-09-29 16:04:41
【问题描述】:

Herespring-integration-aws 项目。他们提供了有关入站通道适配器的示例:

@SpringBootApplication
public static class MyConfiguration {

    @Autowired
    private AmazonSQSAsync amazonSqs;

    @Bean
    public PollableChannel inputChannel() {
        return new QueueChannel();
    }

    @Bean
    public MessageProducer sqsMessageDrivenChannelAdapter() {
        SqsMessageDrivenChannelAdapter adapter = new SqsMessageDrivenChannelAdapter(this.amazonSqs, "myQueue");
        adapter.setOutputChannel(inputChannel());
        return adapter;
    }
}

好的,ChannelSqsMessageDrivenChannelAdapter 已定义,但接下来是什么?假设我有这样的spring bean:

import com.amazonaws.services.sqs.model.Message;

@Component
public class MyComponent {
    public void onMessage(Message message) throws Exception {
        //handle sqs message
    }
} 
  1. 如何tell spring 将所有来自myQueue 的消息传递给这个 组件?
  2. 是否有任何额外的配置来处理消息 一?例如在收到消息SQS 后将它们标记为 处理并且它们对其他客户端不可见,因此它是 只需要获取一条消息,接下来处理 nad 获取一条。做 默认情况下启用此行为?

【问题讨论】:

    标签: java spring amazon-web-services spring-integration spring-integration-aws


    【解决方案1】:

    您应该阅读Spring Integration Reference Manual

    @Component
    public class MyComponent {
    
        @ServiceActivator(inputChannel = "inputChannel")
        public void onMessage(Message message) throws Exception {
            //handle sqs message
        }
    
    } 
    

    【讨论】:

      【解决方案2】:

      回答你的第二个问题:

      /**
       * Configure the maximum number of messages that should be retrieved during one poll to the Amazon SQS system. This
       * number must be a positive, non-zero number that has a maximum number of 10. Values higher then 10 are currently
       * not supported by the queueing system.
       *
       * @param maxNumberOfMessages
       *      the maximum number of messages (between 1-10)
       */
      public void setMaxNumberOfMessages(Integer maxNumberOfMessages) {
          this.maxNumberOfMessages = maxNumberOfMessages;
      }
      

      默认为10

      关于mark them as processing的问题可以通过SqsMessageDeletionPolicy选项来实现:

      /**
       * Never deletes message automatically. The receiving listener method must acknowledge each message manually by using
       * the acknowledgment parameter.
       * <p><b>IMPORTANT</b>: When using this policy the listener method must take care of the deletion of the messages.
       * If not, it will lead to an endless loop of messages (poison messages).</p>
       *
       * @see Acknowledgment
       */
      NEVER,
      

      这样一个Acknowledgment 对象被放置在AwsHeaders.ACKNOWLEDGMENT 消息头中,您可以从onMessage() 方法中获取并在需要时确认它。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-05-25
        • 1970-01-01
        • 1970-01-01
        • 2015-03-08
        • 2021-10-31
        • 1970-01-01
        • 2010-10-01
        • 1970-01-01
        相关资源
        最近更新 更多