RabbitMQ: How to send Dead Letter Exchange from Erlang client

357 views Asked by At

I want to send Dead Letter Exchange from Erlang client but tried for few days but unable to figure out how while I can send it through Ruby client easily.

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}
)

Thanks

1

There are 1 answers

0
Gabriele Santomaggio On BEST ANSWER

The problem is when you set the arguments.

Here is the code:

 %% 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}
  ),

when you set the argument you have to specify also the type (longstr, signedint).

I bound a queue to the ex"my_exchange" then I published a message to the queue:"myqueue12" and it worked correctly:

enter image description here

Hope it helps.