dsqpx86002
2018-06-17 13:12
浏览 74
已采纳

如何在Go中按名称将任务发布到RabbitMQ队列?

I need to publish a task by name (creating a message on a queue with task name passed in headers, passing some args).

I've done this in Celery (Python) like this:

celery_app.send_task("task_name", args=("arg1", "arg2",))

Here's my code in Go to do the same thing:

headers := make(amqp.Table)
headers["argsrepr"] = []string{"arg1", "arg2"}
headers["task"] = "task_name"

body := ""
jsonBody, _ := json.Marshal(body)

err = ch.Publish(
  "",     // exchange
  "queue_name", // routing key
  false,  // mandatory
  false,  // immediate
  amqp.Publishing {
    DeliveryMode: amqp.Persistent,
    ContentType:  "application/json",
    Body:         jsonBody,
    Headers:      headers,
  })

But I get this error:

Failed to publish a message: table field "argsrepr" value [%!t(string=arg1) %!t(string=arg2)] not supported

I'm using "github.com/streadway/amqp" library to talk with my rabbitmq node. It seems that "send task by name" is not implemented in this library.

图片转代码服务由CSDN问答提供 功能建议

我需要按名称发布任务(在队列中创建消息,并在标头中传递任务名称,并传递一些

我已经在Celery(Python)中完成了以下操作:

  celery_app.send_task(“ task_name”,args  =(“ arg1”,“ arg2”,))
   
 
 

这是我在Go中执行相同操作的代码:

 标头:= make(amqp.Table)
headers [“ argsrepr”] = [] string {“ arg1”,“ arg2”} 
headers [“ task”] =“ task_name” 
 
body:=  “” 
jsonBody,_:= json.Marshal(body)
 
err = ch.Publish(
“”,// exchange 
“ queue_name”,//路由键
 false,//强制
  false,//立即
 amqp。发布{
 DeliveryMode:amqp.Persistent,
 ContentType:“ application / json”,
 Body:jsonBody,
标头:标头,
})
   
 
 

但出现此错误:

 无法发布消息:表字段“ argsrepr”值[%!t(string  = arg1)%!t(string = arg2)] 
   
 
 

我正在使用“ github.com/streadway/amqp” 库与我的Rabbitmq节点交谈。 似乎该库中未实现“按名称发送任务”。

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongpang9573 2018-06-17 13:25
    已采纳

    Here is an illustration of the symptoms. The fmt format string is apparently expecting boolean values.

    package main
    
    import "fmt"
    
    func main() {
        s := fmt.Sprintf("%t %t", "arg1", "arg2")
        fmt.Println(s)
        t := fmt.Sprintf("%t %t", true, false)
        fmt.Println(t)
    }
    

    Output:

    %!t(string=arg1) %!t(string=arg2)
    true false
    
    打赏 评论

相关推荐 更多相似问题