use of com.amazonaws.services.sqs.model.AmazonSQSException in project aws-doc-sdk-examples by awsdocs.
the class UsingQueues method main.
public static void main(String[] args) {
AmazonSQS sqs = AmazonSQSClientBuilder.defaultClient();
// Creating a Queue
CreateQueueRequest create_request = new CreateQueueRequest(QUEUE_NAME).addAttributesEntry("DelaySeconds", "60").addAttributesEntry("MessageRetentionPeriod", "86400");
try {
sqs.createQueue(create_request);
} catch (AmazonSQSException e) {
if (!e.getErrorCode().equals("QueueAlreadyExists")) {
throw e;
}
}
// Get the URL for a queue
String queue_url = sqs.getQueueUrl(QUEUE_NAME).getQueueUrl();
// Delete the Queue
sqs.deleteQueue(queue_url);
sqs.createQueue("Queue1" + new Date().getTime());
sqs.createQueue("Queue2" + new Date().getTime());
sqs.createQueue("MyQueue" + new Date().getTime());
// List your queues
ListQueuesResult lq_result = sqs.listQueues();
System.out.println("Your SQS Queue URLs:");
for (String url : lq_result.getQueueUrls()) {
System.out.println(url);
}
// List queues with filters
String name_prefix = "Queue";
lq_result = sqs.listQueues(new ListQueuesRequest(name_prefix));
System.out.println("Queue URLs with prefix: " + name_prefix);
for (String url : lq_result.getQueueUrls()) {
System.out.println(url);
}
}
use of com.amazonaws.services.sqs.model.AmazonSQSException in project aws-doc-sdk-examples by awsdocs.
the class VisibilityTimeout method main.
public static void main(String[] args) {
final String queue_name = "testQueue" + new Date().getTime();
AmazonSQS sqs = AmazonSQSClientBuilder.defaultClient();
// first, create a queue (unless it exists already)
try {
CreateQueueResult cq_result = sqs.createQueue(queue_name);
} catch (AmazonSQSException e) {
if (!e.getErrorCode().equals("QueueAlreadyExists")) {
throw e;
}
}
final String queue_url = sqs.getQueueUrl(queue_name).getQueueUrl();
// Send some messages to the queue
for (int i = 0; i < 20; i++) {
sqs.sendMessage(queue_url, "This is message " + i);
}
// change visibility timeout (single)
changeMessageVisibilitySingle(queue_url, 3600);
// change visibility timeout (multiple)
changeMessageVisibilityMultiple(queue_url, 2000);
}
use of com.amazonaws.services.sqs.model.AmazonSQSException in project aws-doc-sdk-examples by awsdocs.
the class DeadLetterQueues method main.
public static void main(String[] args) {
if (args.length != 2) {
System.out.println("Usage: DeadLetterQueues <src_queue_name> <dl_queue_name>");
System.exit(1);
}
String src_queue_name = args[0];
String dl_queue_name = args[1];
final AmazonSQS sqs = AmazonSQSClientBuilder.defaultClient();
// Create source queue
try {
sqs.createQueue(src_queue_name);
} catch (AmazonSQSException e) {
if (!e.getErrorCode().equals("QueueAlreadyExists")) {
throw e;
}
}
// Create dead-letter queue
try {
sqs.createQueue(dl_queue_name);
} catch (AmazonSQSException e) {
if (!e.getErrorCode().equals("QueueAlreadyExists")) {
throw e;
}
}
// Get dead-letter queue ARN
String dl_queue_url = sqs.getQueueUrl(dl_queue_name).getQueueUrl();
GetQueueAttributesResult queue_attrs = sqs.getQueueAttributes(new GetQueueAttributesRequest(dl_queue_url).withAttributeNames("QueueArn"));
String dl_queue_arn = queue_attrs.getAttributes().get("QueueArn");
// Set dead letter queue with redrive policy on source queue.
String src_queue_url = sqs.getQueueUrl(src_queue_name).getQueueUrl();
SetQueueAttributesRequest request = new SetQueueAttributesRequest().withQueueUrl(src_queue_url).addAttributesEntry("RedrivePolicy", "{\"maxReceiveCount\":\"5\", \"deadLetterTargetArn\":\"" + dl_queue_arn + "\"}");
sqs.setQueueAttributes(request);
}
use of com.amazonaws.services.sqs.model.AmazonSQSException in project aws-doc-sdk-examples by awsdocs.
the class LongPolling method main.
public static void main(String[] args) {
final String USAGE = "To run this example, supply the name of a queue to create and\n" + "queue url of an existing queue.\n\n" + "Ex: LongPolling <unique-queue-name> <existing-queue-url>\n";
if (args.length != 2) {
System.out.println(USAGE);
System.exit(1);
}
String queue_name = args[0];
String queue_url = args[1];
final AmazonSQS sqs = AmazonSQSClientBuilder.defaultClient();
// Enable long polling when creating a queue
CreateQueueRequest create_request = new CreateQueueRequest().withQueueName(queue_name).addAttributesEntry("ReceiveMessageWaitTimeSeconds", "20");
try {
sqs.createQueue(create_request);
} catch (AmazonSQSException e) {
if (!e.getErrorCode().equals("QueueAlreadyExists")) {
throw e;
}
}
// Enable long polling on an existing queue
SetQueueAttributesRequest set_attrs_request = new SetQueueAttributesRequest().withQueueUrl(queue_url).addAttributesEntry("ReceiveMessageWaitTimeSeconds", "20");
sqs.setQueueAttributes(set_attrs_request);
// Enable long polling on a message receipt
ReceiveMessageRequest receive_request = new ReceiveMessageRequest().withQueueUrl(queue_url).withWaitTimeSeconds(20);
sqs.receiveMessage(receive_request);
}
use of com.amazonaws.services.sqs.model.AmazonSQSException in project aws-doc-sdk-examples by awsdocs.
the class SendReceiveMessages method main.
public static void main(String[] args) {
final AmazonSQS sqs = AmazonSQSClientBuilder.defaultClient();
try {
CreateQueueResult create_result = sqs.createQueue(QUEUE_NAME);
} catch (AmazonSQSException e) {
if (!e.getErrorCode().equals("QueueAlreadyExists")) {
throw e;
}
}
String queueUrl = sqs.getQueueUrl(QUEUE_NAME).getQueueUrl();
SendMessageRequest send_msg_request = new SendMessageRequest().withQueueUrl(queueUrl).withMessageBody("hello world").withDelaySeconds(5);
sqs.sendMessage(send_msg_request);
// Send multiple messages to the queue
SendMessageBatchRequest send_batch_request = new SendMessageBatchRequest().withQueueUrl(queueUrl).withEntries(new SendMessageBatchRequestEntry("msg_1", "Hello from message 1"), new SendMessageBatchRequestEntry("msg_2", "Hello from message 2").withDelaySeconds(10));
sqs.sendMessageBatch(send_batch_request);
// receive messages from the queue
List<Message> messages = sqs.receiveMessage(queueUrl).getMessages();
// delete messages from the queue
for (Message m : messages) {
sqs.deleteMessage(queueUrl, m.getReceiptHandle());
}
}
Aggregations