RabbitMQ--go完整例項
阿新 • • 發佈:2019-01-06
RabbitMQ–go
接收和傳送例項(下面是完整程式碼):
package main
import (
"fmt"
"log"
"bytes"
"time"
"github.com/streadway/amqp"
)
var conn *amqp.Connection
var channel *amqp.Channel
var count = 0
const (
queueName = "push.msg.q"
exchange = "t.msg.ex"
mqurl ="amqp://shi:[email protected] :5672/test"
)
func main() {
go func() {
for {
push()
time.Sleep(1 * time.Second)
}
}()
receive()
fmt.Println("end")
close()
}
func failOnErr(err error, msg string) {
if err != nil {
log.Fatalf("%s:%s", msg, err)
panic(fmt.Sprintf("%s:%s" , msg, err))
}
}
func mqConnect() {
var err error
conn, err = amqp.Dial(mqurl)
failOnErr(err, "failed to connect tp rabbitmq")
channel, err = conn.Channel()
failOnErr(err, "failed to open a channel")
}
func close() {
channel.Close()
conn.Close()
}
//連線rabbitmq server
func push() {
if channel == nil {
mqConnect()
}
msgContent := "hello world!"
channel.Publish(exchange, queueName, false, false, amqp.Publishing{
ContentType: "text/plain",
Body: []byte(msgContent),
})
}
func receive() {
if channel == nil {
mqConnect()
}
msgs, err := channel.Consume(queueName, "", true, false, false, false, nil)
failOnErr(err, "")
forever := make(chan bool)
go func() {
//fmt.Println(*msgs)
for d := range msgs {
s := BytesToString(&(d.Body))
count++
fmt.Printf("receve msg is :%s -- %d\n", *s, count)
}
}()
fmt.Printf(" [*] Waiting for messages. To exit press CTRL+C\n")
<-forever
}
func BytesToString(b *[]byte) *string {
s := bytes.NewBuffer(*b)
r := s.String()
return &r
}