gorabbit

package module
v0.0.0-...-3564198 Latest Latest
Warning

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

Go to latest
Published: Nov 8, 2023 License: MIT Imports: 9 Imported by: 1

README

Go

Rabbit MQ Publish & Subscribe

Simple library for AMQP Rabbit MQ publish subscribe

Instalation

Install gorabbit depedency on your projects

go get github.com/pandeptwidyaop/gorabbit

Usage

The following samples will assist you to become as comfortable as possible with gorabbit library.

import "github.com/pandeptwidyaop/gorabbit"

Listen/Subscribe

func main(){
    // create infinite chan for listen queue
    forever := make(chan bool)

    mq, err := gorabbit.New(
        "amqp://user:password@host:port",
        "myQueueName",
        "myExchangeName",
    )

    if err != nil {
        panic(err)
    }

    // start connection
    err = mq.Connect()

    if err != nil {
        panic(err)
    }

    // binding all routing key

    err = mq.Bind([]string{"routing_a","routing_b","routing_c"})

    if err != nil {
        panic(err)
    }

    deliveries, err := mq.Consume()

    if err != nil {
        panic(err)
    }

    log.Println("Waiting for messages")

    for q, d := range deliveries {
        go mq.HandleConsumedDeliveries(q, d, handleConsume)
    }

    <-forever
}

// Handling messages
func handleConsume(mq gorabbit.RabbitMQ, queue string, deliveries <-chan amqp.Delivery){
    for d := range deliveries {
        switch d.RoutingKey {
        case "routing_a": 
            log.Println("message come from routing_a")
        case "routing_b":
            log.Println("message come from routing_b")
        case "routing_c":
            log.Println("message come from routing_c")
        }
    }
}

Publish Message

Create struct for message body


type Message struct {
    Name string `json:"name"`
    Address string `json:"address"`
}

Publish message

m := Message{
    Name : "pande",
    Address : "address"
}

jsonMessage, err := json.Marshal(m)

if err != nil {
    panic(err)
}

mq, err := gorabbit.New(
    "amqp://user:password@host:port",
    "myQueueName",
    "myExchangeName",
)

if err != nil {
    panic(err)
}

// start connection
err = mq.Connect()

if err != nil {
    panic(err)
}

err = mq.Publish("routing_1", "application/json", jsonMessage)

if err != nil {
    panic(err)
}

TLS Support

Added method ConnectTLS()
One should generates a CA and uses it to produce two certificate/key pairs.

git clone https://github.com/rabbitmq/tls-gen tls-gen
cd tls-gen/basic
# private key password
make PASSWORD=toor
make verify
make info
ls -l ./result
   // start TLS connection
    err = mq.ConnectTLS()

    if err != nil {
        panic(err)
    }

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	Context *RabbitMQ

	ErrConRefused   = errors.New("connection refused by server")
	ErrNotConnected = errors.New("not connected to server")
)

Functions

This section is empty.

Types

type RabbitMQ

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

func New

func New(connectionString string, name string, exchange string) (*RabbitMQ, error)

New instance

func (*RabbitMQ) Bind

func (mq *RabbitMQ) Bind(bindings []string) error

func (*RabbitMQ) Connect

func (mq *RabbitMQ) Connect() error

func (*RabbitMQ) ConnectTLS

func (mq *RabbitMQ) ConnectTLS() error

Method of TLS.

func (*RabbitMQ) Consume

func (mq *RabbitMQ) Consume() (map[string]<-chan amqp.Delivery, error)

func (*RabbitMQ) HandleConsumedDeliveries

func (mq *RabbitMQ) HandleConsumedDeliveries(q string, delivery <-chan amqp.Delivery, fn func(RabbitMQ, string, <-chan amqp.Delivery))

func (*RabbitMQ) Publish

func (mq *RabbitMQ) Publish(event string, contentType string, message []byte) error

func (*RabbitMQ) Reconnect

func (mq *RabbitMQ) Reconnect() error

Jump to

Keyboard shortcuts

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