Golang AMQP wrapper for RabbitMQ with better API

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.DeclareQueue(rabbitmq.NewQueueOptions().SetName("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.
  • Reuse connection to create MQ stuff.

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("amqp://guest:guest@localhost:5672/")
if err != nil {
 log.Fatal(err)
}

// don't forget to close the connection and channel
defer mq.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 := mq.DeclareQueue(rabbitmq.NewQueueOptions().SetName("hello"))
if err != nil {
 log.Fatal(err)
}

Declare Exchange

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

err := mq.DeclareExchange(rabbitmq.NewExchangeOptions().SetName("hello").SetType(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 := mq.QueueBind(rabbitmq.NewQueueBindOptions().SetName("hello").SetExchange("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.NewPublishOptions().
        SetExchange("hello").
	SetMessage(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(nil)
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

Every AMQP related function call will invoke the AMQP connector function. For example when creating a new MQ, it will call the AMQP connection function. Declare queue, declare exchange, publish, consume, etc will do the same.

Please use any references made previously to prevent too many function calls.

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.

Similar Resources

A high-level RabbitMQ driver for Golang.

grabbitmq A high-level RabbitMQ driver for Golang. Import in your project: go get github.com/shaswata56/grabbitmq Usage Demo: package main import (

Aug 2, 2022

:incoming_envelope: A fast Message/Event Hub using publish/subscribe pattern with support for topics like* rabbitMQ exchanges for Go applications

Hub 📨 A fast enough Event Hub for go applications using publish/subscribe with support patterns on topics like rabbitMQ exchanges. Table of Contents

Dec 17, 2022

Abstraction layer for simple rabbitMQ connection, messaging and administration

Abstraction layer for simple rabbitMQ connection, messaging and administration

Jazz Abstraction layer for quick and simple rabbitMQ connection, messaging and administration. Inspired by Jazz Jackrabbit and his eternal hatred towa

Dec 12, 2022

RabbitMQ wire tap and swiss army knife

RabbitMQ wire tap and swiss army knife

rabtap - RabbitMQ wire tap Swiss army knife for RabbitMQ. Tap/Pub/Sub messages, create/delete/bind queues and exchanges, inspect broker. Contents Feat

Dec 28, 2022

RabbitMQ Reconnection client

rmqconn RabbitMQ Reconnection for Golang Wrapper over amqp.Connection and amqp.Dial. Allowing to do a reconnection when the connection is broken befor

Sep 27, 2022

An easy-to-use CLI client for RabbitMQ.

buneary, pronounced bun-ear-y, is an easy-to-use RabbitMQ command line client for managing exchanges, managing queues and publishing messages to exchanges.

Sep 3, 2022

High level manegment for rabbitmq.

High level manegment for rabbitmq. Features Simple configuration bootstrap. Gracefully shutting down. Consume messages in parallel specifying a number

Sep 24, 2022

Testing message queues with RabbitMQ

Rabbit-MessageQueue Just a repository of RabbitMQ simple usage for queueing messages. You can use this as a sender or a receiver. More information is

Mar 10, 2022

A RabbitMQ connection pool write in pure go

A RabbitMQ connection pool write in pure go

Oct 8, 2021
golang amqp rabbitmq produce consume

Step 1: Container Run Container docker run -itp 9001:9001 --name go_temp -v /usr/local/project/temp/go_amqp/:/home/ -d golang:1.16.6 Enter Container

Nov 26, 2021
Declare AMQP entities like queues, producers, and consumers in a declarative way. Can be used to work with RabbitMQ.

About This package provides an ability to encapsulate creation and configuration of RabbitMQ([AMQP])(https://www.amqp.org) entities like queues, excha

Dec 28, 2022
Tool for collect statistics from AMQP (RabbitMQ) broker. Good for cloud native service calculation.

amqp-statisticator Tool for collect statistics around your AMQP broker. For example RabbitMQ expose a lot information trought the management API, but

Dec 13, 2021
A tiny wrapper over amqp exchanges and queues 🚌 ✨

Rabbus ?? ✨ A tiny wrapper over amqp exchanges and queues. In memory retries with exponential backoff for sending messages. Protect producer calls wit

Dec 18, 2022
A wrapper of streadway/amqp that provides reconnection logic and sane defaults

go-rabbitmq Wrapper of streadway/amqp that provides reconnection logic and sane defaults. Hit the project with a star if you find it useful ⭐ Supporte

Dec 28, 2022
Go library to build event driven applications easily using AMQP as a backend.

event Go library to build event driven applications easily using AMQP as a backend. Publisher package main import ( "github.com/creekorful/event" "

Dec 5, 2021
Gorabbit - Simple library for AMQP Rabbit MQ publish subscribe

gorabbit Rabbit MQ Publish & Subscribe Simple library for AMQP Rabbit MQ publish

Oct 4, 2022
App for test hypothesis about API for rabbitmq

REST API для работы с RabbitMQ Приложение для работы с брокером сообщений RabbitMQ через REST API. Основная мысль - что одиночные сообщения отправлять

Nov 12, 2021
A user friendly RabbitMQ library written in Golang.

TurboCookedRabbit A user friendly RabbitMQ library written in Golang to help use streadway/amqp. Based on my work found at CookedRabbit. Work Recently

Jan 6, 2023
🚀 Golang, Go Fiber, RabbitMQ, MongoDB, Docker, Kubernetes, GitHub Actions and Digital Ocean
🚀 Golang, Go Fiber, RabbitMQ, MongoDB, Docker, Kubernetes, GitHub Actions and Digital Ocean

Bookings Solução de cadastro de usuários e reservas. Tecnologias Utilizadas Golang MongoDB RabbitMQ Github Actions Docker Hub Docker Kubernetes Digita

Feb 18, 2022