AWS.简单队列服务。访问现有 SQS 队列时引发的不存在队列异常
我对 AWS SQS 服务比较陌生。我已经编写了一些代码来包装 Amazon SQS api。
我能够使用创建的队列执行基本功能,但尽管如此(事实上我一直没有遇到任何问题地使用此代码,并且我正在创建JUnit测试作为一种形式),由于一个对我来说没有意义的错误,我未能通过JUnit测试。
我使用 AWS 管理控制台创建了一个队列名称 SerenaQForTest。当我查看 AWS 控制台时,我可以看到我创建的队列已列出。我已将队列上的权限设置为向所有人开放。我正在用Java编码。
当我尝试与队列交互时,我得到一个带有错误代码AWS的AmazonServiceException。SimpleQueueService.NonExistentQueueerror.
这是我的代码。
在朱尼特类中:
/**
* Prefix for queues used to run junit tests.
*/
private static final String TESTQ = "SerenaForTest";
/**
* Ensures that the queue exists.
*/
@Test
public void testExists() {
System.out.println("JUnit Test EXISTS.");
CloudSQS cloudsqs = new CloudSQS();
// this queue does exist and i can see it through the aws management console in sqs
assertTrue(cloudsqs.exists(TESTQ));
// this queue does not exist.
assertTrue(cloudsqs.exists("thisQDoesNotExist") == false);
}
和 exists() 定义如下:
/**
* Determines if the queue exists or not.
*
* @param qName
* , name of the queue to determine existence of.
* @return boolean, true if the queue exists; false otherwise.
*/
public boolean exists(final String qName) {
boolean retVal = false;
try {
// create a request for the url of qName
GetQueueUrlRequest getQueueUrlRequest = new GetQueueUrlRequest(qName);
String addy = sqs.getQueueUrl(getQueueUrlRequest).getQueueUrl();
System.out.println(qName + " url : " + addy);
if (addy != null) {
// get all queues on sqs
ListQueuesResult queues = sqs.listQueues();
// for each url,
for (String url : queues.getQueueUrls()) {
// System.out.println("Comparing " + addy + " and " + url);
if (url.equalsIgnoreCase(addy)) {
System.out.println("Queue exists.");
retVal = true;
break;
}
}
} else {
System.out.println("Queue " + qName + " does not exist.");
}
} catch (AmazonServiceException ase) {
System.err.println("ERR: AmazonServiceException. Error code: " + ase.getErrorCode());
} catch (AmazonClientException ace) {
System.err.println("ERR: AmazonClientException.");
ace.printStackTrace();
} catch (Exception e) {
System.err.println("ERR: Regular Old Error.");
e.printStackTrace();
}
return retVal;
}
控制台输出:
JUnit 测试存在。SerenaForTest url : https://sqs.us-west-2.amazonaws.com/079023477467/SerenaForTest Queue 存在。ERR:AmazonServiceException。错误代码:AWS。SimpleQueueService.NonExistentQueue
下面是堆栈跟踪:
AmazonServiceException: 状态代码: 400, AWS 服务: AmazonSQS, AWS 请求 ID: a2809a40-223f-5c4d-b369-d0c3301a8e4e, AWS 错误代码: AWS.SimpleQueueService.NonExistentQueue, AWS 错误消息:对于此 wsdl 版本,指定的队列不存在。at com.amazonaws.http.AmazonHttpClient.handleErrorResponse(AmazonHttpClient.java:644) at com.amazonaws.http.AmazonHttpClient.executeHelper(AmazonHttpClient.java:338) at com.amazonaws.http.AmazonHttpClient.execute(AmazonHttpClient.java:190) at com.amazonaws.services.sqs.AmazonSQSClient.invoke(AmazonSQSClient.java:875) at com.amazonaws.services.sqs.AmazonSQSClient.getQueueUrl(AmazonSQSClient.java:364) atcom.tutelatechnologies.SQLiteConverter.cloud.CloudSQS.exists(CloudSQS.java:301) at com.tutelatechnologies.SQLiteConverter.cloud.CloudSQSTest.testExists(CloudSQSTest.java:169) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.nativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.delegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:601) atorg.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20) at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28) atorg.junit.internal.runners.statement.RunAfters.evaluate(RunAfters.java:30) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60) atorg.junit.runners.ParentRunner.runChildren(ParentRunner.java:229) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222) at org.junit.internal.runners.statement.RunBefores.evaluate(RunBefores.java:28) at org.junit.internal.runners.statement.RunAfters.evaluate(RunAfters.java:30) at org.junit.runners.ParentRunner.run(ParentRunner.java:300) atorg.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) atorg.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
从中您可以看到该函数能够获取队列URL并找到匹配项。但它仍然会引发异常。
任何人都知道为什么会发生这种情况?每次我需要从队列中抛出一些东西或从队列中删除一些东西时,我都会调用 exists(),所以它实际上没有通过我所有的 JUnit 测试,但出于同样的原因。
提前致谢!!!