Skip to content

Instantly share code, notes, and snippets.

@wellle
Last active December 15, 2015 20:29
Show Gist options
  • Save wellle/5319271 to your computer and use it in GitHub Desktop.
Save wellle/5319271 to your computer and use it in GitHub Desktop.
Small go program to demonstrate a case for https://github.com/streadway/amqp/pull/56. Using the adeven fork, the program prints `Sucess! Body: payload`. Using streadway/amqp, the program prints `Get failed: Exception (501) Reason: "EOF"` Requirements: Local amqp server with a queue and a corresponding dead letter queue. Both should be empty.
package main
import (
"github.com/adeven/amqp" // `adeven` works, `streadway` doesn't
"log"
"time"
)
func main() {
amqpConfig := "amqp://guest:guest@localhost:5672/"
exchange := "yourexchange"
tag := "yourtag"
routingKey := "yourkey"
queue := "yourqueue"
failedQueue := "yourdeadletterqueue"
// connect
connection, err := amqp.Dial(amqpConfig)
if err != nil {
log.Fatalln("Dial failed")
}
channel, err := connection.Channel()
if err != nil {
log.Fatalln("Channel failed")
}
// write to queue
body := []byte("payload")
publishing := amqp.Publishing{ContentType: "text/json", Body: body}
err = channel.Publish(exchange, routingKey, true, false, publishing)
if err != nil {
log.Fatalln("Publish failed")
}
// reject to failedQueue (see func below)
go reject(channel, queue, tag)
time.Sleep(10 * time.Millisecond)
// get from failedQueue
delivery, ok, err := channel.Get(failedQueue, false)
if err != nil {
log.Fatalln("Get failed:", err.Error())
} else if !ok {
log.Fatalln("Get not ok")
}
log.Println("Sucess! Body:", string(delivery.Body))
}
func reject(channel *amqp.Channel, queue, tag string) {
deliveries, err := channel.Consume(queue, tag, false, false, false, false, nil)
if err != nil {
log.Fatalln("Consume failed")
}
for delivery := range deliveries {
delivery.Reject(false)
}
}
@streadway
Copy link

Thanks for the reproduction case. This has been rolled into an integration test, and the parsing fix has made it into master on https://github.com/streadway/amqp.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment