krot

rabbitmq

Aug 24th, 2020
472
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Erlang 1.51 KB | None | 0 0
  1. rq_insert(Index, Type, DocId, Doc)->
  2.         DocNew = #{<<"i">>=>Index,<<"t">>=>Type,<<"id">>=>DocId,<<"doc">>=>Doc},
  3.         PacketMQ = jiffy:encode(DocNew),
  4.        
  5.         %Z=zlib:open(),
  6.         %zlib:deflateInit(Z),
  7.         %CData=list_to_binary(zlib:deflate(Z, PacketMQ, finish)),
  8.         %zlib:deflateEnd(Z),
  9.         %zlib:close(Z),
  10.          CData=zlib:gzip(PacketMQ),
  11.  
  12.        
  13.         MQParam =gen_mod:get_module_opt(global, mod_custom_mam, rabbitmq,[{host,"localhost"}]),
  14.  
  15.         {ok, Connection} = amqp_connection:start(#amqp_params_network{
  16.                             host=proplists:get_value(host,MQParam,"localhost"),
  17.                             port=proplists:get_value(port,MQParam),
  18.                             username=proplists:get_value(username,MQParam),
  19.                             password=proplists:get_value(password,MQParam)
  20.                             }),
  21.        
  22.             Queue = proplists:get_value(queue,MQParam),
  23.        
  24.             {ok, Channel} = amqp_connection:open_channel(Connection),
  25.  
  26.                 amqp_channel:call(Channel, #'queue.declare'{
  27.                                             queue = Queue,
  28.                                             durable   = true
  29.                                             }),
  30.  
  31.                 amqp_channel:cast(Channel,
  32.                                   #'basic.publish'{
  33.                                     exchange = <<"">>,
  34.                                     routing_key = Queue},
  35.                                   #amqp_msg{
  36.                                             props = #'P_basic'{delivery_mode = 2,content_encoding= <<"gzip">> },
  37.                                              
  38.                                             payload = CData
  39.                                             }
  40.                                             ),
  41.                
  42.                 io:format(" [x] Sent 'Hello World!' ~p ~n",[size(CData)]),
  43.                 ok = amqp_channel:close(Channel),
  44.          ok = amqp_connection:close(Connection).
  45. %https://www.rabbitmq.com/erlang-client-user-guide.html
  46. %https://github.com/pma/amqp/blob/master/lib/amqp/basic.ex#L68
Add Comment
Please, Sign In to add comment