Created
February 23, 2019 15:08
-
-
Save masnun/3b168956fcfcf52e19134c67d38458d8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"encoding/json" | |
"github.com/masnun/gopher-and-rabbit" | |
"github.com/streadway/amqp" | |
"log" | |
"math/rand" | |
"time" | |
) | |
func handleError(err error, msg string) { | |
if err != nil { | |
log.Fatalf("%s: %s", msg, err) | |
} | |
} | |
func main() { | |
conn, err := amqp.Dial(gopher_and_rabbit.Config.AMQPConnectionURL) | |
handleError(err, "Can't connect to AMQP") | |
defer conn.Close() | |
amqpChannel, err := conn.Channel() | |
handleError(err, "Can't create a amqpChannel") | |
defer amqpChannel.Close() | |
queue, err := amqpChannel.QueueDeclare("add", true, false, false, false, nil) | |
handleError(err, "Could not declare `add` queue") | |
rand.Seed(time.Now().UnixNano()) | |
addTask := gopher_and_rabbit.AddTask{Number1: rand.Intn(999), Number2: rand.Intn(999)} | |
body, err := json.Marshal(addTask) | |
if err != nil { | |
handleError(err, "Error encoding JSON") | |
} | |
err = amqpChannel.Publish("", queue.Name, false, false, amqp.Publishing{ | |
DeliveryMode: amqp.Persistent, | |
ContentType: "text/plain", | |
Body: body, | |
}) | |
if err != nil { | |
log.Fatalf("Error publishing message: %s", err) | |
} | |
log.Printf("AddTask: %d+%d", addTask.Number1, addTask.Number2) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment