17.01.2015 Views

Erlang and OTP in Action.pdf - Synrc

Erlang and OTP in Action.pdf - Synrc

Erlang and OTP in Action.pdf - Synrc

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

246<br />

All of this <strong>in</strong>formation is important for the startup <strong>and</strong> proper function<strong>in</strong>g of the App.<br />

However, two pieces of <strong>in</strong>formation are especially important <strong>in</strong> the context of Releases. That<br />

<strong>in</strong>formation is the ‘vsn’ <strong>and</strong> the ‘mod’ tuples. The ‘vsn’ details the version of this application.<br />

You will see specifically why it’s useful later. The mod entry <strong>in</strong>dicates to the application<br />

<strong>in</strong>frastructure which module implements the application behaviour <strong>in</strong>terface <strong>and</strong> is therefore<br />

the start<strong>in</strong>g po<strong>in</strong>t for the application. Figure 10.4 is the application behaviour implementation<br />

for the simple_cache that the mod entry above <strong>in</strong> code list<strong>in</strong>g 10.3 refers to.<br />

Code List<strong>in</strong>g 10.2 - Application Behaviour<br />

-module(sc_app).<br />

-behaviour(application).<br />

%% Application callbacks<br />

-export([start/2, stop/1]).<br />

start(_StartType, _StartArgs) -><br />

sc_store:<strong>in</strong>it(),<br />

case sc_sup:start_l<strong>in</strong>k() of<br />

{ok, Pid} -><br />

{ok, Pid};<br />

Error -><br />

Error<br />

end.<br />

stop(_State) -><br />

ok.<br />

The start function starts the application <strong>and</strong> the stop function stops the application.<br />

Notice that <strong>in</strong> the start function for this module we do someth<strong>in</strong>g just a bit more then start<br />

the root supervisor. We call this sc_store:<strong>in</strong>it() rout<strong>in</strong>e. We call this out to illustrate the fact<br />

that the application behaviour implementation is the entry po<strong>in</strong>t, the startup po<strong>in</strong>t for your<br />

liv<strong>in</strong>g breath<strong>in</strong>g application. Whatever startup/shutdown functionality you have should end<br />

up here. We wouldn't suggest you add the code itself here. Put that <strong>in</strong> other, well named<br />

modules, but you should call that <strong>in</strong>itialization <strong>and</strong> shutdown code here if you have it.<br />

Remember that this start<strong>in</strong>g po<strong>in</strong>t for applications is just a behaviour implementation. That<br />

means that it conforms to a behaviour <strong>in</strong>terface <strong>and</strong> that it has an associated behaviour<br />

conta<strong>in</strong>er which conta<strong>in</strong>s all the functionality generic to application h<strong>and</strong>l<strong>in</strong>g. When the start<br />

function completes <strong>and</strong> returns the pid of the top level supervisor it is return<strong>in</strong>g that pid to<br />

the application conta<strong>in</strong>er. This conta<strong>in</strong>er is responsible for more than just manag<strong>in</strong>g the<br />

startup of that supervisor. It also h<strong>and</strong>les th<strong>in</strong>gs manag<strong>in</strong>g the load<strong>in</strong>g of the very compiled<br />

object code for the application, it can also stop it <strong>and</strong> even unload it from the VM. The<br />

application conta<strong>in</strong>er performs these functions for not just a s<strong>in</strong>gle application but for many.<br />

©Mann<strong>in</strong>g Publications Co. Please post comments or corrections to the Author Onl<strong>in</strong>e forum:<br />

http://www.mann<strong>in</strong>g-s<strong>and</strong>box.com/forum.jspaforumID=454

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!