Skip to content

Commit b108942

Browse files
authored
update documentation [skip ci] (#30)
* update documentation [skip ci] --------- Signed-off-by: Gabriele Santomaggio <[email protected]>
1 parent 54eab65 commit b108942

File tree

3 files changed

+140
-1
lines changed

3 files changed

+140
-1
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,15 @@ Suitable for testing in pre-production environments.
88

99
- [Getting Started](docs/examples/getting_started)
1010
- [Examples](docs/examples)
11+
- Getting started Video tutorial: </br>
12+
[![Getting Started](https://img.youtube.com/vi/iR1JUFh3udI/0.jpg)](https://youtu.be/iR1JUFh3udI)
13+
14+
15+
16+
## Documentation
17+
18+
- [Client Guide](https://www.rabbitmq.com/client-libraries/amqp-client-libraries) (work in progress for this client)
19+
1120

1221

1322
# Packages

docs/examples/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@
55
- [Reliable](reliable) - An example of how to deal with reconnections and error handling.
66
- [Streams](streams) - An example of how to use [RabbitMQ Streams](https://www.rabbitmq.com/docs/streams) with AMQP 1.0
77
- [Stream Filtering](streams_filtering) - An example of how to use streams [Filter Expressions](https://www.rabbitmq.com/blog/2024/12/13/amqp-filter-expressions)
8-
- [Publisher per message target](publisher_msg_targets) - An example of how to use a single publisher to send messages in different queues with the address to the message target in the message properties.
8+
- [Publisher per message target](publisher_msg_targets) - An example of how to use a single publisher to send messages in different queues with the address to the message target in the message properties.
9+
- [Video](video) - From the YouTube tutorial [AMQP 1.0 with Golang](https://youtu.be/iR1JUFh3udI)
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"fmt"
6+
rmq "github.com/rabbitmq/rabbitmq-amqp-go-client/pkg/rabbitmqamqp"
7+
)
8+
9+
func main() {
10+
exchangeName := "getting-started-go-exchange"
11+
queueName := "getting-started-go-queue"
12+
routingKey := "routing-key"
13+
14+
env := rmq.NewEnvironment([]string{"amqp://guest:guest@localhost:5672"}, nil)
15+
16+
// Open a connection to the AMQP 1.0 server ( RabbitMQ >= 4.0)
17+
amqpConnection, err := env.NewConnection(context.Background())
18+
if err != nil {
19+
rmq.Error("Error opening connection", err)
20+
return
21+
}
22+
23+
// Create the management interface for the connection
24+
// so we can declare exchanges, queues, and bindings
25+
management := amqpConnection.Management()
26+
// TopicExchangeSpecification but can be also DirectExchangeSpecification/FanOutExchangeSpecification
27+
_, err = management.DeclareExchange(context.Background(), &rmq.TopicExchangeSpecification{
28+
Name: exchangeName,
29+
})
30+
31+
if err != nil {
32+
rmq.Error("Error declaring exchange", err)
33+
return
34+
}
35+
36+
// Declare a Quorum queue
37+
// QuorumQueueSpecification but can be also ClassicQueueSpecification,
38+
// AutoGeneratedQueueSpecification, and StreamQueueSpecification
39+
_, err = management.DeclareQueue(context.Background(), &rmq.QuorumQueueSpecification{
40+
Name: queueName,
41+
})
42+
43+
if err != nil {
44+
rmq.Error("Error declaring queue", err)
45+
return
46+
}
47+
48+
// Bind the queue to the exchange
49+
bindingPath, err := management.Bind(context.Background(), &rmq.ExchangeToQueueBindingSpecification{
50+
SourceExchange: exchangeName,
51+
DestinationQueue: queueName,
52+
BindingKey: routingKey,
53+
})
54+
55+
if err != nil {
56+
rmq.Error("Error binding", err)
57+
return
58+
}
59+
60+
publisher, err := amqpConnection.NewPublisher(context.Background(), &rmq.ExchangeAddress{
61+
Exchange: exchangeName,
62+
Key: routingKey,
63+
}, "getting-started-publisher")
64+
if err != nil {
65+
rmq.Error("Error creating publisher", err)
66+
return
67+
}
68+
69+
for i := 0; i < 10; i++ {
70+
publishResult, err := publisher.Publish(context.Background(),
71+
rmq.NewMessage([]byte(fmt.Sprint("Hello AMQP 1.0 - id:", i))))
72+
if err != nil {
73+
rmq.Error("Error publishing message", err)
74+
return
75+
}
76+
77+
switch publishResult.Outcome.(type) {
78+
// publish result
79+
case *rmq.StateAccepted:
80+
rmq.Info("Message accepted", "message", publishResult.Message.GetData())
81+
default:
82+
rmq.Error("Message not accepted", "outcome", publishResult.Outcome)
83+
}
84+
85+
}
86+
87+
consumer, err := amqpConnection.NewConsumer(context.Background(), queueName, nil)
88+
89+
if err != nil {
90+
rmq.Error("Error creating consumer", err)
91+
return
92+
}
93+
94+
for i := 0; i < 10; i++ {
95+
deliveryContext, err := consumer.Receive(context.Background())
96+
if err != nil {
97+
rmq.Error("Error receiving message", err)
98+
return
99+
}
100+
rmq.Info("Received message", "message", deliveryContext.Message().GetData())
101+
// Accept the message. Message will be removed from the queue
102+
err = deliveryContext.Accept(context.Background())
103+
if err != nil {
104+
rmq.Error("Error accepting message", err)
105+
return
106+
}
107+
}
108+
109+
// Close the publisher
110+
_ = publisher.Close(context.Background())
111+
112+
// Close the consumer
113+
_ = consumer.Close(context.Background())
114+
115+
// Delete binding
116+
_ = management.Unbind(context.Background(), bindingPath)
117+
118+
// Delete the queue
119+
_ = management.DeleteQueue(context.Background(), queueName)
120+
121+
// Delete the exchange
122+
_ = management.DeleteExchange(context.Background(), exchangeName)
123+
124+
// Close the connection
125+
_ = amqpConnection.Close(context.Background())
126+
127+
// close the connection with env
128+
_ = env.CloseConnections(context.Background())
129+
}

0 commit comments

Comments
 (0)