Search in sources :

Example 1 with ServiceBusContract

use of com.microsoft.windowsazure.services.servicebus.ServiceBusContract in project azure-sdk-for-java by Azure.

the class ServiceBusPublishSubscribeAdvanceFeatures method runSample.

/**
     * Main function which runs the actual sample.
     * @param azure instance of the azure client
     * @return true if sample runs successfully
     */
public static boolean runSample(Azure azure) {
    // New resources
    final String rgName = SdkContext.randomResourceName("rgSB04_", 24);
    final String namespaceName = SdkContext.randomResourceName("namespace", 20);
    final String topic1Name = SdkContext.randomResourceName("topic1_", 24);
    final String topic2Name = SdkContext.randomResourceName("topic2_", 24);
    final String subscription1Name = SdkContext.randomResourceName("subs_", 24);
    final String subscription2Name = SdkContext.randomResourceName("subs_", 24);
    final String subscription3Name = SdkContext.randomResourceName("subs_", 24);
    final String sendRuleName = "SendRule";
    final String manageRuleName = "ManageRule";
    try {
        //============================================================
        // Create a namespace.
        System.out.println("Creating name space " + namespaceName + " in resource group " + rgName + "...");
        ServiceBusNamespace serviceBusNamespace = azure.serviceBusNamespaces().define(namespaceName).withRegion(Region.US_WEST).withNewResourceGroup(rgName).withSku(NamespaceSku.PREMIUM_CAPACITY1).withNewTopic(topic1Name, 1024).create();
        System.out.println("Created service bus " + serviceBusNamespace.name());
        Utils.print(serviceBusNamespace);
        System.out.println("Created topic following topic along with namespace " + namespaceName);
        Topic firstTopic = serviceBusNamespace.topics().getByName(topic1Name);
        Utils.print(firstTopic);
        //============================================================
        // Create a service bus subscription in the topic with session and dead-letter enabled.
        System.out.println("Creating subscription " + subscription1Name + " in topic " + topic1Name + "...");
        ServiceBusSubscription firstSubscription = firstTopic.subscriptions().define(subscription1Name).withSession().withDefaultMessageTTL(new Period().withMinutes(20)).withMessageMovedToDeadLetterSubscriptionOnMaxDeliveryCount(20).withExpiredMessageMovedToDeadLetterSubscription().withMessageMovedToDeadLetterSubscriptionOnFilterEvaluationException().create();
        System.out.println("Created subscription " + subscription1Name + " in topic " + topic1Name + "...");
        Utils.print(firstSubscription);
        //============================================================
        // Create another subscription in the topic with auto deletion of idle entities.
        System.out.println("Creating another subscription " + subscription2Name + " in topic " + topic1Name + "...");
        ServiceBusSubscription secondSubscription = firstTopic.subscriptions().define(subscription2Name).withSession().withDeleteOnIdleDurationInMinutes(20).create();
        System.out.println("Created subscription " + subscription2Name + " in topic " + topic1Name + "...");
        Utils.print(secondSubscription);
        //============================================================
        // Create second topic with new Send Authorization rule, partitioning enabled and a new Service bus Subscription.
        System.out.println("Creating second topic " + topic2Name + ", with De-duplication and AutoDeleteOnIdle features...");
        Topic secondTopic = serviceBusNamespace.topics().define(topic2Name).withNewSendRule(sendRuleName).withPartitioning().withNewSubscription(subscription3Name).create();
        System.out.println("Created second topic in namespace");
        Utils.print(secondTopic);
        System.out.println("Creating following authorization rules in second topic ");
        PagedList<TopicAuthorizationRule> authorizationRules = secondTopic.authorizationRules().list();
        for (TopicAuthorizationRule authorizationRule : authorizationRules) {
            Utils.print(authorizationRule);
        }
        //============================================================
        // Update second topic to change time for AutoDeleteOnIdle time, without Send rule and with a new manage authorization rule.
        System.out.println("Updating second topic " + topic2Name + "...");
        secondTopic = secondTopic.update().withDeleteOnIdleDurationInMinutes(5).withoutAuthorizationRule(sendRuleName).withNewManageRule(manageRuleName).apply();
        System.out.println("Updated second topic to change its auto deletion time");
        Utils.print(secondTopic);
        System.out.println("Updated  following authorization rules in second topic, new list of authorization rules are ");
        authorizationRules = secondTopic.authorizationRules().list();
        for (TopicAuthorizationRule authorizationRule : authorizationRules) {
            Utils.print(authorizationRule);
        }
        //=============================================================
        // Get connection string for default authorization rule of namespace
        PagedList<NamespaceAuthorizationRule> namespaceAuthorizationRules = serviceBusNamespace.authorizationRules().list();
        System.out.println("Number of authorization rule for namespace :" + namespaceAuthorizationRules.size());
        for (NamespaceAuthorizationRule namespaceAuthorizationRule : namespaceAuthorizationRules) {
            Utils.print(namespaceAuthorizationRule);
        }
        System.out.println("Getting keys for authorization rule ...");
        AuthorizationKeys keys = namespaceAuthorizationRules.get(0).getKeys();
        Utils.print(keys);
        // Send a message to topic.
        try {
            Configuration config = Configuration.load();
            config.setProperty(ServiceBusConfiguration.CONNECTION_STRING, keys.primaryConnectionString());
            ServiceBusContract service = ServiceBusService.create(config);
            service.sendTopicMessage(topic1Name, new BrokeredMessage("Hello World"));
        } catch (Exception ex) {
        }
        //=============================================================
        // Delete a topic and namespace
        System.out.println("Deleting topic " + topic1Name + "in namespace " + namespaceName + "...");
        serviceBusNamespace.topics().deleteByName(topic1Name);
        System.out.println("Deleted topic " + topic1Name + "...");
        System.out.println("Deleting namespace " + namespaceName + "...");
        // This will delete the namespace and topic within it.
        try {
            azure.serviceBusNamespaces().deleteById(serviceBusNamespace.id());
        } catch (Exception ex) {
        }
        System.out.println("Deleted namespace " + namespaceName + "...");
        return true;
    } catch (Exception e) {
        System.err.println(e.getMessage());
        e.printStackTrace();
    } finally {
        try {
            System.out.println("Deleting Resource Group: " + rgName);
            azure.resourceGroups().beginDeleteByName(rgName);
            System.out.println("Deleted Resource Group: " + rgName);
        } catch (NullPointerException npe) {
            System.out.println("Did not create any resources in Azure. No clean up is necessary");
        } catch (Exception g) {
            g.printStackTrace();
        }
    }
    return false;
}
Also used : BrokeredMessage(com.microsoft.windowsazure.services.servicebus.models.BrokeredMessage) ServiceBusConfiguration(com.microsoft.windowsazure.services.servicebus.ServiceBusConfiguration) Configuration(com.microsoft.windowsazure.Configuration) ServiceBusSubscription(com.microsoft.azure.management.servicebus.ServiceBusSubscription) AuthorizationKeys(com.microsoft.azure.management.servicebus.AuthorizationKeys) Period(org.joda.time.Period) NamespaceAuthorizationRule(com.microsoft.azure.management.servicebus.NamespaceAuthorizationRule) ServiceBusContract(com.microsoft.windowsazure.services.servicebus.ServiceBusContract) ServiceBusNamespace(com.microsoft.azure.management.servicebus.ServiceBusNamespace) TopicAuthorizationRule(com.microsoft.azure.management.servicebus.TopicAuthorizationRule) Topic(com.microsoft.azure.management.servicebus.Topic)

