Add broker package for message queue support. Include http and nats

This commit is contained in:
Asim
2015-04-26 19:33:35 +01:00
parent 52f140da5f
commit d99180ea37
3 changed files with 381 additions and 0 deletions

60
broker/broker.go Normal file
View File

@@ -0,0 +1,60 @@
package broker
import (
"code.google.com/p/go-uuid/uuid"
)
type Broker interface {
Address() string
Connect() error
Disconnect() error
Init() error
Publish(string, []byte) error
Subscribe(string, func(*Message)) (Subscriber, error)
}
type Message struct {
Id string
Timestamp int64
Topic string
Data []byte
}
type Subscriber interface {
Topic() string
Unsubscribe() error
}
var (
Address string
Id string
DefaultBroker Broker
)
func Init() error {
if len(Id) == 0 {
Id = "broker-" + uuid.NewUUID().String()
}
if DefaultBroker == nil {
DefaultBroker = NewHttpBroker(Address)
}
return DefaultBroker.Init()
}
func Connect() error {
return DefaultBroker.Connect()
}
func Disconnect() error {
return DefaultBroker.Disconnect()
}
func Publish(topic string, data []byte) error {
return DefaultBroker.Publish(topic, data)
}
func Subscribe(topic string, function func(*Message)) (Subscriber, error) {
return DefaultBroker.Subscribe(topic, function)
}