如果客户端配置创建临时队列接收消息,是否可以配置只允许创建一个队列?如果有缺陷的客户端在服务器上创建了太多队列,对服务器来说会是一个大问题吗?
如何配置以防止客户端浪费资源?
-module(amqp_example).
-include("amqp_client.hrl").
-compile([export_all]).
test() ->
%% Start a network connection
{ok, Connection} = amqp_connection:start(#amqp_params_network{}),
%% Open a channel on the connection
{ok, Channel} = amqp_connection:open_channel(Connection),
%% Declare a queue
#'queue.declare_ok'{queue = Q}
= amqp_channel:call(Channel, #'queue.declare'{}),
ok = create_temp_queue(Channel,10),
%% Publish a message
Payload = <<"foobar">>,
Publish = #'basic.publish'{exchange = <<>>, routing_key = Q},
amqp_channel:cast(Channel, Publish, #amqp_msg{payload = Payload}),
%% Get the message back from the queue
Get = #'basic.get'{queue = Q},
{#'basic.get_ok'{delivery_tag = Tag}, _Content}
= amqp_channel:call(Channel, Get),
%% Do something with the message payload
%% (some work here)
%% Ack the message
amqp_channel:cast(Channel, #'basic.ack'{delivery_tag = Tag}),
%% Close the channel
amqp_channel:close(Channel),
%% Close the connection
amqp_connection:close(Connection),
ok.
create_temp_queue(Channel,Loop)->
[ %% Declare a queue
#'queue.declare_ok'{queue = _Q}
= amqp_channel:call(Channel, #'queue.declare'{})
||
_X <- lists:seq(1,Loop)].
(emacs@yus-iMac.local)58> amqp_example:test().
** exception error: no match of right hand side value
[{'queue.declare_ok',
<<"amq.gen-AqAaMLydgMf43y_XoYSdq5">>,0,0},
{'queue.declare_ok',
<<"amq.gen-A75g--nsvheNbwYMr34M-E">>,0,0},
{'queue.declare_ok',
<<"amq.gen-wmkOrALHBIj6Ot6ZuZZOQJ">>,0,0},
{'queue.declare_ok',
<<"amq.gen-wX2NmwMHBeDaKLvoZgJhEh">>,0,0},
{'queue.declare_ok',
<<"amq.gen-gcvScDp-RFMVwxWpyWjI-9">>,0,0},
{'queue.declare_ok',
<<"amq.gen-Q4CS7jNu3cde0RNdVdO3PJ">>,0,0},
{'queue.declare_ok',
<<"amq.gen-QKNrG8IJPVvfAlLukq38x_">>,0,0},
{'queue.declare_ok',
<<"amq.gen-wqJ2V1HQDaJjOzRDhv8gT4">>,0,0},
{'queue.declare_ok',
<<"amq.gen-AYeZiuNYsFOUMVw6xKcZh4">>,0,0},
{'queue.declare_ok',
<<"amq.gen-AJDqT2h2fq9cZOsVbNESi0">>,0,0}]
in function amqp_example:test/0 (src/amqp_example.erl, line 16)
yus-iMac:~ yuchen$ sudo rabbitmqctl list_queues
Password:
Listing queues ...
amq.gen-Q4CS7jNu3cde0RNdVdO3PJ 0
amq.gen-QKNrG8IJPVvfAlLukq38x_ 0
amq.gen-AqAaMLydgMf43y_XoYSdq5 0
amq.gen-AJDqT2h2fq9cZOsVbNESi0 0
amq.gen-wqJ2V1HQDaJjOzRDhv8gT4 0
amq.gen-AYeZiuNYsFOUMVw6xKcZh4 0
amq.gen-wzvWzxXo2MJVZsyrwfzM8A 0
amq.gen-A75g--nsvheNbwYMr34M-E 0
amq.gen-gcvScDp-RFMVwxWpyWjI-9 0
amq.gen-wX2NmwMHBeDaKLvoZgJhEh 0
amq.gen-wmkOrALHBIj6Ot6ZuZZOQJ 0
...done.