github.com/SaurabhDubey-Groww/go-cloud@v0.0.0-20221124105541-b26c29285fd8/pubsub/azuresb/example_test.go (about) 1 // Copyright 2018 The Go Cloud Development Kit Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // https://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package azuresb_test 16 17 import ( 18 "context" 19 "log" 20 "os" 21 22 servicebus "github.com/Azure/azure-sdk-for-go/sdk/messaging/azservicebus" 23 "gocloud.dev/pubsub" 24 "gocloud.dev/pubsub/azuresb" 25 ) 26 27 func ExampleOpenTopic() { 28 // PRAGMA: This example is used on gocloud.dev; PRAGMA comments adjust how it is shown and can be ignored. 29 // PRAGMA: On gocloud.dev, hide lines until the next blank line. 30 ctx := context.Background() 31 32 // Change these as needed for your application. 33 connString := os.Getenv("SERVICEBUS_CONNECTION_STRING") 34 topicName := "test-topic" 35 36 if connString == "" { 37 log.Fatal("Service Bus ConnectionString is not set") 38 } 39 40 // Connect to Azure Service Bus for the given topic. 41 sbClient, err := azuresb.NewClientFromConnectionString(connString, nil) 42 if err != nil { 43 log.Fatal(err) 44 } 45 sbSender, err := azuresb.NewSender(sbClient, topicName, nil) 46 if err != nil { 47 log.Fatal(err) 48 } 49 defer sbSender.Close(ctx) 50 51 // Construct a *pubsub.Topic. 52 topic, err := azuresb.OpenTopic(ctx, sbSender, nil) 53 if err != nil { 54 log.Fatal(err) 55 } 56 defer topic.Shutdown(ctx) 57 } 58 59 func Example_openTopicFromURL() { 60 // PRAGMA: This example is used on gocloud.dev; PRAGMA comments adjust how it is shown and can be ignored. 61 // PRAGMA: On gocloud.dev, add a blank import: _ "gocloud.dev/pubsub/azuresb" 62 // PRAGMA: On gocloud.dev, hide lines until the next blank line. 63 ctx := context.Background() 64 65 // pubsub.OpenTopic creates a *pubsub.Topic from a URL. 66 // This URL will open the topic "mytopic" using a connection string 67 // from the environment variable SERVICEBUS_CONNECTION_STRING. 68 topic, err := pubsub.OpenTopic(ctx, "azuresb://mytopic") 69 if err != nil { 70 log.Fatal(err) 71 } 72 defer topic.Shutdown(ctx) 73 } 74 75 func ExampleOpenSubscription() { 76 // PRAGMA: This example is used on gocloud.dev; PRAGMA comments adjust how it is shown and can be ignored. 77 // PRAGMA: On gocloud.dev, hide lines until the next blank line. 78 ctx := context.Background() 79 80 // Change these as needed for your application. 81 serviceBusConnString := os.Getenv("SERVICEBUS_CONNECTION_STRING") 82 const topicName = "test-topic" 83 const subscriptionName = "test-subscription" 84 85 // Connect to Azure Service Bus for the given subscription. 86 sbClient, err := azuresb.NewClientFromConnectionString(serviceBusConnString, nil) 87 if err != nil { 88 log.Fatal(err) 89 } 90 sbReceiver, err := azuresb.NewReceiver(sbClient, topicName, subscriptionName, nil) 91 if err != nil { 92 log.Fatal(err) 93 } 94 defer sbReceiver.Close(ctx) 95 96 // Construct a *pubsub.Subscription. 97 subscription, err := azuresb.OpenSubscription(ctx, sbClient, sbReceiver, nil) 98 if err != nil { 99 log.Fatal(err) 100 } 101 defer subscription.Shutdown(ctx) 102 } 103 104 func Example_openSubscriptionFromURL() { 105 // PRAGMA: This example is used on gocloud.dev; PRAGMA comments adjust how it is shown and can be ignored. 106 // PRAGMA: On gocloud.dev, add a blank import: _ "gocloud.dev/pubsub/azuresb" 107 // PRAGMA: On gocloud.dev, hide lines until the next blank line. 108 ctx := context.Background() 109 110 // pubsub.OpenSubscription creates a *pubsub.Subscription from a URL. 111 // This URL will open the subscription "mysubscription" for the topic 112 // "mytopic" using a connection string from the environment variable 113 // SERVICEBUS_CONNECTION_STRING. 114 subscription, err := pubsub.OpenSubscription(ctx, 115 "azuresb://mytopic?subscription=mysubscription") 116 if err != nil { 117 log.Fatal(err) 118 } 119 defer subscription.Shutdown(ctx) 120 } 121 122 func Example_OpenSubscription_inReceiveAndDeleteMode() { 123 124 ctx := context.Background() 125 126 // Change these as needed for your application. 127 serviceBusConnString := os.Getenv("SERVICEBUS_CONNECTION_STRING") 128 const topicName = "test-topic" 129 const subscriptionName = "test-subscription" 130 131 // Connect to Azure Service Bus for the given subscription. 132 sbClient, err := azuresb.NewClientFromConnectionString(serviceBusConnString, nil) 133 if err != nil { 134 log.Fatal(err) 135 } 136 // Create the azuresb.Subscription, configuring it with the 137 // ReceiveAndDelete option. 138 // See https://godoc.org/github.com/Azure/azure-sdk-for-go/sdk/messaging/azservicebus#SubscriptionWithReceiveAndDelete. 139 sbReceiverOptions := &servicebus.ReceiverOptions{ 140 ReceiveMode: servicebus.ReceiveModeReceiveAndDelete, 141 } 142 sbReceiver, err := azuresb.NewReceiver(sbClient, topicName, subscriptionName, sbReceiverOptions) 143 if err != nil { 144 log.Fatal(err) 145 } 146 defer sbReceiver.Close(ctx) 147 148 // Construct a *pubsub.Subscription. Since we configured 149 // the azuresb.Subscription with ReceiveAndDelete mode, we need 150 // to set SubscriptionOptions.ReceiveAndDelete = true. 151 subscription, err := azuresb.OpenSubscription(ctx, 152 sbClient, sbReceiver, &azuresb.SubscriptionOptions{ReceiveAndDelete: true}) 153 if err != nil { 154 log.Fatal(err) 155 } 156 defer subscription.Shutdown(ctx) 157 }