softwaremill / elasticmq

In-memory message queue with an Amazon SQS-compatible interface. Runs stand-alone or embedded.
https://softwaremill.com/open-source/
Apache License 2.0
2.53k stars 193 forks source link

Redrive Policy Example #124

Open harry9111985 opened 6 years ago

harry9111985 commented 6 years ago

Hi @adamw ,

If you have any samples to create redrivepolicy links using dead queues that would be great . Currently , I am trying to create a redrivepolicy during CreateQueueRequest (Amazon Java SDK) using queue attributes. I do get a debug point on the RedrivePolicy.class in the elasticmq package . But elasticmq doesnt seem to create a link with the DeadLetterQueue.

My java code :

final Map<String, String> deadLetterQueueAttrs = new HashMap<>(); deadLetterQueueAttrs.put("VisibilityTimeout", "600"); final CreateQueueRequest createDeadLetterQueueRequest = new CreateQueueRequest() .withQueueName(queueName + "-error") .withAttributes(deadLetterQueueAttrs);

    final CreateQueueResult deadLetterQueueResult = amazonSQSAsyncClient.createQueue(createDeadLetterQueueRequest);
    final GetQueueAttributesResult deadLetterQueueArnResult = amazonSQSAsyncClient.getQueueAttributes(deadLetterQueueResult.getQueueUrl(), Arrays.asList("QueueArn"));

    final Map<String, String> queueAttributes = new HashMap<>();
    final StringBuilder redrivePolicyParamBuilder = new StringBuilder()
        .append("{\"deadLetterTargetArn\" : \"")
        .append(deadLetterQueueArnResult.getAttributes().get("QueueArn"))
        .append("\",")
        .append("\"maxReceiveCount\": \"1\"}");
    queueAttributes.put("RedrivePolicy", redrivePolicyParamBuilder.toString());
    queueAttributes.put("VisibilityTimeout","600");

    final CreateQueueRequest createQueueRequest = new CreateQueueRequest()
        .withQueueName(queueName)
        .withAttributes(queueAttributes);

    final CreateQueueResult createQueueResult = amazonSQSAsyncClient.createQueue(createQueueRequest);

No errors are thrown but the redrive policy link is not created,

Kindly help.

Regards, Harish

adamw commented 6 years ago

Hello,

here's a test which uses a redrive policy:

https://github.com/adamw/elasticmq/blob/master/rest/rest-sqs-testing-amazon-java-sdk/src/test/scala/org/elasticmq/rest/sqs/AmazonJavaSdkTestSuite.scala#L944-L960

Maybe it will help?

Adam

On 4 Jan 2018, at 07:10, harry9111985 notifications@github.com wrote:

Hi adamw,

If you have any samples to create redrivepolicy links using dead queues that would be great . Currently , I am trying to create a redrivepolicy during CreateQueueRequest (Amazon Java SDK) using queue attributes. I do get a debug point on the RedrivePolicy.class in the elasticmq package . But elasticmq doesnt seem to create a link with the DeadLetterQueue.

My java code :

final Map<String, String> deadLetterQueueAttrs = new HashMap<>(); deadLetterQueueAttrs.put("VisibilityTimeout", "600"); final CreateQueueRequest createDeadLetterQueueRequest = new CreateQueueRequest() .withQueueName(queueName + "-error") .withAttributes(deadLetterQueueAttrs);

final CreateQueueResult deadLetterQueueResult = amazonSQSAsyncClient.createQueue(createDeadLetterQueueRequest); final GetQueueAttributesResult deadLetterQueueArnResult = amazonSQSAsyncClient.getQueueAttributes(deadLetterQueueResult.getQueueUrl(), Arrays.asList("QueueArn"));

final Map<String, String> queueAttributes = new HashMap<>(); final StringBuilder redrivePolicyParamBuilder = new StringBuilder() .append("{\"deadLetterTargetArn\" : \"") .append(deadLetterQueueArnResult.getAttributes().get("QueueArn")) .append("\",") .append("\"maxReceiveCount\": \"1\"}"); queueAttributes.put("RedrivePolicy", redrivePolicyParamBuilder.toString()); queueAttributes.put("VisibilityTimeout","600");

final CreateQueueRequest createQueueRequest = new CreateQueueRequest() .withQueueName(queueName) .withAttributes(queueAttributes);

final CreateQueueResult createQueueResult = amazonSQSAsyncClient.createQueue(createQueueRequest);

No errors are thrown but the redrive policy link is not created,

Kindly help.

Regards, Harish

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub, or mute the thread.

-- Adam Warski http://www.softwaremill.com http://twitter.com/#!/adamwarski

harry9111985 commented 6 years ago

Hi @adamw ,

Thanks for your response. I am not sure that piece of code works in Java . I am doing the same thing in Java as mentioned in the above snippet .

I get the following CreateRequest in the logs

Creating queue QueueData(test-api-log,MillisVisibilityTimeout(600000),PT0S,PT0S,2018-01-04T20:31:29.425+10:00,2018-01-04T20:31:29.425+10:00,Some(DeadLettersQueueData(test-api-log-error,1)),None)

But when I get "All" Attributes for the queue I get the following

Queue Attributes : {"attributes":{"ReceiveMessageWaitTimeSeconds":"0","ApproximateNumberOfMessagesDelayed":"0","CreatedTimestamp":"1515061889","DelaySeconds":"0","VisibilityTimeout":"600","ApproximateNumberOfMessages":"0","ApproximateNumberOfMessagesNotVisible":"0","LastModifiedTimestamp":"1515061889","QueueArn":"arn:aws:sqs:elasticmq:000000000000:test-api-log"}}

Can you spot any errors ?

Regards, Harish

adamw commented 6 years ago

True, it looks the same ... however, I noticed the test doesn't really check the results, so it's incomplete - maybe there's a bug in there. I'd have to check, but no time right now unfortunately - sorry!.

Adam

On 4 Jan 2018, at 11:35, harry9111985 notifications@github.com wrote:

Hi Adam,

Thanks for your response. I am not sure that piece of code works in Java . I am doing the same thing in Java as mentioned in the above snippet .

I get the following CreateRequest in the logs

Creating queue QueueData(test-api-log,MillisVisibilityTimeout(600000),PT0S,PT0S,2018-01-04T20:31:29.425+10:00,2018-01-04T20:31:29.425+10:00,Some(DeadLettersQueueData(test-api-log-error,1)),None)

But when I get "All" Attributes for the queue I get the following

Queue Attributes : {"attributes":{"ReceiveMessageWaitTimeSeconds":"0","ApproximateNumberOfMessagesDelayed":"0","CreatedTimestamp":"1515061889","DelaySeconds":"0","VisibilityTimeout":"600","ApproximateNumberOfMessages":"0","ApproximateNumberOfMessagesNotVisible":"0","LastModifiedTimestamp":"1515061889","QueueArn":"arn:aws:sqs:elasticmq:000000000000:test-api-log"}}

Can you spot any errors ?

Regards, Harish

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or mute the thread.

-- Adam Warski http://www.softwaremill.com http://twitter.com/#!/adamwarski

dsiegal commented 6 years ago

I just ran into the same issue. If I create a queue with a redrive policy and then query its attributes, the redrive policy is not among them. This differs from AWS. However the good news is that in my own testing, the redrive policy is actually in effect; messages are failing over after the maxReceiveCount number of receives. But it would be nice to query a queue regarding its redrive policy. Thanks.

jaxley commented 6 years ago

When is 0.13.9 set to release? I see the standalone version is up on S3 and linked to from this repository, but there isn't any official release to the maven repos yet.

adamw commented 6 years ago

@jaxley sorry, my bad - somehow this step slipped :) Should be in central in ~15 minutes!

jaxley commented 6 years ago

Thanks. But there's at least one bug in the code. RedrivePolicy is still listed as an unsupported attribute name.

object UnsupportedAttributeNames {
    val PolicyAttribute = "Policy"
    val MaximumMessageSizeAttribute = "MaximumMessageSize"
    val MessageRetentionPeriodAttribute = "MessageRetentionPeriod"
    val RedrivePolicyAttribute = "RedrivePolicy"

    val AllUnsupportedAttributeNames = PolicyAttribute :: MaximumMessageSizeAttribute ::
      MessageRetentionPeriodAttribute :: RedrivePolicyAttribute :: Nil
  }

This code triggers that, so should probably be mirrored in one of the unit tests to make sure it works properly:

final SetQueueAttributesRequest queueAttributesRequest = new SetQueueAttributesRequest()
                    .withQueueUrl(client.getQueueUrl("my-listener")
                            .getQueueUrl())
                    .addAttributesEntry("RedrivePolicy", JSON.toString(redriveSettings));