| 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 rabbitpubsub_test |
| 16 |
|
| 17 |
import ( |
| 18 |
"context" |
| 19 |
"log" |
| 20 |
|
| 21 |
amqp "github.com/rabbitmq/amqp091-go" |
| 22 |
"gocloud.dev/pubsub" |
| 23 |
"gocloud.dev/pubsub/rabbitpubsub" |
| 24 |
) |
| 25 |
|
| 26 |
func ExampleOpenTopic() { |
| 27 |
// PRAGMA: This example is used on gocloud.dev; PRAGMA comments adjust how it is shown and can be ignored. |
| 28 |
// PRAGMA: On gocloud.dev, hide lines until the next blank line. |
| 29 |
ctx := context.Background() |
| 30 |
|
| 31 |
rabbitConn, err := amqp.Dial("amqp://guest:guest@localhost:5672/") |
| 32 |
if err != nil { |
| 33 |
log.Fatal(err) |
| 34 |
} |
| 35 |
defer rabbitConn.Close() |
| 36 |
topic := rabbitpubsub.OpenTopic(rabbitConn, "myexchange", nil) |
| 37 |
defer topic.Shutdown(ctx) |
| 38 |
} |
| 39 |
|
| 40 |
func ExampleOpenSubscription() { |
| 41 |
// PRAGMA: This example is used on gocloud.dev; PRAGMA comments adjust how it is shown and can be ignored. |
| 42 |
// PRAGMA: On gocloud.dev, hide lines until the next blank line. |
| 43 |
ctx := context.Background() |
| 44 |
|
| 45 |
rabbitConn, err := amqp.Dial("amqp://guest:guest@localhost:5672/") |
| 46 |
if err != nil { |
| 47 |
log.Fatal(err) |
| 48 |
} |
| 49 |
defer rabbitConn.Close() |
| 50 |
subscription := rabbitpubsub.OpenSubscription(rabbitConn, "myqueue", nil) |
| 51 |
defer subscription.Shutdown(ctx) |
| 52 |
} |
| 53 |
|
| 54 |
func Example_openTopicFromURL() { |
| 55 |
// PRAGMA: This example is used on gocloud.dev; PRAGMA comments adjust how it is shown and can be ignored. |
| 56 |
// PRAGMA: On gocloud.dev, add a blank import: _ "gocloud.dev/pubsub/rabbitpubsub" |
| 57 |
// PRAGMA: On gocloud.dev, hide lines until the next blank line. |
| 58 |
ctx := context.Background() |
| 59 |
|
| 60 |
// pubsub.OpenTopic creates a *pubsub.Topic from a URL. |
| 61 |
// This URL will Dial the RabbitMQ server at the URL in the environment |
| 62 |
// variable RABBIT_SERVER_URL and open the exchange "myexchange". |
| 63 |
topic, err := pubsub.OpenTopic(ctx, "rabbit://myexchange") |
| 64 |
if err != nil { |
| 65 |
log.Fatal(err) |
| 66 |
} |
| 67 |
defer topic.Shutdown(ctx) |
| 68 |
} |
| 69 |
|
| 70 |
func Example_openSubscriptionFromURL() { |
| 71 |
// PRAGMA: This example is used on gocloud.dev; PRAGMA comments adjust how it is shown and can be ignored. |
| 72 |
// PRAGMA: On gocloud.dev, add a blank import: _ "gocloud.dev/pubsub/rabbitpubsub" |
| 73 |
// PRAGMA: On gocloud.dev, hide lines until the next blank line. |
| 74 |
ctx := context.Background() |
| 75 |
|
| 76 |
// pubsub.OpenSubscription creates a *pubsub.Subscription from a URL. |
| 77 |
// This URL will Dial the RabbitMQ server at the URL in the environment |
| 78 |
// variable RABBIT_SERVER_URL and open the queue "myqueue". |
| 79 |
subscription, err := pubsub.OpenSubscription(ctx, "rabbit://myqueue") |
| 80 |
if err != nil { |
| 81 |
log.Fatal(err) |
| 82 |
} |
| 83 |
defer subscription.Shutdown(ctx) |
| 84 |
} |