software.amazon.awssdk.services.sqs.model.CreateQueueRequest Java Examples

The following examples show how to use software.amazon.awssdk.services.sqs.model.CreateQueueRequest. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SQSExample.java    From aws-doc-sdk-examples with Apache License 2.0 6 votes vote down vote up
public static String createQueue(SqsClient sqsClient,String queueName ) {

        System.out.println("\nCreate queue");
        // snippet-start:[sqs.java2.sqs_example.create_queue]

        CreateQueueRequest createQueueRequest = CreateQueueRequest.builder()
                .queueName(queueName)
                .build();

        sqsClient.createQueue(createQueueRequest);
        // snippet-end:[sqs.java2.sqs_example.create_queue]

        System.out.println("\nGet queue URL");
        // snippet-start:[sqs.java2.sqs_example.get_queue]
        GetQueueUrlResponse getQueueUrlResponse =
                sqsClient.getQueueUrl(GetQueueUrlRequest.builder().queueName(queueName).build());
        String queueUrl = getQueueUrlResponse.queueUrl();
        return queueUrl;
        // snippet-end:[sqs.java2.sqs_example.get_queue]
    }
 
Example #2
Source File: SendMessages.java    From aws-doc-sdk-examples with Apache License 2.0 6 votes vote down vote up
public static void sendMessage(SqsClient sqsClient, String queueName, String message) {

        try {
            CreateQueueRequest request = CreateQueueRequest.builder()
                    .queueName(queueName)
                    .build();
            CreateQueueResponse createResult = sqsClient.createQueue(request);

            GetQueueUrlRequest getQueueRequest = GetQueueUrlRequest.builder()
                .queueName(queueName)
                .build();

            String queueUrl = sqsClient.getQueueUrl(getQueueRequest).queueUrl();

            SendMessageRequest sendMsgRequest = SendMessageRequest.builder()
                .queueUrl(queueUrl)
                .messageBody(message)
                .delaySeconds(5)
                .build();
            sqsClient.sendMessage(sendMsgRequest);

        } catch (QueueNameExistsException e) {
            throw e;
        }
    }
 
Example #3
Source File: EmbeddedSqsServer.java    From beam with Apache License 2.0 6 votes vote down vote up
static void start() {
  sqsRestServer = SQSRestServerBuilder.withPort(port).start();

  client =
      SqsClient.builder()
          .credentialsProvider(
              StaticCredentialsProvider.create(AwsBasicCredentials.create("x", "x")))
          .endpointOverride(URI.create(endPoint))
          .region(Region.US_WEST_2)
          .build();

  CreateQueueRequest createQueueRequest =
      CreateQueueRequest.builder().queueName(queueName).build();
  final CreateQueueResponse queue = client.createQueue(createQueueRequest);
  queueUrl = queue.queueUrl();
}
 
Example #4
Source File: SqsPolicyIntegrationTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
/**
 * Tests that the SQS specific access control policy code works as expected.
 */
@Test
public void testPolicies() throws Exception {
    String queueName = getUniqueQueueName();
    queueUrl = sqsSync.createQueue(CreateQueueRequest.builder().queueName(queueName).build()).queueUrl();

    Policy policy = new Policy().withStatements(new Statement(Effect.Allow).withPrincipals(Principal.ALL_USERS)
                                                                           .withActions(new Action("sqs:SendMessage"), new Action("sqs:ReceiveMessage"))
                                                                           .withResources(new SqsQueueResource(ACCOUNT_ID, queueName))
                                                                           .withConditions(new DateCondition(DateComparisonType.DateLessThan,
                    new Date())));
    setQueuePolicy(policy);
}
 
Example #5
Source File: LongPolling.java    From aws-doc-sdk-examples with Apache License 2.0 5 votes vote down vote up
public static void setLongPoll( SqsClient sqsClient) {

        // Enable long polling when creating a queue
        HashMap<QueueAttributeName, String> attributes = new HashMap<QueueAttributeName, String>();
        attributes.put(QueueAttributeName.RECEIVE_MESSAGE_WAIT_TIME_SECONDS, "20");

        CreateQueueRequest createRequest = CreateQueueRequest.builder()
                .queueName(QueueName)
                .attributes(attributes)
                .build();

        try {
            sqsClient.createQueue(createRequest);

            GetQueueUrlRequest getQueueRequest = GetQueueUrlRequest.builder()
                .queueName(QueueName)
                .build();

            String queueUrl = sqsClient.getQueueUrl(getQueueRequest).queueUrl();

           // Enable long polling on an existing queue
           SetQueueAttributesRequest setAttrsRequest = SetQueueAttributesRequest.builder()
                .queueUrl(queueUrl)
                .attributes(attributes)
                .build();

           sqsClient.setQueueAttributes(setAttrsRequest);

            // Enable long polling on a message receipt
            ReceiveMessageRequest receiveRequest = ReceiveMessageRequest.builder()
                .queueUrl(queueUrl)
                .waitTimeSeconds(20)
                .build();

            sqsClient.receiveMessage(receiveRequest);

        } catch (QueueNameExistsException e) {
            throw e;
        }
    }
 
Example #6
Source File: VisibilityTimeout.java    From aws-doc-sdk-examples with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) {
    final String queueName = "testQueue" + new Date().getTime();
    SqsClient sqs = SqsClient.builder().build();

    // First, create a queue (unless it already exists)
    CreateQueueRequest createRequest = CreateQueueRequest.builder()
            .queueName(queueName)
            .build();
    try {
        CreateQueueResponse cqResult = sqs.createQueue(createRequest);
    } catch (QueueNameExistsException e) {
        throw e;
    }

    GetQueueUrlRequest getRequest = GetQueueUrlRequest.builder()
            .queueName(queueName)
            .build();
    final String queue_url = sqs.getQueueUrl(getRequest).queueUrl();

    // Send some messages to the queue
    for (int i = 0; i < 20; i++) {
        SendMessageRequest sendRequest = SendMessageRequest.builder()
                .queueUrl(queueName)
                .messageBody("This is message " + i)
                .build();
        sqs.sendMessage(sendRequest);
    }

    // Change visibility timeout (single)
    changeMessageVisibilitySingle(queueName, 3600);

    // Change visibility timeout (multiple)
    changeMessageVisibilityMultiple(queueName, 2000);
}
 
Example #7
Source File: SendReceiveMessages.java    From aws-doc-sdk-examples with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) {

        SqsClient sqsClient = SqsClient.builder()
                .region(Region.US_WEST_2)
                .build();

        try {
            CreateQueueRequest request = CreateQueueRequest.builder()
                    .queueName(QUEUE_NAME)
                    .build();
            CreateQueueResponse createResult = sqsClient.createQueue(request);

            GetQueueUrlRequest getQueueRequest = GetQueueUrlRequest.builder()
                .queueName(QUEUE_NAME)
                .build();

            String queueUrl = sqsClient.getQueueUrl(getQueueRequest).queueUrl();

            SendMessageRequest sendMsgRequest = SendMessageRequest.builder()
                .queueUrl(queueUrl)
                .messageBody("hello world")
                .delaySeconds(5)
                .build();
            sqsClient.sendMessage(sendMsgRequest);

             // Send multiple messages to the queue
            SendMessageBatchRequest sendBatchRequest = SendMessageBatchRequest.builder()
                .queueUrl(queueUrl)
                .entries(
                        SendMessageBatchRequestEntry.builder()
                                .messageBody("Hello from message 1")
                                .id("msg_1")
                                .build()
                        ,
                        SendMessageBatchRequestEntry.builder()
                                .messageBody("Hello from message 2")
                                .delaySeconds(10)
                                .id("msg_2")
                                .build())
                .build();
             sqsClient.sendMessageBatch(sendBatchRequest);

            // Receive messages from the queue
            ReceiveMessageRequest receiveRequest = ReceiveMessageRequest.builder()
                .queueUrl(queueUrl)
                .build();
            List<Message> messages = sqsClient.receiveMessage(receiveRequest).messages();

            // Print out the messages
             for (Message m : messages) {
                System.out.println("\n" +m.body());
            }
        } catch (QueueNameExistsException e) {
            throw e;
        }
    }
 
Example #8
Source File: IntegrationTestBase.java    From aws-sdk-java-v2 with Apache License 2.0 3 votes vote down vote up
/**
 * Helper method to create a SQS queue with a unique name
 *
 * @return The queue url for the created queue
 */
protected String createQueue(SqsAsyncClient sqsClient) {
    CreateQueueResponse res = sqsClient.createQueue(CreateQueueRequest.builder().queueName(getUniqueQueueName()).build()).join();
    return res.queueUrl();
}