consumer

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Sep 26, 2013 License: MIT Imports: 7 Imported by: 0

README

Go Consumer

GoConsumer is a wrapper around amqp that allows you to easily write applications that consume AMQP queues. Exchange, Queue and bindings are defined in a simple configuration file. GoConsumer handles plumbing those together allowing you to focus on writing great consumer code.

This project borrows many ideas from sparkplug

Configuration

Configuration is done through an ini style file. In this file you define the connection, exchange and queue(s) you want to bind into. Once your file has been loaded and parsed you can attach your consumer function in. The following is an example config file:

[connection]
host = localhost
virtual_host = '/'
user = guest
password = guest
ssl = False

[exchange]
name = grand
type = direct
durable = True
auto_delete = False

[queue]
name = hose
durable = True
auto_delete = True
exclusive = False
routing_key = fire

Consuming functions need to have the following signature:

func(*consumer.Message)

A simple example application would look like:

import (
	"github.com/markstory/go-consumer"
	"log"
)

c, err = consumer.LoadConfig("./consumer.ini")
if err != nil {
	log.Fatalf("Unable to create consumer. Error: %v", err)
}
err = c.Consume(func(msg *consumer.Message) {
	log.Print("Got a message")
	msg.Ack(true)
})

Your consumer function will receive message types that can be acked or nacked as you see fit.

Signals

GoConsumer handles SIGINT, SIGTERM and SIGQUIT. In all cases the it attempts to shutdown the AMQP connection and finish consuming any buffered messages.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Consumer

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

A consumer that applications use to register functions to act as consumers.

Consumers will connect to the AMQP server when the Consume method is called. You can manualy connect using the Connect method as well.

func Create

func Create(configFile string) (c *Consumer, err error)

Create a new consumer using the connection, exchange binding and queue configurations in the provide configuration file. Once created you can bind consumers to start handling messages

func (*Consumer) Connect

func (c *Consumer) Connect() (err error)

Connect to the AMQP server.

Will do the following work:

- Create the connection. - Declare the exchange. - Declare the queue. - Bind the queue + exchange together.

func (*Consumer) Consume

func (c *Consumer) Consume(handler worker) (err error)

Takes a function that accepts amqp.Delivery and binds it to the configured queue.

The provided function will be called each time a message is received and the function is expected to Ack or Nack the message.

func (*Consumer) Queue

func (c *Consumer) Queue() queue

func (*Consumer) StartLoop

func (c *Consumer) StartLoop()

Start the loop that keeps the process alive.

Registers signal handlers to cancel consumers, on signals.

func (*Consumer) Stop

func (c *Consumer) Stop() error

Disconnect from the AMQP server and stop consuming messages.

type Message

type Message struct {
	amqp.Delivery
}

Simple message type so users of this library don't have to import amqp as well

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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