Erlang Programming Exercise 12-3: The Database Server as an Application
4 November 2011 Leave a Comment
First create an OTP Application directory structure. The work flow resembles something like the following if you’re on UNIX.
cd ~/work/erlang
mkdir ~/work/erlang/chapter12/{src,ebin,include,priv}
cp db_server_otp.erl db_server_sup.erl ~/work/erlang/chapter12/src
cd ~/work/erlang/chapter12/src
erlc *.erl
mv *.beam ../ebin
With the source code and BEAM files ready, now create an app file, db_server_app.erl:
-module(db_server_app).
-export([start/2,stop/1]).
-behavior(application).
start(_Type, _StartArgs) ->
db_server_sup:start().
stop(_State) ->
ok.
Compile this code and then move the BEAM file to the ebin folder. Create an application resource file describing the application, db_server.app in the ebin folder:
{application,
db_server,
[{description, "Erlang Prgramming Ch. 12 DB Server"},
{vsn, "1.0"},
{modules, [db_server_otp, db_server_sup, db_server_app]},
{registered, [db_server_sup]},
{applications, [kernel, stdlib]},
{env, []},
{mod, {db_server_app, []}}
]
}.
The final directory structure should look like this:
~/work/erlang/chapter12$ ls -1 ebin include priv src ~/work/erlang/chapter12$ ls -1 ebin db_server.app db_server_app.beam db_server_otp.beam db_server_sup.beam ~/work/erlang/chapter12$ ls -1 src db_server_app.erl db_server_otp.erl db_server_sup.erl
With the BEAM files and app files in the ebin directory, the application can now be tested with the Erlang shell.
Erlang R13B03 (erts-5.7.4) [64-bit] [smp:8:2] [rq:8] [async-threads:0] [hipe] [kernel-poll:false]
Eshell V5.7.4 (abort with ^G)
1> code:add_path("/home/bryan/work/erlang/chapter12/ebin").
true
2> application:start(db_server).
ok
3> db_server_otp:write("Ted", "7").
ok
4> db_server_otp:read("Ted").
{ok,"7"}
5> application:stop(db_server).
ok
=INFO REPORT==== 3-Nov-2011::16:32:08 ===
application: db_server
exited: stopped
type: temporary
6> whereis(db_server_sup).
undefined
Success! (Note: this was much easier than the first time I tried to get a Tomcat servlet working…)