elasticmq icon indicating copy to clipboard operation
elasticmq copied to clipboard

Redrive Policy Example

Open harry9111985 opened this issue 7 years ago • 7 comments

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

harry9111985 avatar Jan 04 '18 06:01 harry9111985

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 [email protected] 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

adamw avatar Jan 04 '18 07:01 adamw

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

harry9111985 avatar Jan 04 '18 10:01 harry9111985

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 [email protected] 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

adamw avatar Jan 05 '18 15:01 adamw

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.

dsiegal avatar Mar 06 '18 00:03 dsiegal

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.

jaxley avatar Apr 02 '18 17:04 jaxley

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

adamw avatar Apr 02 '18 18:04 adamw

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));

jaxley avatar Apr 02 '18 21:04 jaxley