编程语言
首页 > 编程语言> > java – 亚马逊sqs的异步消费者

java – 亚马逊sqs的异步消费者

作者:互联网

我不熟悉队列.
我能够成功发布消息并同步接收消息
但是,我现在正试图异步.

sqs提供的参考链接建议使用jmsclient包装器.
如果您已经有一个集成到jms客户端的代码,那么该链接还会提到使用它.

http://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/jmsclient.html#samples

但我重新开始了
我将此示例称为同步发送和recv消息.

https://github.com/aws/aws-sdk-java/blob/master/src/samples/AmazonSimpleQueueService/SimpleQueueServiceSample.java

我可以使用相同的代码,但使用消息监听器实现它吗?
任何代码示例将不胜感激.

解决方法:

Amazon SQS开发人员指南中有关Using JMS with Amazon SQS的部分中有一个代码示例,其中显示了如何使用JMS异步接收消息.

首先,实现MessageListener接口:

class MyListener implements MessageListener {

    @Override
    public void onMessage(Message message) {
        try {
            // Cast the received message as TextMessage and print the text to screen.
            if (message != null) {
                System.out.println("Received: " + ((TextMessage) message).getText());
            }
        } catch (JMSException e) {
            e.printStackTrace();
        }
    }
}

然后将其设置为MessageConsumer的MessageListener:

// Create a consumer for the 'TestQueue'.
MessageConsumer consumer = session.createConsumer(queue);

// Instantiate and set the message listener for the consumer.
consumer.setMessageListener(new MyListener());

// Start receiving incoming messages.
connection.start();

// Wait for 1 second. The listener onMessage() method will be invoked when a message is received.
Thread.sleep(1000);

标签:java,asynchronous,amazon-sqs,jmstemplate
来源: https://codeday.me/bug/20190611/1221804.html