有人可以提供spring-integration-aws SQS用法的例子吗?

时间:2017-05-02 07:44:22

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

Herespring-integration-aws项目。它们提供了有关Inbound Channle适配器的示例:

@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已定义,但下一步是什么?假设我有这样的春豆:

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

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

2 个答案:

答案 0 :(得分:1)

您应该阅读Spring Integration Reference Manual

@Component
public class MyComponent {

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

} 

答案 1 :(得分:1)

回答你的第二个问题:

/**
 * 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()方法获取该标题,并在需要时对其进行确认。