Example 2 with ServiceBusContract

use of com.microsoft.windowsazure.services.servicebus.ServiceBusContract in project azure-sdk-for-java by Azure.

the class ServiceBusPublishSubscribeBasic method runSample.

/**
     * Main function which runs the actual sample.
     * @param azure instance of the azure client
     * @return true if sample runs successfully
     */
public static boolean runSample(Azure azure) {
    // New resources
    final String rgName = SdkContext.randomResourceName("rgSB02_", 24);
    final String namespaceName = SdkContext.randomResourceName("namespace", 20);
    final String topicName = SdkContext.randomResourceName("topic_", 24);
    final String subscription1Name = SdkContext.randomResourceName("sub1_", 24);
    final String subscription2Name = SdkContext.randomResourceName("sub2_", 24);
    try {
        //============================================================
        // Create a namespace.
        System.out.println("Creating name space " + namespaceName + " in resource group " + rgName + "...");
        ServiceBusNamespace serviceBusNamespace = azure.serviceBusNamespaces().define(namespaceName).withRegion(Region.US_WEST).withNewResourceGroup(rgName).withSku(NamespaceSku.PREMIUM_CAPACITY1).create();
        System.out.println("Created service bus " + serviceBusNamespace.name());
        Utils.print(serviceBusNamespace);
        //============================================================
        // Create a topic in namespace
        System.out.println("Creating topic " + topicName + " in namespace " + namespaceName + "...");
        Topic topic = serviceBusNamespace.topics().define(topicName).withSizeInMB(2048).create();
        System.out.println("Created second queue in namespace");
        Utils.print(topic);
        //============================================================
        // Get and update topic with new size and a subscription
        System.out.println("Updating topic " + topicName + " with new size and a subscription...");
        topic = serviceBusNamespace.topics().getByName(topicName);
        topic = topic.update().withNewSubscription(subscription1Name).withSizeInMB(3072).apply();
        System.out.println("Updated topic to change its size in MB along with a subscription");
        Utils.print(topic);
        ServiceBusSubscription firstSubscription = topic.subscriptions().getByName(subscription1Name);
        Utils.print(firstSubscription);
        //============================================================
        // Create a subscription
        System.out.println("Adding second subscription" + subscription2Name + " to topic " + topicName + "...");
        ServiceBusSubscription secondSubscription = topic.subscriptions().define(subscription2Name).withDeleteOnIdleDurationInMinutes(10).create();
        System.out.println("Added second subscription" + subscription2Name + " to topic " + topicName + "...");
        Utils.print(secondSubscription);
        //=============================================================
        // List topics in namespaces
        PagedList<Topic> topics = serviceBusNamespace.topics().list();
        System.out.println("Number of topics in namespace :" + topics.size());
        for (Topic topicInNamespace : topics) {
            Utils.print(topicInNamespace);
        }
        //=============================================================
        // List all subscriptions for topic in namespaces
        PagedList<ServiceBusSubscription> subscriptions = topic.subscriptions().list();
        System.out.println("Number of subscriptions to topic: " + subscriptions.size());
        for (ServiceBusSubscription subscription : subscriptions) {
            Utils.print(subscription);
        }
        //=============================================================
        // Get connection string for default authorization rule of namespace
        PagedList<NamespaceAuthorizationRule> namespaceAuthorizationRules = serviceBusNamespace.authorizationRules().list();
        System.out.println("Number of authorization rule for namespace :" + namespaceAuthorizationRules.size());
        for (NamespaceAuthorizationRule namespaceAuthorizationRule : namespaceAuthorizationRules) {
            Utils.print(namespaceAuthorizationRule);
        }
        System.out.println("Getting keys for authorization rule ...");
        AuthorizationKeys keys = namespaceAuthorizationRules.get(0).getKeys();
        Utils.print(keys);
        System.out.println("Regenerating secondary key for authorization rule ...");
        keys = namespaceAuthorizationRules.get(0).regenerateKey(Policykey.SECONDARY_KEY);
        Utils.print(keys);
        // Send a message to topic.
        try {
            Configuration config = Configuration.load();
            config.setProperty(ServiceBusConfiguration.CONNECTION_STRING, keys.primaryConnectionString());
            ServiceBusContract service = ServiceBusService.create(config);
            service.sendTopicMessage(topicName, new BrokeredMessage("Hello World"));
        } catch (Exception ex) {
        }
        //=============================================================
        // Delete a queue and namespace
        System.out.println("Deleting subscription " + subscription1Name + " in topic " + topicName + " via update flow...");
        topic = topic.update().withoutSubscription(subscription1Name).apply();
        System.out.println("Deleted subscription " + subscription1Name + "...");
        System.out.println("Number of subscriptions in the topic after deleting first subscription: " + topic.subscriptionCount());
        System.out.println("Deleting namespace " + namespaceName + "...");
        // This will delete the namespace and queue within it.
        try {
            azure.serviceBusNamespaces().deleteById(serviceBusNamespace.id());
        } catch (Exception ex) {
        }
        System.out.println("Deleted namespace " + namespaceName + "...");
        return true;
    } catch (Exception e) {
        System.err.println(e.getMessage());
        e.printStackTrace();
    } finally {
        try {
            System.out.println("Deleting Resource Group: " + rgName);
            azure.resourceGroups().beginDeleteByName(rgName);
            System.out.println("Deleted Resource Group: " + rgName);
        } catch (NullPointerException npe) {
            System.out.println("Did not create any resources in Azure. No clean up is necessary");
        } catch (Exception g) {
            g.printStackTrace();
        }
    }
    return false;
}
Also used : BrokeredMessage(com.microsoft.windowsazure.services.servicebus.models.BrokeredMessage) ServiceBusConfiguration(com.microsoft.windowsazure.services.servicebus.ServiceBusConfiguration) Configuration(com.microsoft.windowsazure.Configuration) ServiceBusSubscription(com.microsoft.azure.management.servicebus.ServiceBusSubscription) AuthorizationKeys(com.microsoft.azure.management.servicebus.AuthorizationKeys) NamespaceAuthorizationRule(com.microsoft.azure.management.servicebus.NamespaceAuthorizationRule) ServiceBusContract(com.microsoft.windowsazure.services.servicebus.ServiceBusContract) ServiceBusNamespace(com.microsoft.azure.management.servicebus.ServiceBusNamespace) Topic(com.microsoft.azure.management.servicebus.Topic)

Example 3 with ServiceBusContract

use of com.microsoft.windowsazure.services.servicebus.ServiceBusContract in project azure-sdk-for-java by Azure.

the class ServiceBusQueueAdvanceFeatures method runSample.

/**
     * Main function which runs the actual sample.
     * @param azure instance of the azure client
     * @return true if sample runs successfully
     */
public static boolean runSample(Azure azure) {
    // New resources
    final String rgName = SdkContext.randomResourceName("rgSB04_", 24);
    final String namespaceName = SdkContext.randomResourceName("namespace", 20);
    final String queue1Name = SdkContext.randomResourceName("queue1_", 24);
    final String queue2Name = SdkContext.randomResourceName("queue2_", 24);
    final String sendRuleName = "SendRule";
    try {
        //============================================================
        // Create a namespace.
        System.out.println("Creating name space " + namespaceName + " in resource group " + rgName + "...");
        ServiceBusNamespace serviceBusNamespace = azure.serviceBusNamespaces().define(namespaceName).withRegion(Region.US_WEST).withNewResourceGroup(rgName).withSku(NamespaceSku.PREMIUM_CAPACITY1).create();
        System.out.println("Created service bus " + serviceBusNamespace.name());
        Utils.print(serviceBusNamespace);
        //============================================================
        // Add a queue in namespace with features session and dead-lettering.
        System.out.println("Creating first queue " + queue1Name + ", with session, time to live and move to dead-letter queue features...");
        Queue firstQueue = serviceBusNamespace.queues().define(queue1Name).withSession().withDefaultMessageTTL(new Period().withMinutes(10)).withExpiredMessageMovedToDeadLetterQueue().withMessageMovedToDeadLetterQueueOnMaxDeliveryCount(40).create();
        Utils.print(firstQueue);
        //============================================================
        // Create second queue with Deduplication and AutoDeleteOnIdle feature
        System.out.println("Creating second queue " + queue2Name + ", with De-duplication and AutoDeleteOnIdle features...");
        Queue secondQueue = serviceBusNamespace.queues().define(queue2Name).withSizeInMB(2048).withDuplicateMessageDetection(new Period().withMinutes(10)).withDeleteOnIdleDurationInMinutes(10).create();
        System.out.println("Created second queue in namespace");
        Utils.print(secondQueue);
        //============================================================
        // Update second queue to change time for AutoDeleteOnIdle.
        secondQueue = secondQueue.update().withDeleteOnIdleDurationInMinutes(5).apply();
        System.out.println("Updated second queue to change its auto deletion time");
        Utils.print(secondQueue);
        //=============================================================
        // Update first queue to disable dead-letter forwarding and with new Send authorization rule
        secondQueue = firstQueue.update().withoutExpiredMessageMovedToDeadLetterQueue().withNewSendRule(sendRuleName).apply();
        System.out.println("Updated first queue to change dead-letter forwarding");
        Utils.print(secondQueue);
        //=============================================================
        // Get connection string for default authorization rule of namespace
        PagedList<NamespaceAuthorizationRule> namespaceAuthorizationRules = serviceBusNamespace.authorizationRules().list();
        System.out.println("Number of authorization rule for namespace :" + namespaceAuthorizationRules.size());
        for (NamespaceAuthorizationRule namespaceAuthorizationRule : namespaceAuthorizationRules) {
            Utils.print(namespaceAuthorizationRule);
        }
        System.out.println("Getting keys for authorization rule ...");
        AuthorizationKeys keys = namespaceAuthorizationRules.get(0).getKeys();
        Utils.print(keys);
        //=============================================================
        // Update first queue to remove Send Authorization rule.
        firstQueue.update().withoutAuthorizationRule(sendRuleName).apply();
        try {
            Configuration config = Configuration.load();
            config.setProperty(ServiceBusConfiguration.CONNECTION_STRING, keys.primaryConnectionString());
            ServiceBusContract service = ServiceBusService.create(config);
            BrokeredMessage message = new BrokeredMessage("Hello");
            message.setSessionId("23424");
            service.sendQueueMessage(queue1Name, message);
        } catch (Exception ex) {
        }
        //=============================================================
        // Delete a queue and namespace
        System.out.println("Deleting queue " + queue1Name + "in namespace " + namespaceName + "...");
        serviceBusNamespace.queues().deleteByName(queue1Name);
        System.out.println("Deleted queue " + queue1Name + "...");
        System.out.println("Deleting namespace " + namespaceName + "...");
        // This will delete the namespace and queue within it.
        try {
            azure.serviceBusNamespaces().deleteById(serviceBusNamespace.id());
        } catch (Exception ex) {
        }
        System.out.println("Deleted namespace " + namespaceName + "...");
        return true;
    } catch (Exception e) {
        System.err.println(e.getMessage());
        e.printStackTrace();
    } finally {
        try {
            System.out.println("Deleting Resource Group: " + rgName);
            azure.resourceGroups().beginDeleteByName(rgName);
            System.out.println("Deleted Resource Group: " + rgName);
        } catch (NullPointerException npe) {
            System.out.println("Did not create any resources in Azure. No clean up is necessary");
        } catch (Exception g) {
            g.printStackTrace();
        }
    }
    return false;
}
Also used : BrokeredMessage(com.microsoft.windowsazure.services.servicebus.models.BrokeredMessage) ServiceBusConfiguration(com.microsoft.windowsazure.services.servicebus.ServiceBusConfiguration) Configuration(com.microsoft.windowsazure.Configuration) ServiceBusNamespace(com.microsoft.azure.management.servicebus.ServiceBusNamespace) AuthorizationKeys(com.microsoft.azure.management.servicebus.AuthorizationKeys) Period(org.joda.time.Period) NamespaceAuthorizationRule(com.microsoft.azure.management.servicebus.NamespaceAuthorizationRule) ServiceBusContract(com.microsoft.windowsazure.services.servicebus.ServiceBusContract) Queue(com.microsoft.azure.management.servicebus.Queue)

