RabbitMQ:如何从 Erlang 客户端发送死信交换

RabbitMQ: How to send Dead Letter Exchange from Erlang client

我想从 Erlang 客户端发送 Dead Letter Exchange,但尝试了几天但无法弄清楚如何通过 Ruby 客户端轻松发送它。

amqp_channel:call(Channel, #'queue.declare'{
  queue = QueueName,
  arguments = [{<<"x-dead-letter-exchange">>, <<"">>}, {<<"x-dead-letter-routing-key">>, <<"task_pool1">>}, {<<"x-message-ttl">>, UnhideInMinute*60000}],
  durable = true}
),
io:format("DECLARED CHANNEL.~n"),
amqp_channel:cast(Channel,
  #'basic.publish'{
    exchange = <<"">>,
    routing_key = QueueName
    },
  #amqp_msg{props = #'P_basic'{delivery_mode = 2},
  payload = JsonMsg}
)

谢谢

问题出在你设置参数的时候。

代码如下:

 %% Start a network connection
  {ok, Connection} = amqp_connection:start(#amqp_params_network{}),
  erlang:display("Connection established"),
  Ex=#'exchange.declare'{exchange    = <<"my_exchange">>,
    ticket      = 0,
    type        = <<"topic">>,
    passive     = false,
    durable     = false,
    auto_delete = false,
    internal    = false,
    nowait      = false,
    arguments   = []},

  {ok, Channel} = amqp_connection:open_channel(Connection),
  amqp_channel:call(Channel, Ex),
  erlang:display("Exchange created"),
  amqp_channel:call(Channel, #'queue.declare'{
    queue = <<"myqueue12">>,
    arguments = [{<<"x-dead-letter-exchange">>,longstr, <<"my_exchange">>}, {<<"x-dead-letter-routing-key">>,longstr, <<"task_pool1">>}, {<<"x-message-ttl">>, signedint,60000}],
    durable = true}
  ),

设置参数时还必须指定类型 (longstr, signedint)

我将队列绑定到 ex"my_exchange" 然后我向队列发布了一条消息:"myqueue12" 并且它正常工作:

希望对您有所帮助。