dongwu9063 2018-12-29 12:34
浏览 130
已采纳

无法使用Go从RabbitMQ发送消息

I just started reading about RabbitMQ and I'm trying to send large number of messages in a for loop. The problem is that it just doesn't work.

package main

import (
    "fmt"
    "github.com/streadway/amqp"
    "strconv"
)

func main() {
    var connectionString = "amqp://guest:guest@localhost:5672/"
    conn, _ := amqp.Dial(connectionString)
    defer conn.Close()

    ch, _ := conn.Channel()
    defer ch.Close()

    q, _ := ch.QueueDeclare(
        "user_actions", // name
        true,   // durable
        false,   // delete when unused
        false,   // exclusive
        false,   // no-wait
        nil,     // arguments
    )

    for i := 0; i < 10000; i++ {
        body := "Hello from Go! " + strconv.Itoa(i)
        ch.Publish(
            "",     // exchange
            "hello", // routing key
            false,  // mandatory
            false,  // immediate
            amqp.Publishing {
                ContentType: "text/plain",
                Body:        []byte(body),
            })
        fmt.Println("Sent: "+body)
    }

}

I even tried reducing the number of iterations and even tried sending messages outside of the loop but it just doesn't work. What am I doing wrong?

  • 写回答

1条回答 默认 最新

  • dsa4d4789789 2018-12-29 12:37
    关注

    The provided code seems fine except that you are using the default exchange and providing a route name different than your queue name.

    Chances are you'll want to use the queue name as the routing name. Try to replace hello with user_actions in ch.Publish function.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用Multisim设计汽车尾灯控制电路
  • ¥15 关于#java#的问题,请各位专家解答!(开发工具-eclipse)
  • ¥100 求用matlab求解上述微分方程的程序代码
  • ¥15 请问各位,如何在Jetson nano主控板的Ubuntu系统中安装PyQt5
  • ¥15 MAC安装佳能LBP2900驱动的网盘提取码
  • ¥400 微信停车小程序谁懂的来
  • ¥15 ATAC测序到底用什么peak文件做Diffbind差异分析
  • ¥15 安装ubantu过程中第一个vfat 文件挂载失败
  • ¥20 GZ::CTF如何兼容一些靶机?
  • ¥15 etcd集群部署问题