rabbitmq

package module
v2.1.0 Latest Latest
Warning

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

Go to latest
Published: Dec 6, 2023 License: MIT Imports: 6 Imported by: 0

README

rabbitmq-go

Golang RabbitMQ high level library

package main

import (
	"github.com/labstack/gommon/log"
	"github.com/trueifnotfalse/rabbitmq-go/v2"
	"time"
)

func failOnError(err error, msg string) {
	if err != nil {
		log.Panicf("%s: %s", msg, err)
	}
}

func main() {
	logger := log.New("-")
	config := rabbitmq.Config{
		Host:             "127.0.0.1",
		Port:             "5672",
		User:             "guest",
		Password:         "guest",
		ReConnect:        true,
		ReconnectTimeOut: 10*time.Second,
	}
	con := rabbitmq.NewConnector(logger, &config)
	qc := rabbitmq.NewDurableQueueConfig("hello")
	err := con.QueueDeclare(qc)
	failOnError(err, "Failed to declare a queue")

	message := struct {
		CreatedAt string `json:"created_at"`
		Text      string `json:"text"`
	}{
		CreatedAt: time.Now().Format("2006-01-02 15:04:05"),
		Text:      "Hello World!",
	}

	err = con.Publish().ToQueue("hello").WithStruct(message).Do()
	failOnError(err, "Failed to publish a message")
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	Host             string
	Port             string
	User             string
	Password         string
	ReConnect        bool
	ReconnectTimeOut time.Duration
}

type Connector

type Connector struct {
	sync.Mutex
	// contains filtered or unexported fields
}

func NewConnector

func NewConnector(logger *log.Logger, config *Config) *Connector

func (*Connector) Close

func (c *Connector) Close() error

func (*Connector) Connect

func (c *Connector) Connect() *Connector

func (*Connector) Consume

func (c *Connector) Consume(consumeConfig *ConsumeConfig, handler MessageHandler) error

func (*Connector) ExchangeDeclare

func (c *Connector) ExchangeDeclare(ec *ExchangeConfig) error

func (*Connector) GetChannel

func (c *Connector) GetChannel() (*amqp.Channel, error)

func (*Connector) Publish

func (c *Connector) Publish() *Publisher

func (*Connector) QueueBind

func (c *Connector) QueueBind(qbc *QueueBindConfig) error

func (*Connector) QueueDeclare

func (c *Connector) QueueDeclare(qc *QueueConfig) error

func (*Connector) QueueDelete added in v2.1.0

func (c *Connector) QueueDelete(qc *QueueConfig) error

type ConsumeConfig

type ConsumeConfig struct {
	Queue         string
	Consumer      string
	AutoAck       bool
	Exclusive     bool
	NoLocal       bool
	NoWait        bool
	Args          amqp.Table
	PrefetchCount int
}

func NewAutoAckConsumeConfig

func NewAutoAckConsumeConfig(name string, consumer string) *ConsumeConfig

func NewManualAckConsumeConfig

func NewManualAckConsumeConfig(name string, consumer string) *ConsumeConfig

type ExchangeConfig

type ExchangeConfig struct {
	Name       string
	Type       string
	Durable    bool
	AutoDelete bool
	Internal   bool
	NoWait     bool
	Args       amqp.Table
}

func NewDurableExchangeConfig

func NewDurableExchangeConfig(name, typeName string) *ExchangeConfig

func NewTransientExchangeConfig

func NewTransientExchangeConfig(name, typeName string) *ExchangeConfig

type MessageHandler

type MessageHandler func(<-chan amqp.Delivery)

type Publisher

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

func (*Publisher) AsPersistent

func (p *Publisher) AsPersistent() *Publisher

func (*Publisher) AsTransient

func (p *Publisher) AsTransient() *Publisher

func (*Publisher) Do

func (p *Publisher) Do() error

func (*Publisher) ToExchange

func (p *Publisher) ToExchange(name string) *Publisher

func (*Publisher) ToQueue

func (p *Publisher) ToQueue(name string) *Publisher

func (*Publisher) WithContentType

func (p *Publisher) WithContentType(contentType string) *Publisher

func (*Publisher) WithData

func (p *Publisher) WithData(data []byte) *Publisher

func (*Publisher) WithExpiration

func (p *Publisher) WithExpiration(expiration string) *Publisher

func (*Publisher) WithStruct

func (p *Publisher) WithStruct(obj interface{}) *Publisher

type QueueBindConfig

type QueueBindConfig struct {
	QueueName    string
	ExchangeName string
	RoutingKey   string
	NoWait       bool
	Args         amqp.Table
}

func NewQueueBindConfig added in v2.1.0

func NewQueueBindConfig(queueName, exchangeName string) *QueueBindConfig

type QueueConfig

type QueueConfig struct {
	Name       string
	Durable    bool
	AutoDelete bool
	Exclusive  bool
	NoWait     bool
	Args       amqp.Table
}

func NewDurableQueueConfig

func NewDurableQueueConfig(name string) *QueueConfig

func NewTransientQueueConfig

func NewTransientQueueConfig(name string) *QueueConfig

Jump to

Keyboard shortcuts

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