Example 4 with ServiceBusContract

use of com.microsoft.windowsazure.services.servicebus.ServiceBusContract in project azure-sdk-for-java by Azure.

the class ServiceBusQueueBasic method runSample.

/**
     * Main function which runs the actual sample.
     * @param azure instance of the azure client
     * @return true if sample runs successfully
     */
public static boolean runSample(Azure azure) {
    // New resources
    final String rgName = SdkContext.randomResourceName("rgSB01_", 24);
    final String namespaceName = SdkContext.randomResourceName("namespace", 20);
    final String queue1Name = SdkContext.randomResourceName("queue1_", 24);
    final String queue2Name = SdkContext.randomResourceName("queue2_", 24);
    try {
        //============================================================
        // Create a namespace.
        System.out.println("Creating name space " + namespaceName + " in resource group " + rgName + "...");
        ServiceBusNamespace serviceBusNamespace = azure.serviceBusNamespaces().define(namespaceName).withRegion(Region.US_WEST).withNewResourceGroup(rgName).withSku(NamespaceSku.PREMIUM_CAPACITY1).withNewQueue(queue1Name, 1024).create();
        System.out.println("Created service bus " + serviceBusNamespace.name());
        Utils.print(serviceBusNamespace);
        Queue firstQueue = serviceBusNamespace.queues().getByName(queue1Name);
        Utils.print(firstQueue);
        //============================================================
        // Create a second queue in same namespace
        System.out.println("Creating second queue " + queue2Name + " in namespace " + namespaceName + "...");
        Queue secondQueue = serviceBusNamespace.queues().define(queue2Name).withExpiredMessageMovedToDeadLetterQueue().withSizeInMB(2048).withMessageLockDurationInSeconds(20).create();
        System.out.println("Created second queue in namespace");
        Utils.print(secondQueue);
        //============================================================
        // Get and update second queue.
        secondQueue = serviceBusNamespace.queues().getByName(queue2Name);
        secondQueue = secondQueue.update().withSizeInMB(3072).apply();
        System.out.println("Updated second queue to change its size in MB");
        Utils.print(secondQueue);
        //=============================================================
        // Update namespace
        System.out.println("Updating sku of namespace " + serviceBusNamespace.name() + "...");
        serviceBusNamespace = serviceBusNamespace.update().withSku(NamespaceSku.PREMIUM_CAPACITY2).apply();
        System.out.println("Updated sku of namespace " + serviceBusNamespace.name());
        //=============================================================
        // List namespaces
        System.out.println("List of namespaces in resource group " + rgName + "...");
        for (ServiceBusNamespace serviceBusNamespace1 : azure.serviceBusNamespaces().listByResourceGroup(rgName)) {
            Utils.print(serviceBusNamespace1);
        }
        //=============================================================
        // List queues in namespaces
        PagedList<Queue> queues = serviceBusNamespace.queues().list();
        System.out.println("Number of queues in namespace :" + queues.size());
        for (Queue queue : queues) {
            Utils.print(queue);
        }
        //=============================================================
        // Get connection string for default authorization rule of namespace
        PagedList<NamespaceAuthorizationRule> namespaceAuthorizationRules = serviceBusNamespace.authorizationRules().list();
        System.out.println("Number of authorization rule for namespace :" + namespaceAuthorizationRules.size());
        for (NamespaceAuthorizationRule namespaceAuthorizationRule : namespaceAuthorizationRules) {
            Utils.print(namespaceAuthorizationRule);
        }
        System.out.println("Getting keys for authorization rule ...");
        AuthorizationKeys keys = namespaceAuthorizationRules.get(0).getKeys();
        Utils.print(keys);
        System.out.println("Regenerating secondary key for authorization rule ...");
        keys = namespaceAuthorizationRules.get(0).regenerateKey(Policykey.SECONDARY_KEY);
        Utils.print(keys);
        // Send a message to queue.
        try {
            Configuration config = Configuration.load();
            config.setProperty(ServiceBusConfiguration.CONNECTION_STRING, keys.primaryConnectionString());
            ServiceBusContract service = ServiceBusService.create(config);
            service.sendQueueMessage(queue1Name, new BrokeredMessage("Hello World"));
        } catch (Exception ex) {
        }
        //=============================================================
        // Delete a queue and namespace
        System.out.println("Deleting queue " + queue1Name + "in namespace " + namespaceName + "...");
        serviceBusNamespace.queues().deleteByName(queue1Name);
        System.out.println("Deleted queue " + queue1Name + "...");
        System.out.println("Deleting namespace " + namespaceName + "...");
        // This will delete the namespace and queue within it.
        try {
            azure.serviceBusNamespaces().deleteById(serviceBusNamespace.id());
        } catch (Exception ex) {
        }
        System.out.println("Deleted namespace " + namespaceName + "...");
        return true;
    } catch (Exception e) {
        System.err.println(e.getMessage());
        e.printStackTrace();
    } finally {
        try {
            System.out.println("Deleting Resource Group: " + rgName);
            azure.resourceGroups().beginDeleteByName(rgName);
            System.out.println("Deleted Resource Group: " + rgName);
        } catch (NullPointerException npe) {
            System.out.println("Did not create any resources in Azure. No clean up is necessary");
        } catch (Exception g) {
            g.printStackTrace();
        }
    }
    return false;
}
Also used : BrokeredMessage(com.microsoft.windowsazure.services.servicebus.models.BrokeredMessage) ServiceBusConfiguration(com.microsoft.windowsazure.services.servicebus.ServiceBusConfiguration) Configuration(com.microsoft.windowsazure.Configuration) ServiceBusNamespace(com.microsoft.azure.management.servicebus.ServiceBusNamespace) AuthorizationKeys(com.microsoft.azure.management.servicebus.AuthorizationKeys) NamespaceAuthorizationRule(com.microsoft.azure.management.servicebus.NamespaceAuthorizationRule) ServiceBusContract(com.microsoft.windowsazure.services.servicebus.ServiceBusContract) Queue(com.microsoft.azure.management.servicebus.Queue)

