Here is spring-integration-aws project. They provide example about Inbound Channle adapter:
@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;
}
}
Ok, Channel and SqsMessageDrivenChannelAdapter are defined, but what is the next? Let say that I have spring bean like that:
import com.amazonaws.services.sqs.model.Message;
@Component
public class MyComponent {
public void onMessage(Message message) throws Exception {
//handle sqs message
}
}
tell spring to pass all messages from myQueue to this
component?SQS mark them as
processing and they are not visible to other clients, so it is
needed to fetch only one message, process nad fetch one next. Does
this behavior enabled by default?You should read the Spring Integration Reference Manual.
@Component
public class MyComponent {
@ServiceActivator(inputChannel = "inputChannel")
public void onMessage(Message message) throws Exception {
//handle sqs message
}
}
Answering to your second question:
/**
* 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;
}
By default it is 10.
The question about mark them as processing can be achieved with the SqsMessageDeletionPolicy option:
/**
* 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,
Such an Acknowledgment object is placed into the AwsHeaders.ACKNOWLEDGMENT Message header which you can get from your onMessage() method and acknowledge it whenever you need.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With