1

I used the NSQ as the mq in my project,java producer produce the message to NSQ and go consumer consume it.But the strange things is that the consumer always get the message after few seconds.There is just a few messages,I really don't know how to explain why it's happened. Here is the test result,please pay attention to the time.They both consume the same topic.You can see in the second time,Go is slower than java for 7s. Java result:

INFO | jvm 1 | 2018/07/11 17:22:01 | Msg receive:{"did":"XSQ000200000005","msg":{"id":"5560","type":1,"content":"ZBINh6CBsLw7k2xjr1wslSjY+5QavEgYU6AzzLZn0lOgON9ZYHnNP4UJVUGB+/SpsxZQnrWR9PlULzpSP/p9l9t8wiAwj8qhznRaT8jeyx1/EUrDE0oXJB8GxWaLJUICCbC92j4BMA2HU8vgcfDOp9nSy1KFafi9zgFiCf9Igqo="}}

INFO | jvm 1 | 2018/07/11 17:22:11 | Msg receive:{"did":"XSQ000200000005","msg":{"id":"5560","type":1,"content":"ZBINh6CBsLw7k2xjr1wslSjY+5QavEgYU6AzzLZn0lOgON9ZYHnNP4UJVUGB+/SpsxZQnrWR9PlULzpSP/p9l9t8wiAwj8qhznRaT8jeyx1/EUrDE0oXJB8GxWaLJUICCbC92j4BMA2HU8vgcfDOp9nSy1KFafi9zgFiCf9Igqo="}}

INFO | jvm 1 | 2018/07/11 17:23:21 | Msg receive:{"did":"XSQ000200000005","msg":{"id":"5560","type":1,"content":"ZBINh6CBsLw7k2xjr1wslSjY+5QavEgYU6AzzLZn0lOgON9ZYHnNP4UJVUGB+/SpsxZQnrWR9PlULzpSP/p9l9t8wiAwj8qhznRaT8jeyx1/EUrDE0oXJB8GxWaLJUICCbC92j4BMA2HU8vgcfDOp9nSy1KFafi9zgFiCf9Igqo="}}

INFO | jvm 1 | 2018/07/11 17:25:31 | Msg receive:{"did":"XSQ000200000005","msg":{"id":"5560","type":1,"content":"ZBINh6CBsLw7k2xjr1wslSjY+5QavEgYU6AzzLZn0lOgON9ZYHnNP4UJVUGB+/SpsxZQnrWR9PlULzpSP/p9l9t8wiAwj8qhznRaT8jeyx1/EUrDE0oXJB8GxWaLJUICCbC92j4BMA2HU8vgcfDOp9nSy1KFafi9zgFiCf9Igqo="}}

Go result:

2018-07-11 17:22:03 broker.go DEBUG Ready to send msg 5560 with type 1 to XSQ000200000005

2018-07-11 17:22:28 broker.go DEBUG Ready to send msg 5560 with type 1 to XSQ000200000005

2018-07-11 17:23:21 broker.go DEBUG Ready to send msg 5560 with type 1 to XSQ000200000005

2018-07-11 17:25:38 broker.go DEBUG Ready to send msg 5560 with type 1 to XSQ000200000005

please ignore the other errors,just because of the business.

Here is my go consumer:

func (b *Broker) createConsumer(topic string, vendor int32) error {
    config := nsq.NewConfig()
    laddr := "127.0.0.1"
    // so that the test can simulate binding consumer to specified address
    config.LocalAddr, _ = net.ResolveTCPAddr("tcp", laddr+":0")
    // so that the test can simulate reaching max requeues and a call to LogFailedMessage
    config.DefaultRequeueDelay = 0
    // so that the test wont timeout from backing off
    config.MaxBackoffDuration = time.Millisecond * 50




    c, err := nsq.NewConsumer(topic, "channel_box_" + util.String(vendor), config)
    if err != nil {
        return log.Error("Failed to new nsq consumers.")
    }

    c.AddConcurrentHandlers(nsq.HandlerFunc(func(message *nsq.Message) error {
        if err := b.handle(message, vendor); err != nil {
            log.Errorf("Handle message %v for vendor %d from mq failed.", message.ID, vendor)
        }
        return nil
    }), 5)
    if err = c.ConnectToNSQLookupds(b.Opts.Nsq.Lookup); err != nil {
        return log.Error("Failed to connect to nsq lookup server.")
    }
    b.consumers = append(b.consumers, c)

    return nil
}
Stefan Steinegger
  • 63,782
  • 15
  • 129
  • 193

0 Answers0