简体   繁体   English

在Java中使用JMS实施AWS SQS是强制性的吗?

[英]Is it mandatory to implement AWS SQS with JMS in Java?

I am new to AWS SQS and I want to implement it in my project. 我是AWS SQS的新手,我想在我的项目中实现它。 My question is Can I implement SQS independently or Do I need to use SQS Java Messaging Library? 我的问题是我可以独立实现SQS还是需要使用SQS Java Messaging Library? Could you please help me on this? 您能帮我吗? Thanks in advance . 提前致谢 。

Absolutely not - JMS is not at all required. 绝对不需要-完全不需要JMS。 Indeed, I would recommend against it as it is JMS 1.0 and doesn't take advantage of any of the JMS 2 code. 确实,我建议不要使用它,因为它是JMS 1.0,并且不利用任何JMS 2代码。

You can start with something as simple as creating a queue: 您可以从创建队列这样简单的事情开始:

CreateQueueRequest createQueueRequest = new CreateQueueRequest("my-queue-name");
String queueUrl = amazonSQS.createQueue(createQueueRequest).getQueueUrl();

This queue can then have messages put into it and be read from. 然后,该队列可以放入消息并从中读取。

The biggest difference is that SQS requires polling. 最大的不同是SQS需要轮询。 It is not event driven like the onMessage method in a MessageListener . 它不是像MessageListeneronMessage方法那样由事件驱动的。 You can poll and receive up to 10 messages at a time. 您一次最多可以轮询和接收10条消息。 This looks something like: 看起来像这样:

ReceiveMessageRequest receiveMessageRequest = new ReceiveMessageRequest(queueUrl)
                  .withMaxNumberOfMessages(10)
                  .withWaitTimeSeconds(5);

List<Message> messages = amazonSQS.receiveMessage(receiveMessageRequest).getMessages();

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

Having said that, you can have a thread do the reading and, if there is a message, send that to a JMS queue yourself if you'd like. 话虽如此,您可以让线程进行读取,并且如果有消息,则可以根据需要自己将其发送到JMS队列。 That's ultimately what the Amazon JMS/SQS library is doing. 最终,这就是Amazon JMS / SQS库正在做的事情。

Take a look at the Java SQS examples for more detail. 查看Java SQS示例以获取更多详细信息。

EDIT 编辑

To address the question posted in the comment, one way of handling SQS queues is to poll a particular SQS queue in a thread. 为了解决评论中发布的问题,处理SQS队列的一种方法是轮询线程中的特定SQS队列。 While you could poll multiple SQS queues it is a bit simpler in code to poll one. 尽管可以轮询多个SQS队列,但轮询一个队列的代码要简单一些。 And you will poll the SQS queue as long as your program is running. 只要您的程序正在运行,您就会轮询SQS队列。 If you stop polling it you will no longer receive any SQS messages. 如果停止轮询,您将不再收到任何SQS消息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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