Amazon SQS 消息相关接口测试用例
因项目功能需求,对接了Amazon的SQS消息队列,封装了常用的接口,本文是对部分接口测试用例的简要说明。
1.发送消息
1.1.发送单条消息到标准队列
@Test
public void sendMessage() {
try {
String queueUrl = "https://sqs.cn-northwest-1.amazonaws.com.cn/4203XXXX2685/queue2";
JSONObject messageBody = new JSONObject();
messageBody.put("type", "order");
messageBody.put("action", "add");
messageBody.put("userId", "658786756756");
messageBody.put("skuId", "111");
ReqSendMessageBody reqSendMessageBody = new ReqSendMessageBody();
reqSendMessageBody.setQueueUrl(queueUrl);
reqSendMessageBody.setMessageBody(messageBody.toJSONString());
reqSendMessageBody.setDelaySeconds(60);
Map<String, MessageAttributeValue> messageAttributes = new HashMap<>();
messageAttributes.put("sender", MessageAttributeValue.builder().dataType("String").stringValue("mgq").build());
reqSendMessageBody.setMessageAttributes(messageAttributes);
Map<MessageSystemAttributeNameForSends, MessageSystemAttributeValue> messageSystemAttributes = new HashMap<>();
reqSendMessageBody.setMessageSystemAttributes(messageSystemAttributes);
SendMessageResponse response = this.sqsApiSdk.getMessageApi().sendMessage(reqSendMessageBody);
System.out.println("sendMessage response = " + response.toString());
} catch (SqsException e) {
e.printStackTrace();
}
}
控制台打印: sendMessage response = SendMessageResponse(MD5OfMessageBody=926b9d4682aacf6b756987662b7b7ac9, MD5OfMessageAttributes=593e6a2572f2afcfac33d4a2632701f1, MessageId=38121dcc-57ce-48e0-8f4c-89184ec37591)
1.2.发送单条消息到FIFO队列
@Test
public void sendMessage2fifo() {
try {
String queueUrl = "https://sqs.cn-northwest-1.amazonaws.com.cn/4203XXXX2685/queue1.fifo";
JSONObject messageBody = new JSONObject();
messageBody.put("type", "order");
messageBody.put("action", "add");
messageBody.put("userId", "658786756756");
messageBody.put("skuId", "111");
ReqSendMessageBody reqSendMessageBody = new ReqSendMessageBody();
reqSendMessageBody.setQueueUrl(queueUrl);
reqSendMessageBody.setMessageBody(messageBody.toJSONString());
reqSendMessageBody.setMessageGroupId("default");
reqSendMessageBody.setMessageDeduplicationId(messageBody.toJSONString());
SendMessageResponse response = this.sqsApiSdk.getMessageApi().sendMessage(reqSendMessageBody);
System.out.println("sendMessage response = " + response.toString());
} catch (SqsException e) {
e.printStackTrace();
}
}
控制台打印: sendMessage response = SendMessageResponse(MD5OfMessageBody=926b9d4682aacf6b756987662b7b7ac9, MessageId=077215e0-41fa-4144-9aea-8048598b09f1, SequenceNumber=37310312980864223744)
1.3.批量发送消息到标准队列
@Test
public void sendMessageBatch() {
try {
String queueUrl = "https://sqs.cn-northwest-1.amazonaws.com.cn/4203XXXX2685/queue2";
ReqSendMessageBatchBody reqSendMessageBatchBody = new ReqSendMessageBatchBody();
reqSendMessageBatchBody.setQueueUrl(queueUrl);
List<ReqSendMessageBody> list = new ArrayList<>();
for (int i = 0; i < 10; i++) {
ReqSendMessageBody item = new ReqSendMessageBody();
item.setId(UUID.randomUUID().toString().replace("-", ""));
item.setMessageBody(String.valueOf(i));
list.add(item);
}
reqSendMessageBatchBody.setSendMessageList(list);
RespSendMessageBatchBody response = this.sqsApiSdk.getMessageApi().sendMessageBatch(reqSendMessageBatchBody);
System.out.println("failed num = " + response.getFailedList().size());
for (BatchResultErrorEntry batchResultErrorEntry : response.getFailedList()) {
System.out.println("failed id = " + batchResultErrorEntry.id());
System.out.println("failed code = " + batchResultErrorEntry.code());
System.out.println("failed message = " + batchResultErrorEntry.message());
System.out.println("failed senderFault = " + batchResultErrorEntry.senderFault());
}
System.out.println("successful num = " + response.getSuccessfulList().size());
for (SendMessageBatchResultEntry sendMessageBatchResultEntry : response.getSuccessfulList()) {
System.out.println("successful id = " + sendMessageBatchResultEntry.id());
System.out.println("successful messageId = " + sendMessageBatchResultEntry.messageId());
}
} catch (SqsException e) {
e.printStackTrace();
}
}
控制台打印: failed num = 0 successful num = 10 successful id = 27b5c8691eb44a4ea238c33867dfeb04 successful messageId = 495daa30-3d45-41d7-967a-53b5af067f05 successful id = 066f8c3f91724e7cb51eaf36e7fa1389 successful messageId = 0ab00678-39ab-465b-a3e7-c993bc8e94b1 。。。中间省略 。。。 successful id = 7ce275db247f498cad26ae1c589a932e successful messageId = 4999a488-0de7-4320-b6b1-46dd9bd38f2a
1.4.批量发送消息到FIFO队列
@Test
public void sendMessageBatch2fifo() {
try {
String queueUrl = "https://sqs.cn-northwest-1.amazonaws.com.cn/4203XXXX2685/queue1.fifo";
ReqSendMessageBatchBody reqSendMessageBatchBody = new ReqSendMessageBatchBody();
reqSendMessageBatchBody.setQueueUrl(queueUrl);
List<ReqSendMessageBody> list = new ArrayList<>();
for (int i = 0; i < 12; i++) {
ReqSendMessageBody item = new ReqSendMessageBody();
String iStr = String.valueOf(i);
item.setId(iStr);
item.setMessageBody(iStr);
item.setMessageGroupId("default");
item.setMessageDeduplicationId(iStr);
list.add(item);
}
reqSendMessageBatchBody.setSendMessageList(list);
RespSendMessageBatchBody response = this.sqsApiSdk.getMessageApi().sendMessageBatch(reqSendMessageBatchBody);
System.out.println("failed size = " + response.getFailedList().size());
for (BatchResultErrorEntry batchResultErrorEntry : response.getFailedList()) {
System.out.println("failed id = " + batchResultErrorEntry.id());
System.out.println("failed code = " + batchResultErrorEntry.code());
System.out.println("failed message = " + batchResultErrorEntry.message());
System.out.println("failed senderFault = " + batchResultErrorEntry.senderFault());
}
System.out.println("successful size = " + response.getSuccessfulList().size());
for (SendMessageBatchResultEntry sendMessageBatchResultEntry : response.getSuccessfulList()) {
System.out.println("successful id = " + sendMessageBatchResultEntry.id());
System.out.println("successful messageId = " + sendMessageBatchResultEntry.messageId());
System.out.println("successful sequenceNumber = " + sendMessageBatchResultEntry.sequenceNumber());
}
} catch (SqsException e) {
e.printStackTrace();
}
}
控制台打印: failed size = 0 successful size = 12 successful id = 0 successful messageId = 8e40a9d6-b82d-4156-ae67-b7d5d58f9460 successful sequenceNumber = 37310316626961119232 successful id = 1 successful messageId = 7d635dc9-8674-4a68-9466-7613bea1d5c0 successful sequenceNumber = 37310316626961119233 。。。中间省略 。。。 successful id = 10 successful messageId = 26d6d1f9-2650-4776-a7c5-32919b2f4b1a successful sequenceNumber = 37310316626992351232 successful id = 11 successful messageId = b095fe7a-bddd-40a4-9fa5-5a4261af6371 successful sequenceNumber = 37310316626992351233
2.检索消息
@Test
public void receiveMessage() {
try {
String queueUrl = "https://sqs.cn-northwest-1.amazonaws.com.cn/4203XXXX2685/queue2";
ReqReceiveMessageBody reqReceiveMessageBody = new ReqReceiveMessageBody();
reqReceiveMessageBody.setQueueUrl(queueUrl);
reqReceiveMessageBody.setMaxNumberOfMessages(10);
ReceiveMessageResponse response = this.sqsApiSdk.getMessageApi().receiveMessage(reqReceiveMessageBody);
System.out.println("hasMessages = " + response.hasMessages());
System.out.println("messages size = " + response.messages().size());
for (Message message : response.messages()) {
System.out.println("");
System.out.println("messageId = " + message.messageId());
System.out.println("body = " + message.body());
System.out.println("receiptHandle = " + message.receiptHandle());
}
} catch (SqsException e) {
e.printStackTrace();
}
}
控制台打印: hasMessages = true messages size = 4
messageId = 90a330cc-b7af-459e-991d-2961d62c527c body = {“title”:“body33”} receiptHandle = AQEBvi83cqdDMxLPhbjr0I7ehR7ANGAoKbL3R5emvV/wgS2tJGISLOTkw9VGehNwmmpvXdeXQ2cUb1GdM4l0boxa/v4dxW2ybFPP+nXYKkRn6LZ3e+T8N62mAOwckNEHreA3F+d6BEE3u939wUb+PZRo79cJi1Z8lSroFQQ4ZJ6F4IngLmrvQOKx81/gp2SqvFFFKKrVBAE43openFl7GNZ1QBSO8cfAFz05uNzMR126ZlsfnShuhySq0if7HizwrLrDGthOcD7migbpJkCBtO1fBQ2DHCDWcYWQA+h0sWBJZfQ1kU/szfN5EVcvEy1Ktgog2EUmmtrdD7YS63CUyZB6vwTp6Uyf/0ob+pMmUrpbwnkpaACNVwaN2d2oAjPHi+ZLhMIrHNfNs6d40XGmqqyiIw==
messageId = 1ec332e4-ebbb-40df-b95e-467a9c89d994 body = {“title”:“body444”} receiptHandle = AQEBkLUzUgStwRxUK8dNG3oKoFmca/+Xdet4fTUcQqN6Ruv2C3uPZnC7W59g+v9UBR5lhuDiuoyYILWH1pNeGnk1P83qlm1s3kaRYkk0gngjjH/PIsg1SmfasXt9Laj/yXqajdk3QA2+Qo2abYk3nMkP5gkbRXAKbmVMAmen2GxvOGnCY+sRFMy8qdOk6mOHx7/HU0AN1ZGabWT6ySo9e6KZI7dqMMkTBmBgswAGoj4JJ4uc/rVHtY1McYc4mJJ9gyKU1q3vIxLlViwFMpAJPxiD0LFWg2pLNZHhVH7jDaOPDxify0cO8OPr0+EDvs11wrIUZypWCNPFKf09AU4fOfXGnlTnXRfNG1GEYU3l/dlcI1ScsIVrT/XgkmQvrFybsDV0wAV4wxXswTqFFbojDuxNPQ==
messageId = 495daa30-3d45-41d7-967a-53b5af067f05 body = 0 receiptHandle = AQEBGiOdGapf5Sg9hWEZYSz+d2N5WLTxKD+LelWIMujMwp5LaWJgVu0goLhrPRGEnOF6V73gZiibLdmF2UC0pit3QJ2HWNu9uAiIP25k75NG02ZiXcS8gqy1k9r8HuNn20VijcfxMN2bGaidI5CoA8RM5kvvEr32kasnIkS7iPMkBnamNC6TFZesAaDUVMKfrkNN2L3tWYwd7TKXerI6547b1yzyNUihW32FYBT4l76h+0/u0yrwZOac2V6TGQ7dU1bBlAGNUqA4RTY0DR6td1DdZh5dPDYSoU/5nNv2RaHwI8pGGr7BlZuYYWhbWiZ6EvK9m4G+YxRQHGBsPa365+SfD6Ff565XDOvT8Tdt12oduwnZPVFcd8keODSGv6sTvUvIOhm8tDwiSfEaFLnm8PRarw==
messageId = 0ab00678-39ab-465b-a3e7-c993bc8e94b1 body = 1 receiptHandle = AQEBopnBcWMntuFwOAAnEyz13rYQBjNA0xey2sOwzOMLFcaE+2LoPlXXBYLSb0OIIrL2ENuTL9egpowQrnJ9g5Qk00fA7zQY3Eq2WEEYOCZ5TVfNA2cDPk+Yx5Ro9FEtBwwZIQVhsO97aFlgQZk69ujWuqNmyNds/vcNovVLCvvKzsHdyCH37y4lkrVb0pRA99wNSX2VY3X2k9y+2+5zzjKTveq0DFVyPmKo9XBu35hijQGeWCQFE78Io2PlLEEOmYWm7nyhfh2SRfOB9Qj/FDRYn7QArSZGbcwng8A0aVH2UuaTr7vo2tWVP64HnW4TGeErlQ4UjjuUkvMzveX+Ti4jgFNPPwLL+MRP29VspgfZSkujnjNj+BUJ1aZUes/edIavW1nyqYmzf7zf+6awIq3dAQ==
3.删除消息
3.1.删除单条消息
@Test
public void deleteMessage() {
try {
String queueUrl = "https://sqs.cn-northwest-1.amazonaws.com.cn/4203XXXX2685/queue2";
ReqReceiveMessageBody reqReceiveMessageBody = new ReqReceiveMessageBody();
reqReceiveMessageBody.setQueueUrl(queueUrl);
reqReceiveMessageBody.setWaitTimeSeconds(1);
reqReceiveMessageBody.setMaxNumberOfMessages(1);
ReceiveMessageResponse response = this.sqsApiSdk.getMessageApi().receiveMessage(reqReceiveMessageBody);
if (response.hasMessages()) {
for (Message message : response.messages()) {
System.out.println("");
System.out.println("messageId = " + message.messageId());
String receiptHandle = message.receiptHandle();
System.out.println("receiptHandle = " + receiptHandle);
this.sqsApiSdk.getMessageApi().deleteMessage(queueUrl, receiptHandle);
}
}
System.out.println("deleteMessage succeed.");
} catch (SqsException e) {
e.printStackTrace();
}
}
控制台打印: messageId = ed7f480d-9120-4d18-aee0-9f830618a074 receiptHandle = AQEBGCX0RgMinlXqCVzizeKa7T0pZQLmuwP6QjAHkUHKcvKJNvxYRFGgvlUImyGAFbCaommYbn8esmrkWhzArKXTXJj1qwqQHvidmwFflOONbj1DWf28MJ29fqzLIy/9IQp2us99chcAAkHFiS70vpeMJHc3hq9X1KLIbEUbI4elS6xMrtpIvzL1XFAOa6wrwq0apUtsGaaI52I9HvL5VIv790nV8/jCQgSFWq5AO0HyfJbgemui4QWK7qH60s0ITIiDjq62qYzsNIyUeGliSBqYI5orzACTgh9h1Y2Wnp9tIx8mCnhASrdG8Qi6vNixqvgZJxijGDsY3mGEzhV1ELfsBoFBFn74Z9VbMwYTgNiUvB8psP9H9DrySnBsOnSbFIZmJoGoPBJseXVWkPrLAOD8pw== deleteMessage succeed.
3.2.批量删除消息
@Test
public void deleteMessageBatch() {
try {
String queueUrl = "https://sqs.cn-northwest-1.amazonaws.com.cn/4203XXXX2685/queue2";
ReqReceiveMessageBody reqReceiveMessageBody = new ReqReceiveMessageBody();
reqReceiveMessageBody.setQueueUrl(queueUrl);
reqReceiveMessageBody.setWaitTimeSeconds(2);
reqReceiveMessageBody.setMaxNumberOfMessages(10);
ReceiveMessageResponse response = this.sqsApiSdk.getMessageApi().receiveMessage(reqReceiveMessageBody);
List<ReqDeleteMessageBatchBody> list = new ArrayList<>();
for (Message message : response.messages()) {
ReqDeleteMessageBatchBody body = new ReqDeleteMessageBatchBody()
.setId(message.messageId()).setReceiptHandle(message.receiptHandle());
list.add(body);
}
System.out.println("delete size = " + list.size());
RespDeleteMessageBatchBody deleteMessageBatchResponse = this.sqsApiSdk.getMessageApi().deleteMessageBatch(queueUrl, list);
System.out.println("deleteMessageBatch succeed.");
List<BatchResultErrorEntry> failedList = deleteMessageBatchResponse.getFailedList();
System.out.println("failedList size = " + failedList.size());
for (BatchResultErrorEntry batchResultErrorEntry : failedList) {
System.out.println("failed id = " + batchResultErrorEntry.id());
System.out.println("failed code = " + batchResultErrorEntry.code());
System.out.println("failed message = " + batchResultErrorEntry.message());
System.out.println("failed senderFault = " + batchResultErrorEntry.senderFault());
}
List<DeleteMessageBatchResultEntry> successfulList = deleteMessageBatchResponse.getSuccessfulList();
System.out.println("successfulList size = " + successfulList.size());
for (DeleteMessageBatchResultEntry deleteMessageBatchResultEntry : successfulList) {
System.out.println("delete id = " + deleteMessageBatchResultEntry.id());
}
} catch (SqsException e) {
e.printStackTrace();
}
}
控制台打印: deleteMessageBatch succeed. failedList size = 0 successfulList size = 5 delete id = 162f8ee0-a914-4a84-9426-27ecfc066596 delete id = cab709ce-e0d6-4eb9-9525-f1ce9db4057b delete id = 8be12f13-6253-4fc0-b43d-1c3963f65778 delete id = 9b64ee4b-c2f0-431e-a9d0-07e1dcd9725f delete id = 4999a488-0de7-4320-b6b1-46dd9bd38f2a
|