messagebroker

package
v0.0.5 Latest Latest
Warning

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

Go to latest
Published: Jul 10, 2024 License: GPL-3.0 Imports: 3 Imported by: 5

README

MessageBroker

This service manages interactions with memory message broker services. For the time being the only service supported is RabbitMQ.

Usage

MessageBroker requires a Client interface for being used, this library offers RabbitMQ as Client interface.

Example
type Client interface {
	SendMessage(string, []byte) error
	ReceiveMessages(context.Context, string, chan<- []byte, chan<- error)
}

Rebbimq type from go-types can be used as Client.

package main

import (
	"context"
	rabbitmqconfig "github.com/a-castellano/go-types/rabbitmq"
	"os"
)

func main() {

	rabbitmqConfig, _ := rabbitmqconfig.NewConfig()
	queueName := "test"
	testString := []byte("This is a Test")

	rabbitmqClient := NewRabbimqClient(rabbitmqConfig)

	messageBroker := MessageBroker{client: rabbitmqClient}

	send_error := messageBroker.SendMessage(queueName, testString)
}

Available Functions

The following functions are avaible for interacting with MemoryDatabase

SendMessage
func (client RabbitmqClient) SendMessage(queueName string, message []byte) error

Sends message through required queue.

ReceiveMessages
func (client RabbitmqClient) ReceiveMessages(ctx context.Context, queueName string, messages chan<- []byte, errors chan<- error)

Receives messages from required queue. I can be sottped using context:

rabbitmqConfig, _ := rabbitmqconfig.NewConfig()
queueName := "test"
rabbitmqClient := NewRabbimqClient(rabbitmqConfig)
messageBroker := MessageBroker{client: rabbitmqClient}

messagesReceived := make(chan []byte)

ctx, cancel := context.WithCancel(context.Background())

receiveErrors := make(chan error)

go messageBroker.ReceiveMessages(ctx, queueName, messagesReceived, receiveErrors)

time.Sleep(2 * time.Second)
cancel()

messageReceived := <-messagesReceived

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client interface {
	SendMessage(string, []byte) error
	ReceiveMessages(context.Context, string, chan<- []byte, chan<- error)
}

Client interface operates against memory database instance

type MessageBroker

type MessageBroker struct {
	Client Client
}

MessageBroker uses Client in order to operate against messagebroker instance

func (MessageBroker) ReceiveMessages

func (messageBroker MessageBroker) ReceiveMessages(ctx context.Context, queueName string, messages chan<- []byte, errors chan<- error)

ReceiveMessages receives messages through queueName using Client

func (MessageBroker) SendMessage

func (messageBroker MessageBroker) SendMessage(queueName string, message []byte) error

SendMessage sends a message through queueName using Client

type RabbitmqClient

type RabbitmqClient struct {
	// contains filtered or unexported fields
}

RabbitmqClient is the real Redis client, it has Client methods

func NewRabbimqClient

func NewRabbimqClient(rabbitmqConfig *rabbitmqconfig.Config) RabbitmqClient

func (RabbitmqClient) ReceiveMessages

func (client RabbitmqClient) ReceiveMessages(ctx context.Context, queueName string, messages chan<- []byte, errors chan<- error)

ReceiveMessages stores messages in channel until it is closed using context

func (RabbitmqClient) SendMessage

func (client RabbitmqClient) SendMessage(queueName string, message []byte) error

SendMessage sends a message through queueName

Jump to

Keyboard shortcuts

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