“I'm late! I'm late! For a very important date!" - White Rabbit.
Alice is a wrapper around the Streadway amqp package, designed to be easier to use and offer automatic handling of many errors out of the box.
Credit for the cute Gopher goes to Ian Derksen
- Automatic broker reconnect (attempted at a user-defined interval)
- Automatic producer and consumer reconnect upon channel error
- Every message handled in a new routine
- Separate TCP connections for producers and consumers
- Queues and exchanges are objects, which can be reused for multiple consumers and/or producers
- Fully mocked broker, consumer and producer for testing
go get github.com/thijsheijden/alice
package main
import (
"fmt"
"log"
"time"
"github.com/streadway/amqp"
"github.com/thijsheijden/alice"
)
func main() {
// Turn on logging
alice.SetLogging()
// Create a connection configuration
connectionConfig := alice.CreateConfig(
"guest",
"guest",
"localhost:5672",
5672,
true,
time.Second*10,
alice.DefaultErrorHandler,
)
// Create a broker using the connection config
broker, err := alice.CreateBroker(connectionConfig)
if err != nil {
log.Println(err)
}
// Create an exchange called 'test-exchange' using direct routing
exchange, err := alice.CreateExchange("test-exchange", alice.Direct, false, true, false, false, nil)
if err != nil {
log.Println(err)
}
// Create a queue called 'test-queue'
q := alice.CreateQueue(exchange, "test-queue", false, false, true, false, nil)
// Create a consumer bound to this queue, listening for messages with routing key 'key'
c, err := broker.CreateConsumer(q, "key", "consumer-tag", alice.DefaultConsumerErrorHandler)
// Start consuming messages
// Every received message is passed to the handleMessage function
go c.ConsumeMessages(nil, false, handleMessage)
select {}
}
func handleMessage(msg amqp.Delivery) {
fmt.Println(msg.Body)
msg.Ack(true)
}