| 1 |
// Copyright 2019 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 natspubsub_test |
| 16 |
|
| 17 |
import ( |
| 18 |
"context" |
| 19 |
"log" |
| 20 |
|
| 21 |
"github.com/nats-io/nats.go" |
| 22 |
|
| 23 |
"gocloud.dev/pubsub" |
| 24 |
"gocloud.dev/pubsub/natspubsub" |
| 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 |
natsConn, err := nats.Connect("nats://nats.example.com") |
| 33 |
if err != nil { |
| 34 |
log.Fatal(err) |
| 35 |
} |
| 36 |
defer natsConn.Close() |
| 37 |
|
| 38 |
topic, err := natspubsub.OpenTopic(natsConn, "example.mysubject", nil) |
| 39 |
if err != nil { |
| 40 |
log.Fatal(err) |
| 41 |
} |
| 42 |
defer topic.Shutdown(ctx) |
| 43 |
} |
| 44 |
|
| 45 |
func ExampleOpenSubscription() { |
| 46 |
// PRAGMA: This example is used on gocloud.dev; PRAGMA comments adjust how it is shown and can be ignored. |
| 47 |
// PRAGMA: On gocloud.dev, hide lines until the next blank line. |
| 48 |
ctx := context.Background() |
| 49 |
|
| 50 |
natsConn, err := nats.Connect("nats://nats.example.com") |
| 51 |
if err != nil { |
| 52 |
log.Fatal(err) |
| 53 |
} |
| 54 |
defer natsConn.Close() |
| 55 |
|
| 56 |
subscription, err := natspubsub.OpenSubscription( |
| 57 |
natsConn, |
| 58 |
"example.mysubject", |
| 59 |
nil) |
| 60 |
if err != nil { |
| 61 |
log.Fatal(err) |
| 62 |
} |
| 63 |
defer subscription.Shutdown(ctx) |
| 64 |
} |
| 65 |
|
| 66 |
func ExampleOpenSubscription_queue() { |
| 67 |
// PRAGMA: This example is used on gocloud.dev; PRAGMA comments adjust how it is shown and can be ignored. |
| 68 |
// PRAGMA: On gocloud.dev, hide lines until the next blank line. |
| 69 |
ctx := context.Background() |
| 70 |
|
| 71 |
natsConn, err := nats.Connect("nats://nats.example.com") |
| 72 |
if err != nil { |
| 73 |
log.Fatal(err) |
| 74 |
} |
| 75 |
defer natsConn.Close() |
| 76 |
|
| 77 |
subscription, err := natspubsub.OpenSubscription( |
| 78 |
natsConn, |
| 79 |
"example.mysubject", |
| 80 |
&natspubsub.SubscriptionOptions{Queue: "queue1"}) |
| 81 |
if err != nil { |
| 82 |
log.Fatal(err) |
| 83 |
} |
| 84 |
defer subscription.Shutdown(ctx) |
| 85 |
} |
| 86 |
|
| 87 |
func Example_openTopicFromURL() { |
| 88 |
// PRAGMA: This example is used on gocloud.dev; PRAGMA comments adjust how it is shown and can be ignored. |
| 89 |
// PRAGMA: On gocloud.dev, add a blank import: _ "gocloud.dev/pubsub/natspubsub" |
| 90 |
// PRAGMA: On gocloud.dev, hide lines until the next blank line. |
| 91 |
ctx := context.Background() |
| 92 |
|
| 93 |
// pubsub.OpenTopic creates a *pubsub.Topic from a URL. |
| 94 |
// This URL will Dial the NATS server at the URL in the environment variable |
| 95 |
// NATS_SERVER_URL and send messages with subject "example.mysubject". |
| 96 |
topic, err := pubsub.OpenTopic(ctx, "nats://example.mysubject") |
| 97 |
if err != nil { |
| 98 |
log.Fatal(err) |
| 99 |
} |
| 100 |
defer topic.Shutdown(ctx) |
| 101 |
} |
| 102 |
|
| 103 |
func Example_openSubscriptionFromURL() { |
| 104 |
// PRAGMA: This example is used on gocloud.dev; PRAGMA comments adjust how it is shown and can be ignored. |
| 105 |
// PRAGMA: On gocloud.dev, add a blank import: _ "gocloud.dev/pubsub/natspubsub" |
| 106 |
// PRAGMA: On gocloud.dev, hide lines until the next blank line. |
| 107 |
ctx := context.Background() |
| 108 |
|
| 109 |
// pubsub.OpenSubscription creates a *pubsub.Subscription from a URL. |
| 110 |
// This URL will Dial the NATS server at the URL in the environment variable |
| 111 |
// NATS_SERVER_URL and receive messages with subject "example.mysubject". |
| 112 |
subscription, err := pubsub.OpenSubscription(ctx, "nats://example.mysubject") |
| 113 |
if err != nil { |
| 114 |
log.Fatal(err) |
| 115 |
} |
| 116 |
defer subscription.Shutdown(ctx) |
| 117 |
} |
| 118 |
|
| 119 |
func Example_openQueueSubscriptionFromURL() { |
| 120 |
// PRAGMA: This example is used on gocloud.dev; PRAGMA comments adjust how it is shown and can be ignored. |
| 121 |
// PRAGMA: On gocloud.dev, add a blank import: _ "gocloud.dev/pubsub/natspubsub" |
| 122 |
// PRAGMA: On gocloud.dev, hide lines until the next blank line. |
| 123 |
ctx := context.Background() |
| 124 |
|
| 125 |
// pubsub.OpenSubscription creates a *pubsub.Subscription from a URL. |
| 126 |
// This URL will Dial the NATS server at the URL in the environment variable |
| 127 |
// NATS_SERVER_URL and receive messages with subject "example.mysubject" |
| 128 |
// This URL will be parsed and the queue attribute will be used as the Queue parameter when creating the NATS Subscription. |
| 129 |
subscription, err := pubsub.OpenSubscription(ctx, "nats://example.mysubject?queue=myqueue") |
| 130 |
if err != nil { |
| 131 |
log.Fatal(err) |
| 132 |
} |
| 133 |
defer subscription.Shutdown(ctx) |
| 134 |
} |
| 135 |
|
| 136 |
func ExampleOpenSubscriptionV2() { |
| 137 |
// PRAGMA: This example is used on gocloud.dev; PRAGMA comments adjust how it is shown and can be ignored. |
| 138 |
// PRAGMA: On gocloud.dev, add a blank import: _ "gocloud.dev/pubsub/natspubsub" |
| 139 |
// PRAGMA: On gocloud.dev, hide lines until the next blank line. |
| 140 |
ctx := context.Background() |
| 141 |
natsConn, err := nats.Connect("nats://nats.example.com") |
| 142 |
if err != nil { |
| 143 |
log.Fatal(err) |
| 144 |
} |
| 145 |
defer natsConn.Close() |
| 146 |
|
| 147 |
subscription, err := natspubsub.OpenSubscriptionV2( |
| 148 |
natsConn, |
| 149 |
"example.mysubject", |
| 150 |
nil) |
| 151 |
if err != nil { |
| 152 |
log.Fatal(err) |
| 153 |
} |
| 154 |
defer subscription.Shutdown(ctx) |
| 155 |
} |
| 156 |
|
| 157 |
func ExampleOpenTopicV2() { |
| 158 |
// PRAGMA: This example is used on gocloud.dev; PRAGMA comments adjust how it is shown and can be ignored. |
| 159 |
// PRAGMA: On gocloud.dev, add a blank import: _ "gocloud.dev/pubsub/natspubsub" |
| 160 |
// PRAGMA: On gocloud.dev, hide lines until the next blank line. |
| 161 |
ctx := context.Background() |
| 162 |
|
| 163 |
natsConn, err := nats.Connect("nats://nats.example.com") |
| 164 |
if err != nil { |
| 165 |
log.Fatal(err) |
| 166 |
} |
| 167 |
defer natsConn.Close() |
| 168 |
|
| 169 |
topic, err := natspubsub.OpenTopicV2(natsConn, "example.mysubject", nil) |
| 170 |
if err != nil { |
| 171 |
log.Fatal(err) |
| 172 |
} |
| 173 |
defer topic.Shutdown(ctx) |
| 174 |
} |
| 175 |
|
| 176 |
func Example_openTopicV2FromURL() { |
| 177 |
// PRAGMA: This example is used on gocloud.dev; PRAGMA comments adjust how it is shown and can be ignored. |
| 178 |
// PRAGMA: On gocloud.dev, add a blank import: _ "gocloud.dev/pubsub/natspubsub" |
| 179 |
// PRAGMA: On gocloud.dev, hide lines until the next blank line. |
| 180 |
ctx := context.Background() |
| 181 |
|
| 182 |
// pubsub.OpenTopic creates a *pubsub.Topic from a URL. |
| 183 |
// This URL will Dial the NATS server at the URL in the environment variable |
| 184 |
// NATS_SERVER_URL and send messages with subject "example.mysubject". |
| 185 |
// This URL will be parsed and the natsv2 attribute will be used to |
| 186 |
// use NATS v2.2.0+ native message headers as the message metadata. |
| 187 |
topic, err := pubsub.OpenTopic(ctx, "nats://example.mysubject?natsv2") |
| 188 |
if err != nil { |
| 189 |
log.Fatal(err) |
| 190 |
} |
| 191 |
defer topic.Shutdown(ctx) |
| 192 |
} |
| 193 |
|
| 194 |
func Example_openSubscriptionV2FromURL() { |
| 195 |
// PRAGMA: This example is used on gocloud.dev; PRAGMA comments adjust how it is shown and can be ignored. |
| 196 |
// PRAGMA: On gocloud.dev, add a blank import: _ "gocloud.dev/pubsub/natspubsub" |
| 197 |
// PRAGMA: On gocloud.dev, hide lines until the next blank line. |
| 198 |
ctx := context.Background() |
| 199 |
|
| 200 |
// pubsub.OpenSubscription creates a *pubsub.Subscription from a URL. |
| 201 |
// This URL will Dial the NATS server at the URL in the environment variable |
| 202 |
// NATS_SERVER_URL and receive messages with subject "example.mysubject". |
| 203 |
// This URL will be parsed and the natsv2 attribute will be used to |
| 204 |
// use NATS v2.2.0+ native message headers as the message metadata. |
| 205 |
subscription, err := pubsub.OpenSubscription(ctx, "nats://example.mysubject?natsv2") |
| 206 |
if err != nil { |
| 207 |
log.Fatal(err) |
| 208 |
} |
| 209 |
defer subscription.Shutdown(ctx) |
| 210 |
} |