Thumperq is a light weight and easy to use Go library for RabbitMQ.
Rquirements
- Thumperq requires Go 1.8+
How to install
go get github.com/thumperq/thumperq
or
go get github.com/thumperq/[email protected]
Quick Start
- Define your message(aka event)
type MyEvent struct {
ID string `json:"id,omitempty"`
Description string `json:"description,omitempty"`
}
func NewMyEvent(id string, description string) *MyEvent{
return &MyEvent{
ID: id,
Description: description,
}
}
- Create a config instance
import (
thumperqcfg "github.com/thumperq/thumperq/pkg/config"
)
busCfg := thumperqcfg.BusConfig{
RmqConnection: "<rmq_connection_string>", // RabbitMQ connection string
PropagateContextMetadata: true, // If set to true it'll propagate go contexts metadata in the Bus message
RetryCount: 3, // How many retries in case of failure
RetryIntervalMs: 2000 // 2 seconds delay in retry executing handler in case of failure
}
- Create a bus instance
import (
thumperq "github.com/thumperq/thumperq/pkg"
)
bus := thumperq.NewBus(busCfg)
- Publish a message(aka event)
myEvent := NewMyEvent("1234", "Some description!")
err := bus.Publish(ctx, myEvent)
- Subscribe to a message(aka event)
import (
thumperq "github.com/thumperq/thumperq/pkg"
"github.com/thumperq/thumperq/pkg/handler"
)
type MyHandler struct {
bus thumperq.IBus
}
func NewMyHandler(bus thumperq.IBus) *MyHandler {
myHandler := &MyHandler{
bus: bus,
}
thumperq.CreateConsumer[*MyEvent](bus, myHandler)
return myHandler
}
func (h *MyHandler) Handle(msg <-chan handler.HandlerMessage[*MyEvent]) error {
busMsg := <-msg
// busMsg.Message -> is type of your event(in this example it's of type *MyEvent)
// busMsg.Headers -> contains contexts' metadata published by the bus if the PropagateContextMetadata in config is set to true
...
}