|
1 | 1 | package main |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "bufio" |
5 | 4 | "context" |
6 | 5 | "fmt" |
7 | | - mq "github.com/rabbitmq/rabbitmq-amqp-go-client/rabbitmq_amqp" |
8 | | - "os" |
| 6 | + "github.com/rabbitmq/rabbitmq-amqp-go-client/rabbitmq_amqp" |
9 | 7 | "time" |
10 | 8 | ) |
11 | 9 |
|
12 | 10 | func main() { |
13 | 11 | fmt.Printf("Getting started with AMQP Go AMQP 1.0 Client\n") |
14 | | - chStatusChanged := make(chan *mq.StatusChanged, 1) |
| 12 | + chStatusChanged := make(chan *rabbitmq_amqp.StatusChanged, 1) |
15 | 13 |
|
16 | | - go func(ch chan *mq.StatusChanged) { |
| 14 | + go func(ch chan *rabbitmq_amqp.StatusChanged) { |
17 | 15 | for statusChanged := range ch { |
18 | | - fmt.Printf("Status changed from %d to %d\n", statusChanged.From, statusChanged.To) |
| 16 | + fmt.Printf("%s\n", statusChanged) |
19 | 17 | } |
20 | 18 | }(chStatusChanged) |
21 | 19 |
|
22 | | - amqpConnection := mq.NewAmqpConnection() |
23 | | - amqpConnection.NotifyStatusChange(chStatusChanged) |
24 | | - err := amqpConnection.Open(context.Background(), mq.NewConnectionSettings()) |
| 20 | + amqpConnection := rabbitmq_amqp.NewAmqpConnectionNotifyStatusChanged(chStatusChanged) |
| 21 | + err := amqpConnection.Open(context.Background(), rabbitmq_amqp.NewConnectionSettings()) |
25 | 22 | if err != nil { |
| 23 | + fmt.Printf("Error opening connection: %v\n", err) |
26 | 24 | return |
27 | 25 | } |
28 | | - |
29 | 26 | fmt.Printf("AMQP Connection opened.\n") |
30 | 27 | management := amqpConnection.Management() |
31 | | - queueSpec := management.Queue("getting_started_queue"). |
32 | | - QueueType(mq.QueueType{Type: mq.Quorum}). |
33 | | - MaxLengthBytes(mq.CapacityGB(1)) |
34 | | - exchangeSpec := management.Exchange("getting_started_exchange"). |
35 | | - ExchangeType(mq.ExchangeType{Type: mq.Topic}) |
36 | | - |
37 | | - queueInfo, err := queueSpec.Declare(context.Background()) |
| 28 | + exchangeInfo, err := management.DeclareExchange(context.TODO(), &rabbitmq_amqp.ExchangeSpecification{ |
| 29 | + Name: "getting-started-exchange", |
| 30 | + }) |
38 | 31 | if err != nil { |
39 | | - fmt.Printf("Error declaring queue %s\n", err) |
| 32 | + fmt.Printf("Error declaring exchange: %v\n", err) |
40 | 33 | return |
41 | 34 | } |
42 | | - fmt.Printf("Queue %s created.\n", queueInfo.GetName()) |
43 | 35 |
|
44 | | - exchangeInfo, err := exchangeSpec.Declare(context.Background()) |
| 36 | + queueInfo, err := management.DeclareQueue(context.TODO(), &rabbitmq_amqp.QueueSpecification{ |
| 37 | + Name: "getting-started-queue", |
| 38 | + QueueType: rabbitmq_amqp.QueueType{Type: rabbitmq_amqp.Quorum}, |
| 39 | + }) |
| 40 | + |
45 | 41 | if err != nil { |
46 | | - fmt.Printf("Error declaring exchange %s\n", err) |
| 42 | + fmt.Printf("Error declaring queue: %v\n", err) |
47 | 43 | return |
48 | 44 | } |
49 | | - fmt.Printf("Exchange %s created.\n", exchangeInfo.GetName()) |
50 | 45 |
|
51 | | - bindingSpec := management.Binding().SourceExchange(exchangeSpec).DestinationQueue(queueSpec).Key("routing-key") |
| 46 | + bindingPath, err := management.Bind(context.TODO(), &rabbitmq_amqp.BindingSpecification{ |
| 47 | + SourceExchange: exchangeInfo.Name(), |
| 48 | + DestinationQueue: queueInfo.Name(), |
| 49 | + BindingKey: "routing-key", |
| 50 | + }) |
52 | 51 |
|
53 | | - err = bindingSpec.Bind(context.Background()) |
54 | 52 | if err != nil { |
55 | | - fmt.Printf("Error binding %s\n", err) |
| 53 | + fmt.Printf("Error binding: %v\n", err) |
56 | 54 | return |
57 | 55 | } |
58 | 56 |
|
59 | | - fmt.Printf("Binding between %s and %s created.\n", exchangeInfo.GetName(), queueInfo.GetName()) |
60 | | - |
61 | | - fmt.Println("Press any key to cleanup and exit") |
62 | | - reader := bufio.NewReader(os.Stdin) |
63 | | - _, _ = reader.ReadString('\n') |
| 57 | + err = management.Unbind(context.TODO(), bindingPath) |
64 | 58 |
|
65 | | - err = bindingSpec.Unbind(context.Background()) |
66 | 59 | if err != nil { |
67 | | - fmt.Printf("Error unbinding %s\n", err) |
| 60 | + fmt.Printf("Error unbinding: %v\n", err) |
68 | 61 | return |
69 | 62 | } |
70 | 63 |
|
71 | | - fmt.Printf("Binding between %s and %s deleted.\n", exchangeInfo.GetName(), queueInfo.GetName()) |
72 | | - |
73 | | - err = exchangeSpec.Delete(context.Background()) |
| 64 | + err = management.DeleteExchange(context.TODO(), exchangeInfo.Name()) |
74 | 65 | if err != nil { |
75 | | - fmt.Printf("Error deleting exchange %s\n", err) |
| 66 | + fmt.Printf("Error deleting exchange: %v\n", err) |
76 | 67 | return |
77 | 68 | } |
78 | 69 |
|
79 | | - err = queueSpec.Delete(context.Background()) |
| 70 | + err = management.DeleteQueue(context.TODO(), queueInfo.Name()) |
80 | 71 | if err != nil { |
| 72 | + fmt.Printf("Error deleting queue: %v\n", err) |
81 | 73 | return |
82 | 74 | } |
83 | | - fmt.Printf("Queue %s deleted.\n", queueInfo.GetName()) |
84 | 75 |
|
85 | 76 | err = amqpConnection.Close(context.Background()) |
86 | 77 | if err != nil { |
| 78 | + fmt.Printf("Error closing connection: %v\n", err) |
87 | 79 | return |
88 | 80 | } |
| 81 | + |
89 | 82 | fmt.Printf("AMQP Connection closed.\n") |
90 | 83 | // Wait for the status change to be printed |
91 | 84 | time.Sleep(500 * time.Millisecond) |
| 85 | + |
92 | 86 | close(chStatusChanged) |
93 | 87 | } |
0 commit comments