Java Code Examples for com.microsoft.azure.storage.queue.CloudQueueClient#getQueueReference()

The following examples show how to use com.microsoft.azure.storage.queue.CloudQueueClient#getQueueReference() . 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: AzureStorageQueueService.java    From components with Apache License 2.0 6 votes vote down vote up
/**
 * This method create a queue if it doesn't exist
 */
public boolean createQueueIfNotExists(String queueName) throws InvalidKeyException, URISyntaxException, StorageException {
    CloudQueueClient client = connection.getCloudStorageAccount().createCloudQueueClient();
    CloudQueue queueRef = client.getQueueReference(queueName);
    boolean creationResult;
    try {
        creationResult = queueRef.createIfNotExists(null, AzureStorageUtils.getTalendOperationContext());
    } catch (StorageException e) {
        if (!e.getErrorCode().equals(StorageErrorCodeStrings.QUEUE_BEING_DELETED)) {
            throw e;
        }
        LOGGER.warn(messages.getMessage("error.QueueDeleted", queueRef.getName()));
        // Documentation doesn't specify how many seconds at least to wait.
        // 40 seconds before retrying.
        // See https://docs.microsoft.com/en-us/rest/api/storageservices/fileservices/delete-queue3
        try {
            Thread.sleep(40000);
        } catch (InterruptedException eint) {
            throw new RuntimeException(messages.getMessage("error.InterruptedException"));
        }
        creationResult = queueRef.createIfNotExists(null, AzureStorageUtils.getTalendOperationContext());
        LOGGER.debug(messages.getMessage("debug.QueueCreated", queueRef.getName()));
    }

    return creationResult;
}
 
Example 2
Source File: SecondaryTests.java    From azure-storage-android with Apache License 2.0 6 votes vote down vote up
private static void testQueueDownloadAttributes(LocationMode optionsLocationMode, LocationMode clientLocationMode,
        StorageLocation initialLocation, List<RetryContext> retryContextList, List<RetryInfo> retryInfoList)
        throws URISyntaxException, StorageException {
    CloudQueueClient client = TestHelper.createCloudQueueClient();
    CloudQueue queue = client.getQueueReference(QueueTestHelper.generateRandomQueueName());

    MultiLocationTestHelper helper = new MultiLocationTestHelper(queue.getServiceClient().getStorageUri(),
            initialLocation, retryContextList, retryInfoList);

    queue.getServiceClient().getDefaultRequestOptions().setLocationMode(clientLocationMode);
    QueueRequestOptions options = new QueueRequestOptions();

    options.setLocationMode(optionsLocationMode);
    options.setRetryPolicyFactory(helper.retryPolicy);

    try {
        queue.downloadAttributes(options, helper.operationContext);
    }
    catch (StorageException ex) {
        assertEquals(HttpURLConnection.HTTP_NOT_FOUND, ex.getHttpStatusCode());
    }
    finally {
        helper.close();
    }
}
 
Example 3
Source File: StorageAccountTests.java    From azure-storage-android with Apache License 2.0 6 votes vote down vote up
@Test
public void testCloudStorageAccountClientUriVerify() throws URISyntaxException, StorageException {
    StorageCredentialsAccountAndKey cred = new StorageCredentialsAccountAndKey(ACCOUNT_NAME, ACCOUNT_KEY);
    CloudStorageAccount cloudStorageAccount = new CloudStorageAccount(cred, true);

    CloudBlobClient blobClient = cloudStorageAccount.createCloudBlobClient();
    CloudBlobContainer container = blobClient.getContainerReference("container1");
    assertEquals(cloudStorageAccount.getBlobEndpoint().toString() + "/container1", container.getUri().toString());

    CloudQueueClient queueClient = cloudStorageAccount.createCloudQueueClient();
    CloudQueue queue = queueClient.getQueueReference("queue1");
    assertEquals(cloudStorageAccount.getQueueEndpoint().toString() + "/queue1", queue.getUri().toString());

    CloudTableClient tableClient = cloudStorageAccount.createCloudTableClient();
    CloudTable table = tableClient.getTableReference("table1");
    assertEquals(cloudStorageAccount.getTableEndpoint().toString() + "/table1", table.getUri().toString());

    CloudFileClient fileClient = cloudStorageAccount.createCloudFileClient();
    CloudFileShare share = fileClient.getShareReference("share1");
    assertEquals(cloudStorageAccount.getFileEndpoint().toString() + "/share1", share.getUri().toString());
}
 
