update documentation [skip ci] (#30)
* update documentation [skip ci] --------- Signed-off-by: Gabriele Santomaggio <G.santomaggio@gmail.com>
This commit is contained in:
parent
54eab6556d
commit
b108942f04
|
|
@ -8,6 +8,15 @@ Suitable for testing in pre-production environments.
|
|||
|
||||
- [Getting Started](docs/examples/getting_started)
|
||||
- [Examples](docs/examples)
|
||||
- Getting started Video tutorial: </br>
|
||||
[](https://youtu.be/iR1JUFh3udI)
|
||||
|
||||
|
||||
|
||||
## Documentation
|
||||
|
||||
- [Client Guide](https://www.rabbitmq.com/client-libraries/amqp-client-libraries) (work in progress for this client)
|
||||
|
||||
|
||||
|
||||
# Packages
|
||||
|
|
|
|||
|
|
@ -5,4 +5,5 @@
|
|||
- [Reliable](reliable) - An example of how to deal with reconnections and error handling.
|
||||
- [Streams](streams) - An example of how to use [RabbitMQ Streams](https://www.rabbitmq.com/docs/streams) with AMQP 1.0
|
||||
- [Stream Filtering](streams_filtering) - An example of how to use streams [Filter Expressions](https://www.rabbitmq.com/blog/2024/12/13/amqp-filter-expressions)
|
||||
- [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.
|
||||
- [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.
|
||||
- [Video](video) - From the YouTube tutorial [AMQP 1.0 with Golang](https://youtu.be/iR1JUFh3udI)
|
||||
|
|
@ -0,0 +1,129 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
rmq "github.com/rabbitmq/rabbitmq-amqp-go-client/pkg/rabbitmqamqp"
|
||||
)
|
||||
|
||||
func main() {
|
||||
exchangeName := "getting-started-go-exchange"
|
||||
queueName := "getting-started-go-queue"
|
||||
routingKey := "routing-key"
|
||||
|
||||
env := rmq.NewEnvironment([]string{"amqp://guest:guest@localhost:5672"}, nil)
|
||||
|
||||
// Open a connection to the AMQP 1.0 server ( RabbitMQ >= 4.0)
|
||||
amqpConnection, err := env.NewConnection(context.Background())
|
||||
if err != nil {
|
||||
rmq.Error("Error opening connection", err)
|
||||
return
|
||||
}
|
||||
|
||||
// Create the management interface for the connection
|
||||
// so we can declare exchanges, queues, and bindings
|
||||
management := amqpConnection.Management()
|
||||
// TopicExchangeSpecification but can be also DirectExchangeSpecification/FanOutExchangeSpecification
|
||||
_, err = management.DeclareExchange(context.Background(), &rmq.TopicExchangeSpecification{
|
||||
Name: exchangeName,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
rmq.Error("Error declaring exchange", err)
|
||||
return
|
||||
}
|
||||
|
||||
// Declare a Quorum queue
|
||||
// QuorumQueueSpecification but can be also ClassicQueueSpecification,
|
||||
// AutoGeneratedQueueSpecification, and StreamQueueSpecification
|
||||
_, err = management.DeclareQueue(context.Background(), &rmq.QuorumQueueSpecification{
|
||||
Name: queueName,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
rmq.Error("Error declaring queue", err)
|
||||
return
|
||||
}
|
||||
|
||||
// Bind the queue to the exchange
|
||||
bindingPath, err := management.Bind(context.Background(), &rmq.ExchangeToQueueBindingSpecification{
|
||||
SourceExchange: exchangeName,
|
||||
DestinationQueue: queueName,
|
||||
BindingKey: routingKey,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
rmq.Error("Error binding", err)
|
||||
return
|
||||
}
|
||||
|
||||
publisher, err := amqpConnection.NewPublisher(context.Background(), &rmq.ExchangeAddress{
|
||||
Exchange: exchangeName,
|
||||
Key: routingKey,
|
||||
}, "getting-started-publisher")
|
||||
if err != nil {
|
||||
rmq.Error("Error creating publisher", err)
|
||||
return
|
||||
}
|
||||
|
||||
for i := 0; i < 10; i++ {
|
||||
publishResult, err := publisher.Publish(context.Background(),
|
||||
rmq.NewMessage([]byte(fmt.Sprint("Hello AMQP 1.0 - id:", i))))
|
||||
if err != nil {
|
||||
rmq.Error("Error publishing message", err)
|
||||
return
|
||||
}
|
||||
|
||||
switch publishResult.Outcome.(type) {
|
||||
// publish result
|
||||
case *rmq.StateAccepted:
|
||||
rmq.Info("Message accepted", "message", publishResult.Message.GetData())
|
||||
default:
|
||||
rmq.Error("Message not accepted", "outcome", publishResult.Outcome)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
consumer, err := amqpConnection.NewConsumer(context.Background(), queueName, nil)
|
||||
|
||||
if err != nil {
|
||||
rmq.Error("Error creating consumer", err)
|
||||
return
|
||||
}
|
||||
|
||||
for i := 0; i < 10; i++ {
|
||||
deliveryContext, err := consumer.Receive(context.Background())
|
||||
if err != nil {
|
||||
rmq.Error("Error receiving message", err)
|
||||
return
|
||||
}
|
||||
rmq.Info("Received message", "message", deliveryContext.Message().GetData())
|
||||
// Accept the message. Message will be removed from the queue
|
||||
err = deliveryContext.Accept(context.Background())
|
||||
if err != nil {
|
||||
rmq.Error("Error accepting message", err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// Close the publisher
|
||||
_ = publisher.Close(context.Background())
|
||||
|
||||
// Close the consumer
|
||||
_ = consumer.Close(context.Background())
|
||||
|
||||
// Delete binding
|
||||
_ = management.Unbind(context.Background(), bindingPath)
|
||||
|
||||
// Delete the queue
|
||||
_ = management.DeleteQueue(context.Background(), queueName)
|
||||
|
||||
// Delete the exchange
|
||||
_ = management.DeleteExchange(context.Background(), exchangeName)
|
||||
|
||||
// Close the connection
|
||||
_ = amqpConnection.Close(context.Background())
|
||||
|
||||
// close the connection with env
|
||||
_ = env.CloseConnections(context.Background())
|
||||
}
|
||||
Loading…
Reference in New Issue