aridwiprayogo / go-rabbitmq

Golang AMQP wrapper for RabbitMQ with better API

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

go-rabbitmq

Golang AMQP wrapper for RabbitMQ with better API

Table of Contents

Background

In Golang, to use RabbitMQ with AMQP has advantages, especially in messaging systems. It's done with the AMQP connector. But, the problem is it has a less convenient API. Programmers have to write something that should be set by default. For example, when creating a queue on RabbitMQ. We have to do this.

q, err := ch.QueueDeclare(
  "hello", // name
  false,   // durable
  false,   // delete when unused
  false,   // exclusive
  false,   // no-wait
  nil,     // arguments
)
failOnError(err, "Failed to declare a queue")

Too many false in there, which should be set as the default value.

By using this module, we can do same think with less code. See above.

q, err := mq.NewQueue(
  ch,
  &gorabbitmq.MQConfigQueue{
   Name: "hello",
  },
)
failOnError(err, "Failed to declare a queue")

No need to write false, because it is the default value.

So, to conclude, this module makes it easy to use amqp for rabbitmq.

Features

  • Built on top of famous AMQP connector in Go.
  • All object reference like connection, channel, queue, etc are original by the AMQP connector (no monkey patch or any modifications).
  • It has construction API and API with builder pattern.
  • Does not modify incoming messages, so it can be controlled manually.

Usage

Installation

Inside terminal emulator, simply run command below.

go get github.com/hadihammurabi/go-rabbitmq

After installation it can be imported into any Go project. For example.

package main

import (
 rabbitmq "github.com/hadihammurabi/go-rabbitmq"
)

Connect to RabbitMQ

It can do as below.

mq, err := rabbitmq.NewMQ(&rabbitmq.MQConfigConnection{
	URL: "amqp://guest:guest@localhost:5672/",
})
if err != nil {
 log.Fatal(err)
}

// don't forget to close the connection and channel
defer func() {
 mq.GetConnection().Close()
 mq.GetChannel().Close()
}()

Declare Queue

Queue declaration can be done like this, after connecting to mq of course.

It only connects to the queue if the queue exists or create one if it doesn't exist. (RabbitMQ behavior)

q, err := rabbitmq.NewQueue(mq.GetChannel(), &rabbitmq.MQConfigQueue{
  Name: "hello",
})
if err != nil {
 log.Fatal(err)
}

Declare Exchange

Exchange declaration can be done like this, after connecting to mq of course.

err := rabbitmq.NewExchange(mq.GetChannel(), &rabbitmq.MQConfigExchange{
  Name: "hello",
  Type: rabbitmq.ExchangeTypeFanout,
})
if err != nil {
 log.Fatal(err)
}

Bind Queue to Exchange

Every message published to exchange will be distributed to every bound queue. To bind queue with exchange, follow example below.

err := rabbitmq.NewQueueBind(mq.GetChannel(), &rabbitmq.MQConfigBind{
  Name:     q.Name,
  Exchange: "hello",
})
if err != nil {
 log.Fatal(err)
}

Publish a Message

A message can be sent to exchange by mentioning the name exchange. Publishing a message can do like this.

err := mq.Publish(&rabbitmq.MQConfigPublish{
  Exchange: "hello",
  Message: amqp.Publishing{
    ContentType: "text/plain",
    Body:        []byte(body),
  },
})
if err != nil {
 log.Fatal(err)
}

Consume Messages

Every message in the queue can be consumed by the queue consumer. To consume messages in queue can do like this.

The following code will run forever to listen for new message in queue.

msgs, err := mq.Consume(q, &rabbitmq.MQConfigConsume{})
if err != nil {
 log.Fatal(err)
}

forever := make(chan bool)
go func() {
  for msg := range msgs {
    fmt.Println(string(msg.Body))
    msg.Ack(false)
  }
}()
<-forever

How It Works

License

This project is under Mozilla Public License 2.0.

Contributing

Realy love any contribution. Feel free to create a Pull Request with following this commit convention.

About

Golang AMQP wrapper for RabbitMQ with better API

License:Mozilla Public License 2.0


Languages

Language:Go 100.0%