Example 4
Source File: AzureStorageQueueService.java    From components with Apache License 2.0 5 votes vote down vote up
public Iterable<CloudQueueMessage> peekMessages(String queueName, int numberOfMessages)
        throws InvalidKeyException, URISyntaxException, StorageException {

    CloudQueueClient client = connection.getCloudStorageAccount().createCloudQueueClient();
    CloudQueue queueRef = client.getQueueReference(queueName);
    return queueRef.peekMessages(numberOfMessages, null, AzureStorageUtils.getTalendOperationContext());
}
 
Example 5
Source File: AzureStorageQueueService.java    From components with Apache License 2.0 5 votes vote down vote up
public Iterable<CloudQueueMessage> retrieveMessages(String queueName, int numberOfMessages, int visibilityTimeoutInSeconds)
        throws InvalidKeyException, URISyntaxException, StorageException {

    CloudQueueClient client = connection.getCloudStorageAccount().createCloudQueueClient();
    CloudQueue queueRef = client.getQueueReference(queueName);
    return queueRef.retrieveMessages(numberOfMessages, visibilityTimeoutInSeconds, null, AzureStorageUtils.getTalendOperationContext());
}
 
Example 6
Source File: AzureStorageQueueService.java    From components with Apache License 2.0 5 votes vote down vote up
public void deleteMessage(String queueName, CloudQueueMessage message)
        throws InvalidKeyException, URISyntaxException, StorageException {

    CloudQueueClient client = connection.getCloudStorageAccount().createCloudQueueClient();
    CloudQueue queueRef = client.getQueueReference(queueName);
    queueRef.deleteMessage(message, null, AzureStorageUtils.getTalendOperationContext());
}
 
Example 7
Source File: GenericTests.java    From azure-storage-android with Apache License 2.0 5 votes vote down vote up
@Test
public void testUserAgentString() throws URISyntaxException, StorageException {
    // Test with a blob request
    CloudBlobClient blobClient = TestHelper.createCloudBlobClient();
    CloudBlobContainer container = blobClient.getContainerReference("container1");
    OperationContext sendingRequestEventContext = new OperationContext();
    sendingRequestEventContext.getSendingRequestEventHandler().addListener(new StorageEvent<SendingRequestEvent>() {

        @Override
        public void eventOccurred(SendingRequestEvent eventArg) {
            assertEquals(
                    Constants.HeaderConstants.USER_AGENT_PREFIX
                            + "/"
                            + Constants.HeaderConstants.USER_AGENT_VERSION
                            + " "
                            + String.format(Utility.LOCALE_US, "(Android %s; %s; %s)",
                            android.os.Build.VERSION.RELEASE, android.os.Build.BRAND,
                            android.os.Build.MODEL), ((HttpURLConnection) eventArg.getConnectionObject())
                            .getRequestProperty(Constants.HeaderConstants.USER_AGENT));
        }
    });
    container.exists(null, null, sendingRequestEventContext);

    // Test with a queue request
    CloudQueueClient queueClient = TestHelper.createCloudQueueClient();
    CloudQueue queue = queueClient.getQueueReference("queue1");
    queue.exists(null, sendingRequestEventContext);

    // Test with a table request
    CloudTableClient tableClient = TestHelper.createCloudTableClient();
    CloudTable table = tableClient.getTableReference("table1");
    table.exists(null, sendingRequestEventContext);
}
 
Example 8
Source File: MaximumExecutionTimeTests.java    From azure-storage-android with Apache License 2.0 5 votes vote down vote up
@Test
@Category({ DevFabricTests.class, DevStoreTests.class, SecondaryTests.class })
public void testQueueMaximumExecutionTime() throws URISyntaxException, StorageException {
    OperationContext opContext = new OperationContext();
    setDelay(opContext, 2500);

    // set the maximum execution time
    QueueRequestOptions options = new QueueRequestOptions();
    options.setMaximumExecutionTimeInMs(2000);

    // set the location mode to secondary, secondary request should fail
    // so set the timeout low to save time failing (or fail with a timeout)
    options.setLocationMode(LocationMode.SECONDARY_THEN_PRIMARY);
    options.setTimeoutIntervalInMs(1000);

    CloudQueueClient queueClient = TestHelper.createCloudQueueClient();
    CloudQueue queue = queueClient.getQueueReference(generateRandomName("queue"));

    try {
        // 1. download attributes will fail as the queue does not exist
        // 2. the executor will attempt to retry as it is accessing secondary
        // 3. maximum execution time should prevent the retry from being made
        queue.downloadAttributes(options, opContext);
        fail("Maximum execution time was reached but request did not fail.");
    }
    catch (StorageException e) {
        assertEquals(SR.MAXIMUM_EXECUTION_TIMEOUT_EXCEPTION, e.getMessage());
    }
}
 
Example 9
Source File: PutAzureQueueStorage.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Override
public void onTrigger(ProcessContext context, ProcessSession session) throws ProcessException {
    FlowFile flowFile = session.get();
    if (flowFile == null) {
        return;
    }

    final long startNanos = System.nanoTime();

    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    session.exportTo(flowFile, baos);
    final String flowFileContent = baos.toString();

    CloudQueueMessage message = new CloudQueueMessage(flowFileContent);
    CloudQueueClient cloudQueueClient;
    CloudQueue cloudQueue;

    final int ttl = context.getProperty(TTL).asTimePeriod(TimeUnit.SECONDS).intValue();
    final int delay = context.getProperty(VISIBILITY_DELAY).asTimePeriod(TimeUnit.SECONDS).intValue();
    final String queue = context.getProperty(QUEUE).evaluateAttributeExpressions(flowFile).getValue().toLowerCase();

    try {
        cloudQueueClient = createCloudQueueClient(context, flowFile);
        cloudQueue = cloudQueueClient.getQueueReference(queue);

        final OperationContext operationContext = new OperationContext();
        AzureStorageUtils.setProxy(operationContext, context);

        cloudQueue.addMessage(message, ttl, delay, null, operationContext);
    } catch (URISyntaxException | StorageException e) {
        getLogger().error("Failed to write the message to Azure Queue Storage due to {}", new Object[]{e});
        flowFile = session.penalize(flowFile);
        session.transfer(flowFile, REL_FAILURE);
        return;
    }

    session.transfer(flowFile, REL_SUCCESS);
    final long transmissionMillis = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startNanos);
    session.getProvenanceReporter().send(flowFile, cloudQueue.getUri().toString(), transmissionMillis);
}
 
Example 10
Source File: AbstractAzureQueueStorageIT.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Before
public void setUpAzureQueueStorageIT() throws Exception {
    String queueName = String.format("%s-%s", TEST_QUEUE_NAME_PREFIX, UUID.randomUUID());
    CloudQueueClient cloudQueueClient = getStorageAccount().createCloudQueueClient();
    cloudQueue = cloudQueueClient.getQueueReference(queueName);
    cloudQueue.createIfNotExists();

    runner.setProperty(AbstractAzureQueueStorage.QUEUE, queueName);
}
 
Example 11
Source File: AzureStorageQueueService.java    From components with Apache License 2.0 4 votes vote down vote up
public boolean deleteQueueIfExists(String queueName) throws InvalidKeyException, URISyntaxException, StorageException {
    CloudQueueClient client = connection.getCloudStorageAccount().createCloudQueueClient();
    CloudQueue queueRef = client.getQueueReference(queueName);
    return queueRef.deleteIfExists(null, AzureStorageUtils.getTalendOperationContext());
}
 
Example 12
Source File: AzureStorageQueueService.java    From components with Apache License 2.0 4 votes vote down vote up
public long getApproximateMessageCount(String queueName) throws InvalidKeyException, URISyntaxException, StorageException {
    CloudQueueClient client = connection.getCloudStorageAccount().createCloudQueueClient();
    CloudQueue queueRef = client.getQueueReference(queueName);
    return queueRef.getApproximateMessageCount();
}
 
Example 13
Source File: AzureStorageQueueService.java    From components with Apache License 2.0 4 votes vote down vote up
public void clear(String queueName) throws InvalidKeyException, URISyntaxException, StorageException {
    CloudQueueClient client = connection.getCloudStorageAccount().createCloudQueueClient();
    CloudQueue queueRef = client.getQueueReference(queueName);
    queueRef.clear(null, AzureStorageUtils.getTalendOperationContext());
}