Skip to content
Wrapper for RabbitMQ using streadway/amqp package.
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
examples
.gitignore
.travis.yml
Gopkg.lock
Gopkg.toml
LICENSE.md
README.md
rabbitmq.go

README.md

Build Status

Overview

A simple wrapper to streadway/amqp for RabbitMQ with support for auto reconnections.

Usage

The library maintains a single connection and channel. It also maintains a map of bindings of exchanges and queues added by the user. Each binding can be configured to be a producer, a consumer, or both.

First, create the connection object with:

// replace the values below with your own
c := rmq.New(&rmq.Config{
		Host:     "localhost",
		Port:     5672,
		Username: "guest",
		Password: "guest",
		Vhost:    "/",
})

err := c.Connect()
...

Next, we will create a binding for a direct-type exchange test-exchange and queue qtest with:

bindId, err := c.AddBinding(&rmq.BindConfig{
		ExchangeOpt: &rmq.ExchangeOptions{
			Name:       "test-exchange",
			Type:       "direct",
			Durable:    false,
			AutoDelete: true,
		},
		QueueOpt: &rmq.QueueOptions{
			QueueName:  "qtest",
			Durable:    false,
			AutoDelete: true,
		},
		QueueBindOpt: &rmq.QueueBindOptions{
			RoutingKey: "rk1",
		},
		// when `ConsumeOpt` is provided, this binding is able to receive
		// messages from the specified exchange/queue
		ConsumeOpt: &rmq.ConsumeOptions{
			ClientTag:  "consumer1",
			FnCallback: func (b []byte) {
				log.Printf("payload: %s", b)
			},
		},
})

// send a message using the binding above
c.Send(bindId, "rk1", []byte("hello world"))

You can also create a send-only binding with:

bindId, err := c.AddBinding(&rmq.BindConfig{
		ExchangeOpt: &rmq.ExchangeOptions{
			Name:       "test-exchange",
			Type:       "direct",
			Durable:    false,
			AutoDelete: true,
		},
		QueueOpt: &rmq.QueueOptions{
			QueueName:  "queue1",
			Durable:    false,
			AutoDelete: true,
		},
		QueueBindOpt: &rmq.QueueBindOptions{
			RoutingKey: "rk1",
		},
})

c.Send(bindId, "rk1", []byte("hello world"))

See the examples directory for a simple receiver/sender implementation.

You can’t perform that action at this time.