cueball

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jun 1, 2017 License: BSD-2-Clause Imports: 3 Imported by: 0

README

cueball

cueball is a small abstraction layer for consuming and publishing messages to RabbitMQ. Internally it uses github.com/streadway/amqp.

Installation

Install the library using go get

$ go get github.com/stuartaroth/cueball
Usage

GoDoc

cueball requires clients to implement a simple interface:

Cueball interface

type Cueball interface {
	Config() Config
	Handle(message Message) (map[string]Message, error)
}

Config is a struct that contains the RabbitMQ connection information, which includes which queues the program will consume and publish to.

Handle is a function that will receive a message from the ConsumerQueue. It returns a map of string and Message, which which publish a Message to its corresponding string.

For an example of use, see below:

Example Usage

You can find this code at https://github.com/stuartaroth/cueball/blob/master/examples/simple-client/main.go

package main

import (
	"errors"
	"github.com/stuartaroth/cueball"
)

var (
	NameQueue       = "cueball-names"
	HelloQueue      = "cueball-hello"
	GoodbyeQueue    = "cueball-goodbye"
	DeadLetterQueue = "cueball-names-dead-letter"
)

type CueballClient struct{}

func (rc CueballClient) Config() cueball.Config {
	return cueball.Config{
		Uri:           "amqp://guest:guest@localhost:5672/",
		Exchange:      "simple-cueball-exchange",
		ExchangeType:  "direct",
		ConsumerQueue: NameQueue,
		PublisherQueues: []string{
			HelloQueue,
			GoodbyeQueue,
		},
		DeadLetterQueue: DeadLetterQueue,
		BindingKey:      "simple-cueball-key",
		ConsumerTag:     "simple-consumer",
		Debug:           true,
	}
}

func (rc CueballClient) Handle(message cueball.Message) (map[string]cueball.Message, error) {
	name := string(message.Body)

	if name == "Deadletter" {
		return map[string]cueball.Message{}, errors.New("Received name that cannot be handled")
	}

	helloMessage := cueball.Message{
		ContentType:     message.ContentType,
		ContentEncoding: message.ContentEncoding,
		DeliveryMode:    cueball.DeliveryModeNonPersistent,
		Priority:        message.Priority,
		Body:            []byte("Hello " + name),
	}

	goodbyeMessage := cueball.Message{
		ContentType:     message.ContentType,
		ContentEncoding: message.ContentEncoding,
		DeliveryMode:    cueball.DeliveryModeNonPersistent,
		Priority:        message.Priority,
		Body:            []byte("Goodbye " + name),
	}

	publishQueueMessages := map[string]cueball.Message{
		HelloQueue:   helloMessage,
		GoodbyeQueue: goodbyeMessage,
	}

	return publishQueueMessages, nil
}

func main() {
	rc := CueballClient{}
	cueball.Start(rc)
}
License

BSD-2 clause - see LICENSE for more details

Documentation

Overview

Package cueball is an abstraction library for using AMQP

Index

Constants

View Source
const (
	DeliveryModeNonPersistent = uint8(1)
	DeliveryModePersistent    = uint8(2)
)

Variables

This section is empty.

Functions

func Start

func Start(cueball Cueball)

Start requires a struct that implements the `Cueball` interface.

It will create the exchanges and queues as needed and then listen forever.

Types

type Config

type Config struct {
	Uri             string
	Exchange        string
	ExchangeType    string
	ConsumerQueue   string
	PublisherQueues []string
	DeadLetterQueue string
	BindingKey      string
	ConsumerTag     string
	Debug           bool
}

Config manages the RabbitMQ connection data.

The Exchange, ConsumerQueue, PublisherQueues, and DeadLetterQueue are all declared in RabbitMQ if they don't exist.

type Cueball

type Cueball interface {
	Config() Config
	Handle(message Message) (map[string]Message, error)
}

Cueball is the interface that must be implemented to `Start` using this RabbitMQ abstraction.

Config() returns a cueball.Config.

Handle(message Message) receives a cueball.Message and returns a map[string]cueball.Message and an error.

If the error is unequal to nil, the original message will be published to the specified DeadLetterQueue. The map[string]cueball.Message should reflect the string name of the queue the corresponding message should be published to.

type Message

type Message struct {
	ContentType     string
	ContentEncoding string
	DeliveryMode    uint8
	Priority        uint8
	Body            []byte
}

Message contains the data from the RabbitMQ message.

DeliveryMode can be non-persistent (1) or persistent (2).

Priority must be a value in range from (0) to (9).

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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