Example 5 with ServiceBusContract

use of com.microsoft.windowsazure.services.servicebus.ServiceBusContract in project azure-sdk-for-java by Azure.

the class ServiceBusWithClaimBasedAuthorization method runSample.

/**
     * Main function which runs the actual sample.
     * @param azure instance of the azure client
     * @return true if sample runs successfully
     */
public static boolean runSample(Azure azure) {
    // New resources
    final String rgName = SdkContext.randomResourceName("rgSB03_", 24);
    final String namespaceName = SdkContext.randomResourceName("namespace", 20);
    final String queueName = SdkContext.randomResourceName("queue1_", 24);
    final String topicName = SdkContext.randomResourceName("topic_", 24);
    final String subscription1Name = SdkContext.randomResourceName("sub1_", 24);
    final String subscription2Name = SdkContext.randomResourceName("sub2_", 24);
    try {
        //============================================================
        // Create a namespace.
        System.out.println("Creating name space " + namespaceName + " along with a queue " + queueName + " and a topic " + topicName + " in resource group " + rgName + "...");
        ServiceBusNamespace serviceBusNamespace = azure.serviceBusNamespaces().define(namespaceName).withRegion(Region.US_WEST).withNewResourceGroup(rgName).withSku(NamespaceSku.PREMIUM_CAPACITY1).withNewQueue(queueName, 1024).withNewTopic(topicName, 1024).create();
        System.out.println("Created service bus " + serviceBusNamespace.name() + " (with queue and topic)");
        Utils.print(serviceBusNamespace);
        Queue queue = serviceBusNamespace.queues().getByName(queueName);
        Utils.print(queue);
        Topic topic = serviceBusNamespace.topics().getByName(topicName);
        Utils.print(topic);
        //============================================================
        // Create 2 subscriptions in topic using different methods.
        System.out.println("Creating a subscription in the topic using update on topic");
        topic = topic.update().withNewSubscription(subscription1Name).apply();
        ServiceBusSubscription subscription1 = topic.subscriptions().getByName(subscription1Name);
        System.out.println("Creating another subscription in the topic using direct create method for subscription");
        ServiceBusSubscription subscription2 = topic.subscriptions().define(subscription2Name).create();
        Utils.print(subscription1);
        Utils.print(subscription2);
        //=============================================================
        // Create new authorization rule for queue to send message.
        System.out.println("Create authorization rule for queue ...");
        NamespaceAuthorizationRule sendQueueAuthorizationRule = serviceBusNamespace.authorizationRules().define("SendRule").withSendingEnabled().create();
        Utils.print(sendQueueAuthorizationRule);
        System.out.println("Getting keys for authorization rule ...");
        AuthorizationKeys keys = sendQueueAuthorizationRule.getKeys();
        Utils.print(keys);
        // Send a message to queue.
        try {
            Configuration config = Configuration.load();
            config.setProperty(ServiceBusConfiguration.CONNECTION_STRING, keys.primaryConnectionString());
            ServiceBusContract queueService = ServiceBusService.create(config);
            queueService.sendMessage(queueName, new BrokeredMessage("Hello"));
        } catch (Exception ex) {
        }
        // Send a message to topic.
        try {
            Configuration config2 = Configuration.load();
            config2.setProperty(ServiceBusConfiguration.CONNECTION_STRING, keys.primaryConnectionString());
            ServiceBusContract topicService = ServiceBusService.create(config2);
            topicService.sendMessage(topicName, new BrokeredMessage("Hello"));
        } catch (Exception ex) {
        }
        //=============================================================
        // Delete a namespace
        System.out.println("Deleting namespace " + namespaceName + " [topic, queues and subscription will delete along with that]...");
        // This will delete the namespace and queue within it.
        try {
            azure.serviceBusNamespaces().deleteById(serviceBusNamespace.id());
        } catch (Exception ex) {
        }
        System.out.println("Deleted namespace " + namespaceName + "...");
        return true;
    } catch (Exception e) {
        System.err.println(e.getMessage());
        e.printStackTrace();
    } finally {
        try {
            System.out.println("Deleting Resource Group: " + rgName);
            azure.resourceGroups().beginDeleteByName(rgName);
            System.out.println("Deleted Resource Group: " + rgName);
        } catch (NullPointerException npe) {
            System.out.println("Did not create any resources in Azure. No clean up is necessary");
        } catch (Exception g) {
            g.printStackTrace();
        }
    }
    return false;
}
Also used : BrokeredMessage(com.microsoft.windowsazure.services.servicebus.models.BrokeredMessage) ServiceBusConfiguration(com.microsoft.windowsazure.services.servicebus.ServiceBusConfiguration) Configuration(com.microsoft.windowsazure.Configuration) ServiceBusSubscription(com.microsoft.azure.management.servicebus.ServiceBusSubscription) ServiceBusNamespace(com.microsoft.azure.management.servicebus.ServiceBusNamespace) AuthorizationKeys(com.microsoft.azure.management.servicebus.AuthorizationKeys) NamespaceAuthorizationRule(com.microsoft.azure.management.servicebus.NamespaceAuthorizationRule) ServiceBusContract(com.microsoft.windowsazure.services.servicebus.ServiceBusContract) Topic(com.microsoft.azure.management.servicebus.Topic) Queue(com.microsoft.azure.management.servicebus.Queue)

Aggregations

AuthorizationKeys (com.microsoft.azure.management.servicebus.AuthorizationKeys)5 NamespaceAuthorizationRule (com.microsoft.azure.management.servicebus.NamespaceAuthorizationRule)5 ServiceBusNamespace (com.microsoft.azure.management.servicebus.ServiceBusNamespace)5 Configuration (com.microsoft.windowsazure.Configuration)5 ServiceBusConfiguration (com.microsoft.windowsazure.services.servicebus.ServiceBusConfiguration)5 ServiceBusContract (com.microsoft.windowsazure.services.servicebus.ServiceBusContract)5 BrokeredMessage (com.microsoft.windowsazure.services.servicebus.models.BrokeredMessage)5 Queue (com.microsoft.azure.management.servicebus.Queue)3 ServiceBusSubscription (com.microsoft.azure.management.servicebus.ServiceBusSubscription)3 Topic (com.microsoft.azure.management.servicebus.Topic)3 Period (org.joda.time.Period)2 TopicAuthorizationRule (com.microsoft.azure.management.servicebus.TopicAuthorizationRule)1