Amazon SQS 消息队列 Java SDK 使用,从平台获取队列数据java SDK的引入和使用
参考官网api地址
https://docs.aws.amazon.com/zh_cn/AWSSimpleQueueService/latest/SQSDeveloperGuide/sqs-java-tutorials.html
1.配置本地的安全凭据,需要事先获取到 aws_access_key_id 和aws_secret_access_key
选择以下选项之一以设置默认凭据:
-
在中设置凭证AWS本地系统上的凭证配置文件,该配置文件位于:
-
~/.aws/credentials
(在 Linux、macOS 或 Unix) 上 -
Windows 上的
C:\Users\USERNAME\.aws\credentials
-
此文件应包含以下格式的行:
[default]
aws_access_key_id = your_access_key_id
aws_secret_access_key = your_secret_access_key
替换您自己的AWS值的凭据值你的 _access_key_id和你的 _secret_access_key.
以上设置凭证方法是多种方法之一,我用这种方式成功了 如图
参考官网api:https://docs.aws.amazon.com/zh_cn/sdk-for-java/latest/developer-guide/setup.html#setup-credentials
2.maven项目的pom.xml引入sdk
<dependencyManagement> <dependencies> <dependency> <groupId>software.amazon.awssdk</groupId> <artifactId>bom</artifactId> <version>2.17.190</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>software.amazon.awssdk</groupId> <artifactId>sqs</artifactId> </dependency> <dependency> <groupId>software.amazon.awssdk</groupId> <artifactId>s3</artifactId> </dependency> </dependencies>
3.使用案例模板测试下可用
//snippet-sourcedescription:[SendReceiveMessages.java demonstrates how to send multiple messages to a queue, check for those messages, and delete the messages once received.] //snippet-keyword:[SDK for Java 2.0] //snippet-keyword:[Code Sample] //snippet-service:[sqs] //snippet-sourcetype:[full-example] //snippet-sourcedate:[2/24/2020] //snippet-sourceauthor:[scmacdon-aws] // snippet-start:[sqs.java2.send_recieve_messages.complete] /* * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at: * * http://aws.amazon.com/apache2.0 * * This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES * OR CONDITIONS OF ANY KIND, either express or implied. See the * License for the specific language governing permissions and * limitations under the License. */ // snippet-start:[sqs.java2.send_recieve_messages.import] package com.example.sqs; import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.sqs.SqsClient; import software.amazon.awssdk.services.sqs.model.CreateQueueRequest; import software.amazon.awssdk.services.sqs.model.CreateQueueResponse; import software.amazon.awssdk.services.sqs.model.GetQueueUrlRequest; import software.amazon.awssdk.services.sqs.model.Message; import software.amazon.awssdk.services.sqs.model.QueueNameExistsException; import software.amazon.awssdk.services.sqs.model.ReceiveMessageRequest; import software.amazon.awssdk.services.sqs.model.SendMessageBatchRequest; import software.amazon.awssdk.services.sqs.model.SendMessageBatchRequestEntry; import software.amazon.awssdk.services.sqs.model.SendMessageRequest; import java.util.Date; import java.util.List; // snippet-end:[sqs.java2.send_recieve_messages.import] // snippet-start:[sqs.java2.send_recieve_messages.main] public class SendReceiveMessages { private static final String QUEUE_NAME = "xxxx"; public static void main(String[] args) { SqsClient sqsClient = SqsClient.builder() //.region(Region.US_WEST_2) .region(Region.AP_SOUTHEAST_1) .build(); try { CreateQueueRequest request = CreateQueueRequest.builder() .queueName(QUEUE_NAME) .build(); CreateQueueResponse createResult = sqsClient.createQueue(request); GetQueueUrlRequest getQueueRequest = GetQueueUrlRequest.builder() .queueName(QUEUE_NAME) .build(); String queueUrl = sqsClient.getQueueUrl(getQueueRequest).queueUrl(); SendMessageRequest sendMsgRequest = SendMessageRequest.builder() .queueUrl(queueUrl) .messageBody("hello world") .delaySeconds(5) .build(); sqsClient.sendMessage(sendMsgRequest); // Send multiple messages to the queue SendMessageBatchRequest sendBatchRequest = SendMessageBatchRequest.builder() .queueUrl(queueUrl) .entries( SendMessageBatchRequestEntry.builder() .messageBody("Hello from message 1") .id("msg_1") .build() , SendMessageBatchRequestEntry.builder() .messageBody("Hello from message 2") .delaySeconds(10) .id("msg_2") .build()) .build(); sqsClient.sendMessageBatch(sendBatchRequest); // Receive messages from the queue ReceiveMessageRequest receiveRequest = ReceiveMessageRequest.builder() .queueUrl(queueUrl) .build(); List<Message> messages = sqsClient.receiveMessage(receiveRequest).messages(); // Print out the messages for (Message m : messages) { System.out.println("\n=====" +m.body()); } } catch (QueueNameExistsException e) { throw e; } } } // snippet-end:[sqs.java2.send_recieve_messages.main] // snippet-end:[sqs.java2.send_recieve_messages.complete]
上边这段代码包含了发送消息,接受消息的都有了,自己在此基础上任意发挥把