Exemplo n.º 1
0
func PublishMessage(msg string) {

	rmqenv := os.Getenv("RABBITMQ_PORT_5672_TCP_ADDR")
	var buffer bytes.Buffer
	buffer.WriteString("amqp://*****:*****@")
	buffer.WriteString(rmqenv)
	buffer.WriteString(":5672")

	dialaddr := helpers.StringConcat("amqp://*****:*****@", rmqenv, ":5672")

	log.Printf("Dialing %s", dialaddr)

	conn, err := amqp.Dial(dialaddr)
	failOnError(err, "failed to connect to rabbitmq")
	defer conn.Close()

	ch, err := conn.Channel()
	failOnError(err, "Failed to open a channel")
	defer ch.Close()

	q, err := ch.QueueDeclare(
		"hello", //name
		false,   //durable
		false,   //autoDelete
		false,   //exclusive
		false,   //noWait
		nil,     //args
	)
	failOnError(err, "Failed to declare a queue")

	err = ch.Publish(
		"",     //exchange
		q.Name, // key
		false,  // mandatory
		false,  // immediate
		amqp.Publishing{
			ContentType: "text/plain",
			Body:        []byte(msg),
		})
	log.Printf(" [x] Sent %s", msg)
	failOnError(err, "failed to publish a message")
}
Exemplo n.º 2
0
func PublishToDirectExchange(exchangename string, routingkey string, msg string) {

	rmqenv := os.Getenv("RABBITMQ_PORT_5672_TCP_ADDR")
	dialaddr := helpers.StringConcat("amqp://*****:*****@", rmqenv, ":5672")

	log.Printf("Dialing %s", dialaddr)
	conn, err := amqp.Dial(dialaddr)
	failOnError(err, "failed to connect to rabbitmq")
	defer conn.Close()

	ch, err := conn.Channel()
	failOnError(err, "Failed to open a channel")
	defer ch.Close()

	err = ch.ExchangeDeclare(
		exchangename, // name
		"direct",     // type
		true,         // durable
		false,        // auto-deleted
		false,        // internal
		false,        // no-wait
		nil,          // arguments
	)
	failOnError(err, "Failed to declare an exchange")

	err = ch.Publish(
		exchangename         // exchange
		routingkey, // routing key
		false, // mandatory
		false, // immediate
		amqp.Publishing{
			ContentType: "text/plain",
			Body:        []byte(msg),
		})
	failOnError(err, "Failed to publish a message")

	log.Printf(" [x] Sent %s", msg)

}