15

I am relatively new to AWS SQS services. I have written some code to wrap the Amazon SQS api.

I am able perform basic functionality with created queues, but despite that (in fact I have been using this code for ever with no problem, and I am creating JUnit tests as a formality), I am failing my JUnit test because of an error that makes little sense to me.

I have created a queue names SerenaQForTest using the AWS Management console. When I look at the AWS Console I can see that the queue I have created is listed. I have set the permissions on the queue to open for everyone. I am coding in Java.

When I try to interact with the queue, I get an AmazonServiceException with error code AWS.SimpleQueueService.NonExistentQueueerror.

Here is my code.

In the Junit Class:

/**
 * 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);
}

and exists() is defined as follows:

/**
 * 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;
}

Console Output:


JUnit Test EXISTS. SerenaForTest url : https://sqs.us-west-2.amazonaws.com/079023477467/SerenaForTest Queue exists. ERR: AmazonServiceException. Error code: AWS.SimpleQueueService.NonExistentQueue

Here is the stacktrace:

AmazonServiceException: Status Code: 400, AWS Service: AmazonSQS, AWS Request ID: a2809a40-223f-5c4d-b369-d0c3301a8e4e, AWS Error Code: AWS.SimpleQueueService.NonExistentQueue, AWS Error Message: The specified queue does not exist for this wsdl version. 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) at com.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) at org.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) at org.junit.internal.runners.statements.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) at org.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.statements.RunBefores.evaluate(RunBefores.java:28) at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:30) at org.junit.runners.ParentRunner.run(ParentRunner.java:300) at org.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) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

From that you can see that the function is able to grab the queue URL and that a match is found. But it still throws an exception.

Any one have any ideas why this is happening? I call exists() every time I need to throw something on or take something off of the queue so its actually failing all of my JUnit tests but for the same reasons.

Thanks in advance!!!

miss.serena
  • 1,170
  • 1
  • 11
  • 28

3 Answers3

8

Are you sure that the queue you created is in the same region that your Java SQS client is going to? The default region in the AWS SDK for Java is US-East-1. You can verify your queue's region by looking at the management console in the top right.

Wade Matveyenko
  • 4,290
  • 1
  • 23
  • 27
  • thanks for your response, Wade. I just double checked and the url for my queue as shown in the AWS Management console is in sqs.us-west-2.amazonaws.com. The clients is also set up for region west-2. Shucks. – miss.serena Aug 12 '13 at 23:02
  • How to change the default of java SQS? I'm creating an app in Android and it works pretty well on us-east-1 but not on other region.. – mboy Dec 31 '15 at 03:59
  • I figured it out. You need to set your endpoint `sqsClient.setEndpoint("sqs.ap-northeast-1.amazonaws.com");` – mboy Dec 31 '15 at 04:22
6

Stumbled on the same problem. Solution is pretty simple after reading the Java docs more carefully :) Simply set client.setEndPoint(...) when creating your SQSClient

sqsClient = new AmazonSQSClient( credentials );
sqsClient.setEndpoint("sqs.eu-west-1.amazonaws.com");

Endpoint values found at AWS Link

Srikar Appalaraju
  • 71,928
  • 54
  • 216
  • 264
1

I believe that listQueues() will fail if you are using credentials that don't have permission for all of the queues (at least this was an issue at some point).

Rob
  • 6,247
  • 2
  • 25
  • 33
  • Thanks for the responding, Rob. The thing is that I am actually able to print out the listQueuesResult object returned from .listQueues(). Is it possible that it would return the correct listQueueResult and still throw an exception? Sorry to pester if that is a silly question. Im a total n00b with AWS SQS. – miss.serena Aug 11 '13 at 05:40
  • You should print the stack trace and look at exactly which call is causing the exception. If you add the stack trace to the question above, I can help you try to figure it out. – Rob Aug 11 '13 at 16:32
  • Updated with stacktrace. Thanks Rob. The stack track literally tells you that the queue I am requesting does not exist (for this wsld version. I am not sure what this means really but I did find this on the AWS forum: https://forums.aws.amazon.com/message.jspa?messageID=107862 However, I am using a queue that I made via AWS Management console under a week ago). I hope the stacktrace gets us a step close to solving the problem. Thanks again! – miss.serena Aug 12 '13 at 16:59
  • A call at CloudSQS.java line 301, you are calling getQueueUrl() and it is failing. I suggest that you use a debugger or print statements to figure out exactly which queue is causing this and investigating that. – Rob Aug 12 '13 at 21:08