A fast background processing framework for Golang and RabbitMQ
Use go get
go get github.com/grantchen/go-sneaker
Then import the go-sneaker package into your own code.
import "github.com/grantchen/go-sneaker"
send background task code sample
Publisher, err := sneaker.NewPublisher(amqp_url, exchange_key))
if err != nil {
panic(err)
}
var json = jsoniter.ConfigCompatibleWithStandardLibrary
dataJsonByte, _ := json.Marshal(map[string]string{"key": "value"})
err = Publisher.Publish("queue_name", "text/json", dataJsonByte)
if err != nil {
panic(err)
}
// if not use can close it
Publisher.Close()
Publish(queueName, bodyContentType string, body []byte)
queueName - Queue Name
bodyContentType - send body content type. Default is text/json
body - send data body byte
background task handle worker sample
Consumer, err := sneaker.NewConsumer(amqp_url, exchange_key))
if err != nil {
panic(err)
}
err = Consumer.Consume("queue_name", map[string]interface{}{}, handleWorker)
if err != nil {
panic(err)
}
func handleWorker(body []byte){
//...
}
Consume(queueName string, args map[string]interface{}, f fn)
queueName - Queue Name
args - consume releated params. Deafult values:
{"durable": true, "autoDelete": false, "autoAck": false,
"exclusive": false, "noWait": false,
"noLocal": false, "consumer": "", "threads": 5}
durable - is queue durable
autoDelete - is queue delete when unused
autoAck - is consume auto ack
exclusive - is queue and consume exclusive
noWait - is queue and consume no wait
noLocal - is consume no local
consumer - consumer name
threads - how many threads to handle worker method f
f - handle body method