box-o-sand/sylvilagus/go/sylvilagus-ch04-rpc-client/main.go
2012-12-02 08:41:05 -05:00

64 lines
1.4 KiB
Go

package main
import (
"encoding/json"
"log"
"time"
)
import (
"github.com/meatballhat/box-o-sand/sylvilagus/go/sylvilagus"
"github.com/streadway/amqp"
)
func main() {
connection, err := amqp.Dial(sylvilagus.AMQP_URI)
if err != nil {
log.Fatal("Failed to connect!:", err)
}
defer connection.Close()
channel, err := sylvilagus.CreateRPCTopology(connection)
if err != nil {
log.Fatal("Failed to build topology!:", err)
}
qResult, err := channel.QueueDeclare("", false, false, true, false, nil)
if err != nil {
log.Fatal("Failed to build topology!:", err)
}
msgBody := &sylvilagus.Ping{
ClientName: "RPC Client 1.0",
Time: time.Now(),
}
msgBytes, err := json.Marshal(msgBody)
if err != nil {
log.Fatal("Failed to json.Marshal the ping!:", err)
}
msg := amqp.Publishing{
ContentType: "application/json",
DeliveryMode: amqp.Persistent,
Timestamp: time.Now(),
Body: msgBytes,
ReplyTo: qResult.Name,
}
channel.Publish("rpc", "ping", false, false, msg)
log.Println("Sent 'ping' RPC call:", string(msg.Body))
log.Println("Waiting for reply...")
pongs, err := channel.Consume(qResult.Name, qResult.Name, false, false, false, false, nil)
for pong := range pongs {
log.Println("RPC Reply ---", string(pong.Body))
if err = channel.Close(); err != nil {
log.Fatal("Failed to close channel!:", err)
} else {
return
}
}
}