eqmrmq

package module
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Jun 1, 2024 License: GPL-3.0 Imports: 3 Imported by: 0

README

EQMRMQ

EQMRMQ is a Go package for simplified interaction with RabbitMQ, specifically designed for sending messages, receiving responses, and consuming messages from queues with ease.

The eqmrmq package utilizes the github.com/rabbitmq/amqp091-go package, which is an AMQP 0.9.1 Go client library. This library provides the underlying functionality for interacting with RabbitMQ, including features such as establishing connections, creating channels, publishing messages, consuming messages, and handling acknowledgments. By leveraging amqp091-go, eqmrmq simplifies the process of sending and receiving messages to and from RabbitMQ queues within Go applications.

Installation

To install EQMRMQ, use go get:

go get github.com/lacolle87/eqmrmq
Usage

Import EQMRMQ into your Go project:

import "github.com/yourusername/eqmrmq"
Sending a Message

To send a message to a queue:

// Create a new RabbitMQ connection
conn, err := amqp.Dial("amqp://guest:guest@localhost:5672/")
if err != nil {
    panic(err)
}
defer conn.Close()

// Create a channel
ch, err := conn.Channel()
if err != nil {
    panic(err)
}
defer ch.Close()

correlationId := GenerateCorrelationId()

// Send a message
err := eqmrmq.SendMessage(eqmrmq.Message{
    QueueName:     "my_queue",
    Message:       "Hello, RabbitMQ!",
    CorrelationId: eqmrmq.correlationId,
    ReplyQueue:    "",
    Ch:            ch,
})
if err != nil {
    panic(err)
}
Sending a Message with Response

To send a message to a queue and wait for a response:

// Send a message and wait for response
response, err := eqmrmq.SendToQueueWithResponse("my_queue", "Hello, RabbitMQ!", ch)
if err != nil {
    panic(err)
}
fmt.Println("Response:", string(response))
Consuming Messages

To consume messages from a queue:

// Define a handler function
handler := func(ch *amqp.Channel, d amqp.Delivery) error {
    fmt.Println("Received message:", string(d.Body))
    return nil
}

// Consume messages
err := eqmrmq.ConsumeMessages(ch, "my_queue", handler)
if err != nil {
    panic(err)
}
Replying to a Message

To reply to a message:

// Reply to a message
err := eqmrmq.ReplyToMessage(ch, delivery, []byte("Response from server"))
if err != nil {
    panic(err)
}
Acknowledgments

Special thanks to the authors of RabbitMQ and the AMQP 0.9.1 Go client library github.com/rabbitmq/amqp091-go for providing the underlying functionality used by this package.README

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ConsumeMessages

func ConsumeMessages(ch *amqp.Channel, queueName string, handler func(*amqp.Channel, amqp.Delivery) error) error

func CreateReplyQueue added in v0.2.0

func CreateReplyQueue(channel *amqp.Channel) (string, error)

func DeclareQueue added in v0.2.0

func DeclareQueue(ch *amqp.Channel, queueName string) (amqp.Queue, error)

func GenerateCorrelationId added in v0.2.0

func GenerateCorrelationId() string

func ReceiveResponse added in v0.2.0

func ReceiveResponse(correlationId, replyQueue string, ch *amqp.Channel) ([]byte, error)

func RegisterConsumer added in v0.2.0

func RegisterConsumer(ch *amqp.Channel, queueName string) (<-chan amqp.Delivery, error)

func ReplyToMessage

func ReplyToMessage(ch *amqp.Channel, d amqp.Delivery, replyData []byte) error

func SendToQueueWithResponse

func SendToQueueWithResponse(queueName, message string, ch *amqp.Channel) ([]byte, error)

Types

type Message

type Message struct {
	QueueName     string
	Message       string
	CorrelationId string
	ReplyQueue    string
	Ch            *amqp.Channel
}

func (*Message) Publish added in v0.2.0

func (msg *Message) Publish() error

Jump to

Keyboard shortcuts

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