我們可以使用多種程式語言來撰寫Rabbitmq的client,包括java, perl, python, erlang...等, 這篇是介紹如何使用Eclipse來寫Erlang Rabbitmq Client,此篇著重在開發的步驟並未詳細介紹程式碼的細節,細節部份可參考這篇文章Erlang Client Guide
步驟如下
1.安裝Erlang, Rabbitmq, Eclipse
2.在Eclise安裝Erlan plugin Erlide
在Eclipse點選Help → Install new software..., 填入http://erlide.org/update後執行安裝
3.下載Erlang library
到此下載amqp_client-3.2.3, rabbit_common-3.2.3,下載完後解縮到任意位置,例如/Users/james/Develop/amqp_client-3.2.3/, /Users/james/Develop/rabbit_common-3.2.3/
4.設定Erlang library路徑
在自已的家目錄下建立.erlang,並寫入步驟3所下載的2個library路徑,記得路徑要指到ebin目錄,例如下列
code:add_pathz("/Users/james/Develop/amqp_client-3.2.3/ebin"). code:add_pathz("/Users/james/Develop/rabbit_common-3.2.3/ebin").
這樣才會讓erlang在執行時期在上列的路徑找到所下載的library
5.使用eclipse建立erlang project
在Eclipse裡點File->New->Project...,找Erlang Project,填入project name後按finish即可。
再將amqp_client.hrl, rabbit.hrl, rabbit_framing.hrl放到project裡的include目錄,如下圖
註:amqp_client.hrl檔案在下載回來的amqp_client-3.2.3/include裡,rabbit.hrl, rabbit_framing.hrl檔案在下載回來的rabbit_common-3.2.3/include裡
6.撰寫erlang client
在src目錄下建立amqp_example.erl並填入下列內容
-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),
%% 建立一個queue
#'queue.declare_ok'{queue = Q}
= amqp_channel:call(Channel, #'queue.declare'{}),
%% 送一則message至queue
Payload = <<"foobar">>,
Publish = #'basic.publish'{exchange = <<>>, routing_key = Q},
amqp_channel:cast(Channel, Publish, #amqp_msg{payload = Payload}),
%% 再將message從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}),
%% 關閉channel
amqp_channel:close(Channel),
%% 關閉connection
amqp_connection:close(Connection),
ok.
7.執行
Eclipse會自動將erl compile後放至ebin目錄下例如amqp_example.beam,(如果沒有看到的話代表編譯失敗,請檢查是否有語法錯誤),我們要到erlang環境裡執行該檔案,步驟如下
- 在shell裡切換到ebin目錄 ex:cd /Users/james/Documents/workspaces/kokola/RabbitmqTest/ebin
- 進入erl
- 列出erlang當前載入的清單 io:format("~p~n", [code:get_path()]).。如果畫面顯示的載入清單沒有看到amqp_client, rabbit_common的話請檢查步驟4描述的.erlang檔內的路徑是否正確,該檔如果有修改的話需要重新進入shell
- 執行amqp_example
註:如果有繼續在eclipse裡修改.erl檔時記得要在在erl裡重新載入該module,否則erlang會繼續執行先前編譯的版本。載入的指令如l(amqp_example).
實際執行畫面大致如下
jamestekiMacBook-Pro:~ james$ cd /Users/james/Documents/workspaces/kokola/RabbitmqTest/ebin
jamestekiMacBook-Pro:ebin james$ erl
Erlang R16B02 (erts-5.10.3) [source] [64-bit] [smp:4:4] [async-threads:10] [kernel-poll:false]
Eshell V5.10.3 (abort with ^G)
1> io:format("~p~n", [code:get_path()]).
[".","/usr/local/lib/erlang/lib/kernel-2.16.3/ebin",
"/usr/local/lib/erlang/lib/stdlib-1.19.3/ebin",
"/usr/local/lib/erlang/lib/xmerl-1.3.4/ebin",
......(略)
"/usr/local/lib/erlang/lib/common_test-1.7.3/ebin",
"/usr/local/lib/erlang/lib/asn1-2.0.3/ebin",
"/usr/local/lib/erlang/lib/appmon-2.1.14.2/ebin",
"/Users/james/Develop/erlang-lib/amqp_client-3.2.3/ebin",
"/Users/james/Develop/erlang-lib/rabbit_common-3.2.3/ebin"]
ok
2> amqp_example:test().
ok
3> l(amqp_example).
{module,amqp_example}
4> amqp_example:test().
ok
沒有留言:
張貼留言