简体   繁体   中英

Amazon sns & sqs messages with java

I have 2 different systems (A an B) that communicates using amazon sqs . System A sends messages to system B.

Currently system B gets the messages using a separated thread that starts when the server goes up. Here is the run method:

@Override
public void run() {
    while (true) {
        ReceiveMessageRequest receiveMessageRequest = new ReceiveMessageRequest(myQueueUrl);
        try {
            receiveMessageRequest.setWaitTimeSeconds(1);
            List<Message> messages = sqs.receiveMessage(receiveMessageRequest).getMessages();

            for (Message message : messages) {
             // process messages
            }
        }
    }
}

Looking at this code I have a feeling it is not efficient since it uses a busy waiting loop. I would expect to get the messages using some sort of push mechanism.

Reading a bit about amazon sqs and sns this seems possible using http (server B can expose servlets for that) but still I'm a bit confused.

  • Which one (sns or sqs) should provide me this ability (pushing a message to server B)?
  • What is the easiest way of doing it (any reference to code)?

Of the two, only SNS can be used to push a message to system B. SQS can be used to have system B poll for messages.

It depends on your workload/application requirements as to which is the better solution. If you use SNS, then a big spike in notifications generated by system A will result in a spike in workload of system B, which may not be able to handle the load.

If you were using a queue, the the load spike would be buffered by Amazon SQS, and would not directly impact your system B. This helps decouple system B from system A, and provides a buffer between them. This means you can shutdown system B, do maintenance, then bring the system back up and carry on processing messages like nothing happened (assuming your application can handle delayed processing of messages).

The other main advantage I see in a queue is that it makes scaling the application simpler, since you can start a new instance running a consumer and now you can process messages at a higher rate (assuming no other bottlenecks in your system).

Another consideration is message delivery semantics. Does it matter if messages are delivered out of order or delivered multiple times? I haven't used SNS much, so am unsure of its semantics around this. SQS is a distributed queue, so you may receive messages out of order, or even multiple times in some cases. Can your application deal with this?

SQS supports long polling, allowing you to specify a period to wait (up to 20s) for messages before the receiveMessage call returns. This can limit the number of requests you are making to SQS, thereby costing you less to run. This may be slightly less efficient than SNS, but in applications I have build using SQS, it is not worth worrying about. See http://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/sqs-long-polling.html .

As for examples, the AWS Java SDK available on GitHub includes usage examples of various AWS services. See https://github.com/aws/aws-sdk-java/blob/master/src/samples/AmazonSimpleQueueService/SimpleQueueServiceSample.java and http://docs.aws.amazon.com/sns/latest/dg/SendMessageToHttp.example.java.html for SQS and SNS examples.

You could also combine both SNS and SQS. ie you push a message to a single SNS topic, and have SNS push copies of the message into multiple queues. See http://docs.aws.amazon.com/sns/latest/dg/SendMessageToSQS.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM