Java Code Examples for com.amazonaws.services.sqs.model.CreateQueueResult#getQueueUrl()

The following examples show how to use com.amazonaws.services.sqs.model.CreateQueueResult#getQueueUrl() . 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: SqsIOTest.java    From beam with Apache License 2.0 6 votes vote down vote up
@Override
protected void before() {
  sqsRestServer = SQSRestServerBuilder.start();

  String endpoint = "http://localhost:9324";
  String region = "elasticmq";
  String accessKey = "x";
  String secretKey = "x";

  client =
      AmazonSQSClientBuilder.standard()
          .withCredentials(
              new AWSStaticCredentialsProvider(new BasicAWSCredentials(accessKey, secretKey)))
          .withEndpointConfiguration(
              new AwsClientBuilder.EndpointConfiguration(endpoint, region))
          .build();
  final CreateQueueResult queue = client.createQueue("test");
  queueUrl = queue.getQueueUrl();
}
 
Example 2
Source File: LocalstackContainerTest.java    From testcontainers-java with MIT License 6 votes vote down vote up
@Test
public void sqsTestOverBridgeNetwork() {
    AmazonSQS sqs = AmazonSQSClientBuilder.standard()
        .withEndpointConfiguration(localstack.getEndpointConfiguration(SQS))
        .withCredentials(localstack.getDefaultCredentialsProvider())
        .build();

    CreateQueueResult queueResult = sqs.createQueue("baz");
    String fooQueueUrl = queueResult.getQueueUrl();
    assertThat("Created queue has external hostname URL", fooQueueUrl,
        containsString("http://" + DockerClientFactory.instance().dockerHostIpAddress() + ":" + localstack.getMappedPort(SQS.getPort())));

    sqs.sendMessage(fooQueueUrl, "test");
    final long messageCount = sqs.receiveMessage(fooQueueUrl).getMessages().stream()
        .filter(message -> message.getBody().equals("test"))
        .count();
    assertEquals("the sent message can be received", 1L, messageCount);
}
 
Example 3
Source File: AmazonSQSIdleQueueDeletingClient.java    From amazon-sqs-java-temporary-queues-client with Apache License 2.0 5 votes vote down vote up
@Override
public CreateQueueResult createQueue(CreateQueueRequest request) {
    Map<String, String> attributes = new HashMap<>(request.getAttributes());
    Optional<Long> retentionPeriod = getRetentionPeriod(attributes);
    if (!retentionPeriod.isPresent()) {
        return super.createQueue(request);
    }

    String queueName = request.getQueueName();
    if (!queueName.startsWith(queueNamePrefix)) {
        throw new IllegalArgumentException();
    }

    CreateQueueRequest superRequest = request.clone()
            .withQueueName(queueName)
            .withAttributes(attributes);

    CreateQueueResult result = super.createQueue(superRequest);
    String queueUrl = result.getQueueUrl();

    String retentionPeriodString = retentionPeriod.get().toString();
    amazonSqsToBeExtended.tagQueue(queueUrl,
            Collections.singletonMap(IDLE_QUEUE_RETENTION_PERIOD_TAG, retentionPeriodString));

    // TODO-RS: Filter more carefully to all attributes valid for createQueue 
    List<String> attributeNames = Arrays.asList(QueueAttributeName.ReceiveMessageWaitTimeSeconds.toString(),
                                                QueueAttributeName.VisibilityTimeout.toString());
    Map<String, String> createdAttributes = amazonSqsToBeExtended.getQueueAttributes(queueUrl, attributeNames).getAttributes();
    createdAttributes.put(IDLE_QUEUE_RETENTION_PERIOD, retentionPeriodString);

    QueueMetadata metadata = new QueueMetadata(queueName, queueUrl, createdAttributes);
    queues.put(queueUrl, metadata);

    metadata.heartbeater = executor.scheduleAtFixedRate(() -> heartbeatToQueue(queueUrl), 
            0, heartbeatIntervalSeconds, TimeUnit.SECONDS);

    return result;
}
 
Example 4
Source File: SQSObservableQueue.java    From conductor with Apache License 2.0 5 votes vote down vote up
@VisibleForTesting
String getOrCreateQueue() {
       List<String> queueUrls = listQueues(queueName);
	if (queueUrls == null || queueUrls.isEmpty()) {
           CreateQueueRequest createQueueRequest = new CreateQueueRequest().withQueueName(queueName);
           CreateQueueResult result = client.createQueue(createQueueRequest);
           return result.getQueueUrl();
	} else {
           return queueUrls.get(0);
       }
   }
 
Example 5
Source File: ApplicationTest.java    From examples with Apache License 2.0 5 votes vote down vote up
private void writeMsg(String[] msgs)
{
  CreateQueueResult res = sqs.createQueue(currentQueueName);

  currentQueueUrl = res.getQueueUrl();

  // we should purge the queue first
  PurgeQueueRequest purgeReq = new PurgeQueueRequest(currentQueueUrl);
  sqs.purgeQueue(purgeReq);
  for (String text : msgs) {
    sqs.sendMessage(currentQueueUrl, text);
  }
}
 
Example 6
Source File: SQSTestBase.java    From attic-apex-malhar with Apache License 2.0 5 votes vote down vote up
public void produceMsg(String[] msgs, boolean purgeFirst) throws Exception
{
  CreateQueueResult res = sqs.createQueue(getCurrentQueueName());
  if (purgeFirst) {
    PurgeQueueRequest purgeReq = new PurgeQueueRequest(res.getQueueUrl());
    sqs.purgeQueue(purgeReq);
  }
  for (String text : msgs) {
    sqs.sendMessage(res.getQueueUrl(), text);
  }
}
 
Example 7
Source File: AwsGlacierInventoryRetriever.java    From core with GNU General Public License v3.0 5 votes vote down vote up
/**
 * For retrieving vault inventory. For initializing SQS for determining when
 * job completed. Does nothing if member snsTopicName is null. Sets members
 * sqsQueueURL, sqsQueueARN, and sqsClient.
 */
   private void setupSQS() {
	// If no sqsQueueName setup then simply return
	if (sqsQueueName == null)
		return;

	CreateQueueRequest request = new CreateQueueRequest()
			.withQueueName(sqsQueueName);
	CreateQueueResult result = sqsClient.createQueue(request);
	sqsQueueURL = result.getQueueUrl();

	GetQueueAttributesRequest qRequest = new GetQueueAttributesRequest()
			.withQueueUrl(sqsQueueURL).withAttributeNames("QueueArn");

	GetQueueAttributesResult qResult = sqsClient
			.getQueueAttributes(qRequest);
	sqsQueueARN = qResult.getAttributes().get("QueueArn");

	Policy sqsPolicy = new Policy().withStatements(new Statement(
			Effect.Allow).withPrincipals(Principal.AllUsers)
			.withActions(SQSActions.SendMessage)
			.withResources(new Resource(sqsQueueARN)));
	Map<String, String> queueAttributes = new HashMap<String, String>();
	queueAttributes.put("Policy", sqsPolicy.toJson());
	sqsClient.setQueueAttributes(new SetQueueAttributesRequest(sqsQueueURL,
			queueAttributes));
}
 
Example 8
Source File: DynamicQueueUrlDestinationResolver.java    From spring-cloud-aws with Apache License 2.0 5 votes vote down vote up
@Override
public String resolveDestination(String name) throws DestinationResolutionException {
	String queueName = name;

	if (this.resourceIdResolver != null) {
		queueName = this.resourceIdResolver.resolveToPhysicalResourceId(name);
	}

	if (isValidQueueUrl(queueName)) {
		return queueName;
	}

	if (this.autoCreate) {
		// Auto-create is fine to be called even if the queue exists.
		CreateQueueResult createQueueResult = this.amazonSqs
				.createQueue(new CreateQueueRequest(queueName));
		return createQueueResult.getQueueUrl();
	}
	else {
		try {
			GetQueueUrlResult getQueueUrlResult = this.amazonSqs
					.getQueueUrl(new GetQueueUrlRequest(queueName));
			return getQueueUrlResult.getQueueUrl();
		}
		catch (QueueDoesNotExistException e) {
			throw toDestinationResolutionException(e);
		}
	}
}
 
Example 9
Source File: TOCQueue.java    From s3-bucket-loader with Apache License 2.0 5 votes vote down vote up
/**
 * Note here we attempt to the TOCQueue which may take some time to be shown as available
 * @param isConsumer
 * @param maxAttempts
 * @throws Exception
 */
public void connectToQueue(boolean isConsumer, int maxAttempts) throws Exception{
	
	for (int i=0; i<maxAttempts; i++) {
		
		logger.debug("connectToQueue() attempt: " + (i+1));
		
		ListQueuesResult queuesResult = sqsClient.listQueues();
		if (queuesResult != null) {
			for (String queueUrl : queuesResult.getQueueUrls()) {
				if (queueUrl.indexOf(sqsQueueName) != -1) {
					tocQueueUrl = queueUrl;
					break;
				}
			}
		}
		
		// if consumer, retry, otherwise is master, so just exit quick to create...
		if (tocQueueUrl == null && isConsumer) {
			Thread.currentThread().sleep(1000);
			continue;
		} else {
			break; // exit;
		}
	}
	
	if (tocQueueUrl == null && !isConsumer) {
		CreateQueueResult createQueueResult = sqsClient.createQueue(sqsQueueName);
		this.tocQueueUrl = createQueueResult.getQueueUrl();
		
	} else if (tocQueueUrl == null) {
		throw new Exception("TOCQueue() isConsumer:"+ isConsumer+ " cannot start, sqsQueueName has yet to be created by master?: " + sqsQueueName);
	}
}
 
Example 10
Source File: SpringCloudSQSLiveTest.java    From tutorials with MIT License 5 votes vote down vote up
@BeforeClass
public static void setupAwsResources() {

    sendQueueName = UUID.randomUUID().toString();
    receiveQueueName = SpringCloudSQS.QUEUE_NAME;

    AmazonSQS amazonSQS = SpringCloudAwsTestUtil.amazonSQS();

    CreateQueueResult receiveQueue = amazonSQS.createQueue(receiveQueueName);
    receiveQueueUrl = receiveQueue.getQueueUrl();

    CreateQueueResult sendQueue = amazonSQS.createQueue(sendQueueName);
    sendQueueURl = sendQueue.getQueueUrl();
}
 
Example 11
Source File: SqsExecutor.java    From spring-integration-aws with MIT License 5 votes vote down vote up
private void createQueueIfNotExists() {
	for (String qUrl : sqsClient.listQueues().getQueueUrls()) {
		if (qUrl.contains(queueName)) {
			queueUrl = qUrl;
			break;
		}
	}
	if (queueUrl == null) {
		CreateQueueRequest request = new CreateQueueRequest(queueName);
		Map<String, String> queueAttributes = new HashMap<String, String>();
		queueAttributes.put("ReceiveMessageWaitTimeSeconds", Integer
				.valueOf(receiveMessageWaitTimeout).toString());
		if (messageDelay != null) {
			queueAttributes.put("DelaySeconds", messageDelay.toString());
		}
		if (maximumMessageSize != null) {
			queueAttributes.put("MaximumMessageSize",
					maximumMessageSize.toString());
		}
		if (messageRetentionPeriod != null) {
			queueAttributes.put("MessageRetentionPeriod",
					messageRetentionPeriod.toString());
		}
		if (visibilityTimeout != null) {
			queueAttributes.put("VisibilityTimeout",
					visibilityTimeout.toString());
		}
		request.setAttributes(queueAttributes);
		CreateQueueResult result = sqsClient.createQueue(request);
		queueUrl = result.getQueueUrl();
		log.debug("New queue available at: " + queueUrl);
	} else {
		log.debug("Queue already exists: " + queueUrl);
	}

	resolveQueueArn();
}
 
Example 12
Source File: ControlChannel.java    From s3-bucket-loader with Apache License 2.0 4 votes vote down vote up
public void connectToTopic(boolean callerIsMaster, int maxAttempts, String userAccountPrincipalId, String userARN) throws Exception {
	
	
	// try up to max attempts to connect to pre-existing topic
	for (int i=0; i<maxAttempts; i++) {
		
		logger.debug("connectToTopic() attempt: " + (i+1));
		
		ListTopicsResult listResult = snsClient.listTopics();
		List<Topic> topics = listResult.getTopics();
		
		while(topics != null) {
			
			for (Topic topic : topics) {

				// note we do index of match....
				if (topic.getTopicArn().indexOf(snsControlTopicName) != -1) {
					snsTopicARN = topic.getTopicArn();
					logger.info("Found existing SNS topic by name: "+snsControlTopicName + " @ " + snsTopicARN);
					break;
				}
			}

			String nextToken = listResult.getNextToken();
			
			if (nextToken != null && snsTopicARN == null) {
				listResult = snsClient.listTopics(nextToken);
				topics = listResult.getTopics();
				
			} else {
				break;
			}
		}
		
		// if consumer, retry, otherwise is master, so just exit quick to create...
		if (snsTopicARN == null && !callerIsMaster) {
			Thread.currentThread().sleep(1000);
			continue;
		} else {
			break; // exit;
		}
	}
	
	
	
	// if master only he can create...
	if (snsTopicARN == null && callerIsMaster) {
		this.snsControlTopicName = this.snsControlTopicName.substring(0,(snsControlTopicName.length() > 80 ? 80 : this.snsControlTopicName.length()));
		
		logger.info("Attempting to create new SNS control channel topic by name: "+this.snsControlTopicName);
		
		CreateTopicResult createTopicResult = snsClient.createTopic(this.snsControlTopicName);
		snsTopicARN = createTopicResult.getTopicArn();
		snsClient.addPermission(snsTopicARN, "Permit_SNSAdd", 
								Arrays.asList(new String[]{userARN}), 
								Arrays.asList(new String[]{"Publish","Subscribe","Receive"}));
		logger.info("Created new SNS control channel topic by name: "+this.snsControlTopicName + " @ " + snsTopicARN);
		
	} else if (snsTopicARN == null) {
		throw new Exception("Worker() cannot start, snsControlTopicName has yet to be created by master?: " + this.snsControlTopicName);
	}
	
	// http://www.jorgjanke.com/2013/01/aws-sns-topic-subscriptions-with-sqs.html
	
	// create SQS queue to get SNS notifications (max 80 len)
	String prefix =  ("s3bktLoaderCC_" + mySourceIdentifier);
	String sqsQueueName = prefix.substring(0,(prefix.length() > 80 ? 80 : prefix.length()));
	
	CreateQueueResult createQueueResult = sqsClient.createQueue(sqsQueueName);
	this.sqsQueueUrl = createQueueResult.getQueueUrl();
	this.sqsQueueARN = sqsClient.getQueueAttributes(sqsQueueUrl, Arrays.asList(new String[]{"QueueArn"})).getAttributes().get("QueueArn");

	Statement statement = new Statement(Effect.Allow)
							.withActions(SQSActions.SendMessage)
							 .withPrincipals(new Principal("*"))
							 .withConditions(ConditionFactory.newSourceArnCondition(snsTopicARN))
							 .withResources(new Resource(sqsQueueARN));
	Policy policy = new Policy("SubscriptionPermission").withStatements(statement);

	HashMap<String, String> attributes = new HashMap<String, String>();
	attributes.put("Policy", policy.toJson());
	SetQueueAttributesRequest request = new SetQueueAttributesRequest(sqsQueueUrl, attributes);
	sqsClient.setQueueAttributes(request);

	logger.info("Created SQS queue: " + sqsQueueARN + " @ " + sqsQueueUrl);
	
	// subscribe our SQS queue to the SNS:s3MountTest topic
	SubscribeResult subscribeResult = snsClient.subscribe(snsTopicARN,"sqs",sqsQueueARN);
	snsSubscriptionARN = subscribeResult.getSubscriptionArn();
	logger.info("Subscribed for messages from SNS control channel:" + snsTopicARN + " ----> SQS: "+sqsQueueARN);
	logger.info("Subscription ARN: " + snsSubscriptionARN);
	
	this.consumerThread = new Thread(this,"ControlChannel msg consumer thread");
	this.consumerThread.start();

	logger.info("\n-------------------------------------------\n" +
				"CONTROL CHANNEL: ALL SNS/SQS resources hooked up OK\n" +
				"-------------------------------------------\n");
}
 
Example 13
Source File: BeanstalkConnector.java    From cloudml with GNU Lesser General Public License v3.0 4 votes vote down vote up
public String createQueue(String name){
    CreateQueueRequest request=new CreateQueueRequest();
    request.setQueueName(name);
    CreateQueueResult res= sqsClient.createQueue(request);
    return res.getQueueUrl();
}