rabbitmqgo

package module
v0.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 13, 2023 License: GPL-3.0 Imports: 5 Imported by: 0

README

RabbitMQGo Package

A package to provide a simplified interface to publish and consume messages from RabbitMQ, using the amqp091-go library by RabbitMQ.

Installation

To use the RabbitMQGo package in your Go project, simply run the following command:

go get -u github.com/AndreGKruger/rabbitmq-go

To use this package, import it as shown below:

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

Initialization

The rabbitmqgo.NewRabbitMq() function is used to initialize a new instance of the RabbitMQ struct. The required configuration parameters are set through the RabbitMQConfig{} struct.

Example:

config := rabbitmqgo.RabbitMQConfig{
    Ctx:            context.Background(),
    Debug:          true,
    Durable:        true,
    Connection_url: "amqp://guest:guest@localhost:5672/",
    Queue_name:     "test",
    Producer_tps:   100,
    Consumer_tps:   100,
    Config:         amqp.Config{},
}

rabbitmqInstance := rabbitmqgo.NewRabbitMq(config)

Publishing Messages

Use the Publish([]byte) error method to send messages to the designated queue.

Example:

message := []byte(`{"data": "This is a message"}`)
err := rabbitmqInstance.Publish(message)

if err != nil {
    log.Fatal(err)
}

Consuming Messages

Use the Consume(string,int,func([]byte) error)error method to retrieve messages from a queue. The method accepts three parameters:

queue name (string) prefetch count (int) handler function for processing messages (func([]byte) error) Example:

const autoAck = false
const concurrentHandlers = 1
const prefetchCount = 5

handler := func(body []byte) error {
    log.Println("Message received: ", string(body))
    return nil
}

err = rabbitmqInstance.Consume("consumer", prefetchCount,
    rabbitmqgo.HandlerWrapper(concurrentHandlers,handler,autoAck),
)

if err != nil {
    log.Fatal(err)
}

Retrieving Queue Statistics

Use the GetStats() (amqp.Queue, error) method to fetch statistics (amqp.Queue struct) for the specified queue. Example:

stats, err := rabbitmqInstance.GetStats()
if err != nil {
    log.Fatal(err)
}

log.Println("Stats:\nName :", stats.Name,
            "\nMessages :", stats.Messages,
            "\nConsumers :", stats.Consumers))

Note that detailed documentation and working examples can be found in the godoc for this library.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type RabbitMQ

type RabbitMQ interface {
	Publish(message []byte) error
	Consume(name string, prefetch int, handler func([]byte) error) error
	GetStats() (amqp.Queue, error)
	// contains filtered or unexported methods
}

func NewRabbitMq

func NewRabbitMq(cnf RabbitMQConfig) RabbitMQ

type RabbitMQConfig

type RabbitMQConfig struct {
	Ctx            context.Context
	Debug          bool
	Durable        bool
	Connection_url string
	Queue_name     string
	Producer_tps   int
	Consumer_tps   int
	Config         amqp.Config
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL