Golang Rabbit MQ Fanout Exchange Multiple Consumers

172 views
Skip to first unread message

Rakesh Goyal

unread,
Jul 4, 2016, 11:09:03 AM7/4/16
to golang-nuts

I am publishing messages in fanout exchange from Java application. I am able to receive message in multiple consumer in Java. I have 2 consumers in golang app but only one of the consumer (alternatively ) is receiving the message (Not both of them for a published message).

func HandleMessageFanout1(){

    conn := system.EltropyAppContext.RabbitMQConn

    channel, err := conn.Channel()

    if(err!=nil){
        log.Println(err)
    }
    //forever := make(chan bool)



    deliveries,err := channel.Consume(
        "example.queue", //queue
        "qw",
        true,
        false,
        false,
        false,
        nil)

    if(err!=nil){
        log.Println(err)
    }

    go func() {

        for d := range deliveries {
            log.Printf("Message recived in fanout 1")
            log.Printf("Received a message: %s", d.Body)
        }
    }()

    //<-forever

}

//2nd Consumer

package consumer

import (
    "github.com/eltropy/shehnai/backend/golang/common-packages/system"
    log "github.com/Sirupsen/logrus"
)

    func HandleMessageFanout2() {

        conn := system.EltropyAppContext.RabbitMQConn

        channel, err := conn.Channel()

        if (err!=nil) {
            log.Println(err)
        }

        //forever := make(chan bool)

        deliveries, err := channel.Consume(
            "example.queue", //queue
            "q2",
            true,
            false,
            false,
            false,
            nil)

        if (err!=nil) {
            log.Println(err)
        }

        go func() {
            for d := range deliveries {
                log.Printf("Message recived in fanout 2")
                log.Printf("Received a message: %s", d.Body)
            }
        }()

        //<-forever

    }

I am using https://github.com/streadway/amqp library for rabbit mq.

Giang Tran

unread,
Jul 4, 2016, 11:22:50 AM7/4/16
to golang-nuts
it's because you use single queue for both of consumer, in fanout exchange, message is copy to all the queue bind to this exchange.
Inorder to has 2 copy, you must have to use 2 queue(use different queue name).

Ross Salas

unread,
Jul 4, 2016, 2:22:25 PM7/4/16
to Giang Tran, golang-nuts

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages