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 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记