Sophie

Sophie

distrib > Fedora > 17 > i386 > media > updates > by-pkgid > 675c8c8167236dfcf8d66da674f931e8 > files > 4

erlang-doc-R15B-03.3.fc17.noarch.rpm

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html xmlns:fn="http://www.w3.org/2005/02/xpath-functions">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" href="../otp_doc.css" type="text/css">
<title>Erlang -- Applications</title>
</head>
<body bgcolor="white" text="#000000" link="#0000ff" vlink="#ff00ff" alink="#ff0000"><div id="container">
<script id="js" type="text/javascript" language="JavaScript" src="../js/flipmenu/flipmenu.js"></script><script id="js2" type="text/javascript" src="../js/erlresolvelinks.js"></script><script language="JavaScript" type="text/javascript">
            <!--
              function getWinHeight() {
                var myHeight = 0;
                if( typeof( window.innerHeight ) == 'number' ) {
                  //Non-IE
                  myHeight = window.innerHeight;
                } else if( document.documentElement && ( document.documentElement.clientWidth ||
                                                         document.documentElement.clientHeight ) ) {
                  //IE 6+ in 'standards compliant mode'
                  myHeight = document.documentElement.clientHeight;
                } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
                  //IE 4 compatible
                  myHeight = document.body.clientHeight;
                }
                return myHeight;
              }

              function setscrollpos() {
                var objf=document.getElementById('loadscrollpos');
                 document.getElementById("leftnav").scrollTop = objf.offsetTop - getWinHeight()/2;
              }

              function addEvent(obj, evType, fn){
                if (obj.addEventListener){
                obj.addEventListener(evType, fn, true);
                return true;
              } else if (obj.attachEvent){
                var r = obj.attachEvent("on"+evType, fn);
                return r;
              } else {
                return false;
              }
             }

             addEvent(window, 'load', setscrollpos);

             //--></script><div id="leftnav"><div class="innertube">
<img alt="Erlang logo" src="../erlang-logo.png"><br><small><a href="users_guide.html">User's Guide</a><br><a href="../pdf/otp-system-documentation-5.9.3.1.pdf">PDF</a><br><a href="../index.html">Top</a></small><p><strong>OTP Design Principles</strong><br><strong>User's Guide</strong><br><small>Version 5.9.3.1</small></p>
<br><a href="javascript:openAllFlips()">Expand All</a><br><a href="javascript:closeAllFlips()">Contract All</a><p><small><strong>Chapters</strong></small></p>
<ul class="flipMenu" imagepath="../js/flipmenu">
<li id="no" title="Overview" expanded="false">Overview<ul>
<li><a href="des_princ.html">
              Top of chapter
            </a></li>
<li title="Supervision Trees"><a href="des_princ.html#id65609">Supervision Trees</a></li>
<li title="Behaviours"><a href="des_princ.html#id59741">Behaviours</a></li>
<li title="Applications"><a href="des_princ.html#id66345">Applications</a></li>
<li title="Releases"><a href="des_princ.html#id61095">Releases</a></li>
<li title="Release Handling"><a href="des_princ.html#id60860">Release Handling</a></li>
</ul>
</li>
<li id="no" title="Gen_Server Behaviour" expanded="false">Gen_Server Behaviour<ul>
<li><a href="gen_server_concepts.html">
              Top of chapter
            </a></li>
<li title="Client-Server Principles"><a href="gen_server_concepts.html#id64192">Client-Server Principles</a></li>
<li title="Example"><a href="gen_server_concepts.html#id61515">Example</a></li>
<li title="Starting a Gen_Server"><a href="gen_server_concepts.html#id67798">Starting a Gen_Server</a></li>
<li title="Synchronous Requests - Call"><a href="gen_server_concepts.html#id66815">Synchronous Requests - Call</a></li>
<li title="Asynchronous Requests - Cast"><a href="gen_server_concepts.html#id62843">Asynchronous Requests - Cast</a></li>
<li title="Stopping"><a href="gen_server_concepts.html#id67345">Stopping</a></li>
<li title="Handling Other Messages"><a href="gen_server_concepts.html#id67435">Handling Other Messages</a></li>
</ul>
</li>
<li id="no" title="Gen_Fsm Behaviour" expanded="false">Gen_Fsm Behaviour<ul>
<li><a href="fsm.html">
              Top of chapter
            </a></li>
<li title="Finite State Machines"><a href="fsm.html#id68991">Finite State Machines</a></li>
<li title="Example"><a href="fsm.html#id69046">Example</a></li>
<li title="Starting a Gen_Fsm"><a href="fsm.html#id69093">Starting a Gen_Fsm</a></li>
<li title="Notifying About Events"><a href="fsm.html#id69273">Notifying About Events</a></li>
<li title="Timeouts"><a href="fsm.html#id69352">Timeouts</a></li>
<li title="All State Events"><a href="fsm.html#id69391">All State Events</a></li>
<li title="Stopping"><a href="fsm.html#id69423">Stopping</a></li>
<li title="Handling Other Messages"><a href="fsm.html#id69514">Handling Other Messages</a></li>
</ul>
</li>
<li id="no" title="Gen_Event Behaviour" expanded="false">Gen_Event Behaviour<ul>
<li><a href="events.html">
              Top of chapter
            </a></li>
<li title="Event Handling Principles"><a href="events.html#id69613">Event Handling Principles</a></li>
<li title="Example"><a href="events.html#id69665">Example</a></li>
<li title="Starting an Event Manager"><a href="events.html#id69706">Starting an Event Manager</a></li>
<li title="Adding an Event Handler"><a href="events.html#id70832">Adding an Event Handler</a></li>
<li title="Notifying About Events"><a href="events.html#id70916">Notifying About Events</a></li>
<li title="Deleting an Event Handler"><a href="events.html#id70987">Deleting an Event Handler</a></li>
<li title="Stopping"><a href="events.html#id71062">Stopping</a></li>
<li title="Handling Other Messages"><a href="events.html#id71116">Handling Other Messages</a></li>
</ul>
</li>
<li id="no" title="Supervisor Behaviour" expanded="false">Supervisor Behaviour<ul>
<li><a href="sup_princ.html">
              Top of chapter
            </a></li>
<li title="Supervision Principles"><a href="sup_princ.html#id71210">Supervision Principles</a></li>
<li title="Example"><a href="sup_princ.html#id71235">Example</a></li>
<li title="Restart Strategy"><a href="sup_princ.html#id71306">Restart Strategy</a></li>
<li title="Maximum Restart Frequency"><a href="sup_princ.html#id71384">Maximum Restart Frequency</a></li>
<li title="Child Specification"><a href="sup_princ.html#id71906">Child Specification</a></li>
<li title="Starting a Supervisor"><a href="sup_princ.html#id72235">Starting a Supervisor</a></li>
<li title="Adding a Child Process"><a href="sup_princ.html#id72349">Adding a Child Process</a></li>
<li title="Stopping a Child Process"><a href="sup_princ.html#id72393">Stopping a Child Process</a></li>
<li title="Simple-One-For-One Supervisors"><a href="sup_princ.html#id72443">Simple-One-For-One Supervisors</a></li>
<li title="Stopping"><a href="sup_princ.html#id72567">Stopping</a></li>
</ul>
</li>
<li id="no" title="Sys and Proc_Lib" expanded="false">Sys and Proc_Lib<ul>
<li><a href="spec_proc.html">
              Top of chapter
            </a></li>
<li title="Simple Debugging"><a href="spec_proc.html#id72665">Simple Debugging</a></li>
<li title="Special Processes"><a href="spec_proc.html#id72749">Special Processes</a></li>
<li title="User-Defined Behaviours"><a href="spec_proc.html#id73359">User-Defined Behaviours</a></li>
</ul>
</li>
<li id="loadscrollpos" title="Applications" expanded="true">Applications<ul>
<li><a href="applications.html">
              Top of chapter
            </a></li>
<li title="Application Concept"><a href="applications.html#id73539">Application Concept</a></li>
<li title="Application Callback Module"><a href="applications.html#id73606">Application Callback Module</a></li>
<li title="Application Resource File"><a href="applications.html#id73720">Application Resource File</a></li>
<li title="Directory Structure"><a href="applications.html#id73969">Directory Structure</a></li>
<li title="Application Controller"><a href="applications.html#id74095">Application Controller</a></li>
<li title="Loading and Unloading Applications"><a href="applications.html#id74128">Loading and Unloading Applications</a></li>
<li title="Starting and Stopping Applications"><a href="applications.html#id74190">Starting and Stopping Applications</a></li>
<li title="Configuring an Application"><a href="applications.html#id74278">Configuring an Application</a></li>
<li title="Application Start Types"><a href="applications.html#id74477">Application Start Types</a></li>
</ul>
</li>
<li id="no" title="Included Applications" expanded="false">Included Applications<ul>
<li><a href="included_applications.html">
              Top of chapter
            </a></li>
<li title="Definition"><a href="included_applications.html#id74617">Definition</a></li>
<li title="Specifying Included Applications"><a href="included_applications.html#id74682">Specifying Included Applications</a></li>
<li title="Synchronizing Processes During Startup"><a href="included_applications.html#id74708">Synchronizing Processes During Startup</a></li>
</ul>
</li>
<li id="no" title="Distributed Applications" expanded="false">Distributed Applications<ul>
<li><a href="distributed_applications.html">
              Top of chapter
            </a></li>
<li title="Definition"><a href="distributed_applications.html#id74921">Definition</a></li>
<li title="Specifying Distributed Applications"><a href="distributed_applications.html#id74957">Specifying Distributed Applications</a></li>
<li title="Starting and Stopping Distributed Applications"><a href="distributed_applications.html#id75164">Starting and Stopping Distributed Applications</a></li>
<li title="Failover"><a href="distributed_applications.html#id75266">Failover</a></li>
<li title="Takeover"><a href="distributed_applications.html#id75408">Takeover</a></li>
</ul>
</li>
<li id="no" title="Releases" expanded="false">Releases<ul>
<li><a href="release_structure.html">
              Top of chapter
            </a></li>
<li title="Release Concept"><a href="release_structure.html#id75603">Release Concept</a></li>
<li title="Release Resource File"><a href="release_structure.html#id75656">Release Resource File</a></li>
<li title="Generating Boot Scripts"><a href="release_structure.html#id75803">Generating Boot Scripts</a></li>
<li title="Creating a Release Package"><a href="release_structure.html#id75897">Creating a Release Package</a></li>
<li title="Directory Structure"><a href="release_structure.html#id76046">Directory Structure</a></li>
</ul>
</li>
<li id="no" title="Release Handling" expanded="false">Release Handling<ul>
<li><a href="release_handling.html">
              Top of chapter
            </a></li>
<li title="Release Handling Principles"><a href="release_handling.html#id76299">Release Handling Principles</a></li>
<li title="Requirements"><a href="release_handling.html#id76543">Requirements</a></li>
<li title="Distributed Systems"><a href="release_handling.html#id76636">Distributed Systems</a></li>
<li title="Release Handling Instructions"><a href="release_handling.html#id76663">Release Handling Instructions</a></li>
<li title="Application Upgrade File"><a href="release_handling.html#id77177">Application Upgrade File</a></li>
<li title="Release Upgrade File"><a href="release_handling.html#id77359">Release Upgrade File</a></li>
<li title="Installing a Release"><a href="release_handling.html#id77515">Installing a Release</a></li>
<li title="Updating Application Specifications"><a href="release_handling.html#id77986">Updating Application Specifications</a></li>
</ul>
</li>
<li id="no" title="Appup Cookbook" expanded="false">Appup Cookbook<ul>
<li><a href="appup_cookbook.html">
              Top of chapter
            </a></li>
<li title="Changing a Functional Module"><a href="appup_cookbook.html#id78170">Changing a Functional Module</a></li>
<li title="Changing a Residence Module"><a href="appup_cookbook.html#id78193">Changing a Residence Module</a></li>
<li title="Changing a Callback Module"><a href="appup_cookbook.html#id78232">Changing a Callback Module</a></li>
<li title="Changing Internal State"><a href="appup_cookbook.html#id78283">Changing Internal State</a></li>
<li title="Module Dependencies"><a href="appup_cookbook.html#id78417">Module Dependencies</a></li>
<li title="Changing Code For a Special Process"><a href="appup_cookbook.html#id78588">Changing Code For a Special Process</a></li>
<li title="Changing a Supervisor"><a href="appup_cookbook.html#id78755">Changing a Supervisor</a></li>
<li title="Adding or Deleting a Module"><a href="appup_cookbook.html#id79000">Adding or Deleting a Module</a></li>
<li title="Starting or Terminating a Process"><a href="appup_cookbook.html#id79026">Starting or Terminating a Process</a></li>
<li title="Adding or Removing an Application"><a href="appup_cookbook.html#id79044">Adding or Removing an Application</a></li>
<li title="Restarting an Application"><a href="appup_cookbook.html#id79075">Restarting an Application</a></li>
<li title="Changing an Application Specification"><a href="appup_cookbook.html#id79117">Changing an Application Specification</a></li>
<li title="Changing Application Configuration"><a href="appup_cookbook.html#id79141">Changing Application Configuration</a></li>
<li title="Changing Included Applications"><a href="appup_cookbook.html#id79174">Changing Included Applications</a></li>
<li title="Changing Non-Erlang Code"><a href="appup_cookbook.html#id79414">Changing Non-Erlang Code</a></li>
<li title="Emulator Restart and Upgrade"><a href="appup_cookbook.html#id79499">Emulator Restart and Upgrade</a></li>
<li title="Emulator Upgrade from pre OTP R15"><a href="appup_cookbook.html#id79575">Emulator Upgrade from pre OTP R15</a></li>
</ul>
</li>
</ul>
</div></div>
<div id="content">
<div class="innertube">
<h1>7 Applications</h1>
  
  <a name="appl"></a>
  <p>This chapter should be read in conjunction with <span class="code">app(4)</span> and
    <span class="code">application(3)</span>.</p>

  <h3><a name="id73539">7.1 
        Application Concept</a></h3>
    
    <p>When we have written code implementing some specific
      functionality, we might want to make the code into an
      <strong>application</strong>, that is a component that can be started and
      stopped as a unit, and which can be re-used in other systems as
      well.</p>
    <p>To do this, we create an
      <span class="bold_code"><a href="#callback_module">application callback module</a></span>, where we describe how the application should
      be started and stopped.</p>
    <p>Then, an <strong>application specification</strong> is needed, which is
      put in an <span class="bold_code"><a href="#appl_res_file">application resource file</a></span>. Among other things, we specify which
      modules the application consists of and the name of the callback
      module.</p>
    <p>If we use <span class="code">systools</span>, the Erlang/OTP tools for packaging code
      (see <span class="bold_code"><a href="release_structure.html">Releases</a></span>),
      the code for each application is placed in a separate directory
      following a pre-defined <span class="bold_code"><a href="#app_dir">directory structure</a></span>.</p>
  

  <h3><a name="id73606">7.2 
        Application Callback Module</a></h3>
    <a name="callback_module"></a>
    
    <p>How to start and stop the code for the application, i.e.
      the supervision tree, is described by two callback functions:</p>
    <div class="example"><pre>
start(StartType, StartArgs) -&gt; {ok, Pid} | {ok, Pid, State}
stop(State)</pre></div>
    <p><span class="code">start</span> is called when starting the application and should
      create the supervision tree by starting the top supervisor.
      It is expected to return the pid of the top supervisor and an
      optional term <span class="code">State</span>, which defaults to []. This term is
      passed as-is to <span class="code">stop</span>.</p>
    <p><span class="code">StartType</span> is usually the atom <span class="code">normal</span>. It has other
      values only in the case of a takeover or failover, see
      <span class="bold_code"><a href="distributed_applications.html">Distributed Applications</a></span>. <span class="code">StartArgs</span> is defined by the key
      <span class="code">mod</span> in the <span class="bold_code"><a href="#appl_res_file">application resource file</a></span> file.</p>
    <p><span class="code">stop/1</span> is called <strong>after</strong> the application has been
      stopped and should do any necessary cleaning up. Note that
      the actual stopping of the application, that is the shutdown of
      the supervision tree, is handled automatically as described in
      <span class="bold_code"><a href="#stopping">Starting and Stopping Applications</a></span>.</p>
    <a name="ch_app"></a>
    <p>Example of an application callback module for packaging
      the supervision tree from
      the <span class="bold_code"><a href="sup_princ.html#ex">Supervisor</a></span> chapter:</p>
    <div class="example"><pre>
-module(ch_app).
-behaviour(application).

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

start(_Type, _Args) -&gt;
    ch_sup:start_link().

stop(_State) -&gt;
    ok.</pre></div>
    <p>A library application, which can not be started or stopped,
      does not need any application callback module.</p>
  

  <h3><a name="id73720">7.3 
        Application Resource File</a></h3>
    <a name="appl_res_file"></a>
    
    <p>To define an application, we create an <strong>application specification</strong> which is put in an <strong>application resource file</strong>, or in short <span class="code">.app</span> file:</p>
    <div class="example"><pre>
{application, Application, [Opt1,...,OptN]}.</pre></div>
    <p><span class="code">Application</span>, an atom, is the name of the application.
      The file must be named <span class="code">Application.app</span>.</p>
    <p>Each <span class="code">Opt</span> is a tuple <span class="code">{Key, Value}</span> which define a
      certain property of the application. All keys are optional.
      Default values are used for any omitted keys.</p>
    <p>The contents of a minimal <span class="code">.app</span> file for a library
      application <span class="code">libapp</span> looks like this:</p>
    <div class="example"><pre>
{application, libapp, []}.</pre></div>
    <p>The contents of a minimal <span class="code">.app</span> file <span class="code">ch_app.app</span> for
      a supervision tree application like <span class="code">ch_app</span> looks like this:</p>
    <div class="example"><pre>
{application, ch_app,
 [{mod, {ch_app,[]}}]}.</pre></div>
    <p>The key <span class="code">mod</span> defines the callback module and start
      argument of the application, in this case <span class="code">ch_app</span> and
      [], respectively. This means that</p>
    <div class="example"><pre>
ch_app:start(normal, [])</pre></div>
    <p>will be called when the application should be started and</p>
    <div class="example"><pre>
ch_app:stop([])</pre></div>
    <p>will be called when the application has been stopped.</p>
    <p>When using <span class="code">systools</span>, the Erlang/OTP tools for packaging
      code (see <span class="bold_code"><a href="release_structure.html">Releases</a></span>),
      the keys <span class="code">description</span>, <span class="code">vsn</span>, <span class="code">modules</span>,
      <span class="code">registered</span> and <span class="code">applications</span> should also be
      specified:</p>
    <div class="example"><pre>
{application, ch_app,
 [{description, "Channel allocator"},
  {vsn, "1"},
  {modules, [ch_app, ch_sup, ch3]},
  {registered, [ch3]},
  {applications, [kernel, stdlib, sasl]},
  {mod, {ch_app,[]}}
 ]}.</pre></div>
    <dl>
      <dt><strong><span class="code">description</span></strong></dt>
      <dd>A short description, a string. Defaults to "".</dd>
      <dt><strong><span class="code">vsn</span></strong></dt>
      <dd>Version number, a string. Defaults to "".</dd>
      <dt><strong><span class="code">modules</span></strong></dt>
      <dd>All modules <strong>introduced</strong> by this application.
      <span class="code">systools</span> uses this list when generating boot scripts and
       tar files. A module must be defined in one and only one
       application. Defaults to [].</dd>
      <dt><strong><span class="code">registered</span></strong></dt>
      <dd>All names of registered processes in the application.
      <span class="code">systools</span> uses this list to detect name clashes
       between applications. Defaults to [].</dd>
      <dt><strong><span class="code">applications</span></strong></dt>
      <dd>All applications which must be started before this
       application is started. <span class="code">systools</span> uses this list to
       generate correct boot scripts. Defaults to [], but note that
       all applications have dependencies to at least <span class="code">kernel</span>
       and <span class="code">stdlib</span>.</dd>
    </dl>
    <p>The syntax and contents of of the application resource file
      are described in detail in <span class="code">app(4)</span>.</p>
  

  <h3><a name="id73969">7.4 
        Directory Structure</a></h3>
    <a name="app_dir"></a>
    
    <p>When packaging code using <span class="code">systools</span>, the code for each
      application is placed in a separate directory
      <span class="code">lib/Application-Vsn</span>, where <span class="code">Vsn</span> is the version number.</p>
    <p>This may be useful to know, even if <span class="code">systools</span> is not used,
      since Erlang/OTP itself is packaged according to the OTP principles
      and thus comes with this directory structure. The code server
      (see <span class="code">code(3)</span>) will automatically use code from
      the directory with the highest version number, if there are
      more than one version of an application present.</p>
    <p>The application directory structure can of course be used in
      the development environment as well. The version number may then
      be omitted from the name.</p>
    <p>The application directory have the following sub-directories:</p>
    <ul>
      <li><span class="code">src</span></li>
      <li><span class="code">ebin</span></li>
      <li><span class="code">priv</span></li>
      <li><span class="code">include</span></li>
    </ul>
    <dl>
      <dt><strong><span class="code">src</span></strong></dt>
      <dd>Contains the Erlang source code.</dd>
      <dt><strong><span class="code">ebin</span></strong></dt>
      <dd>Contains the Erlang object code, the <span class="code">beam</span> files.
       The <span class="code">.app</span> file is also placed here.</dd>
      <dt><strong><span class="code">priv</span></strong></dt>
      <dd>Used for application specific files. For example, C
       executables are placed here. The function <span class="code">code:priv_dir/1</span>
       should be used to access this directory.</dd>
      <dt><strong><span class="code">include</span></strong></dt>
      <dd>Used for include files.</dd>
    </dl>
  

  <h3><a name="id74095">7.5 
        Application Controller</a></h3>
    <a name="application_controller"></a>
    
    <p>When an Erlang runtime system is started, a number of processes
      are started as part of the Kernel application. One of these
      processes is the <strong>application controller</strong> process,
      registered as <span class="code">application_controller</span>.</p>
    <p>All operations on applications are coordinated by the application
      controller. It is interfaced through the functions in
      the module <span class="code">application</span>, see <span class="code">application(3)</span>.
      In particular, applications can be loaded, unloaded, started and
      stopped.</p>
  

  <h3><a name="id74128">7.6 
        Loading and Unloading Applications</a></h3>
    
    <p>Before an application can be started, it must be <strong>loaded</strong>.
      The application controller reads and stores the information from
      the <span class="code">.app</span> file.</p>
    <div class="example"><pre>
1&gt; <span class="bold_code">application:load(ch_app).</span>
ok
2&gt; <span class="bold_code">application:loaded_applications().</span>
[{kernel,"ERTS  CXC 138 10","2.8.1.3"},
 {stdlib,"ERTS  CXC 138 10","1.11.4.3"},
 {ch_app,"Channel allocator","1"}]</pre></div>
    <p>An application that has been stopped, or has never been started,
      can be unloaded. The information about the application is
      erased from the internal database of the application controller.</p>
    <div class="example"><pre>
3&gt; <span class="bold_code">application:unload(ch_app).</span>
ok
4&gt; <span class="bold_code">application:loaded_applications().</span>
[{kernel,"ERTS  CXC 138 10","2.8.1.3"},
 {stdlib,"ERTS  CXC 138 10","1.11.4.3"}]</pre></div>
    <div class="note">
<div class="label">Note</div>
<div class="content"><p>
      <p>Loading/unloading an application does not load/unload the code
        used by the application. Code loading is done the usual way.</p>
    </p></div>
</div>
  

  <h3><a name="id74190">7.7 
        Starting and Stopping Applications</a></h3>
    <a name="stopping"></a>
    
    <p>An application is started by calling:</p>
    <div class="example"><pre>
5&gt; <span class="bold_code">application:start(ch_app).</span>
ok
6&gt; <span class="bold_code">application:which_applications().</span>
[{kernel,"ERTS  CXC 138 10","2.8.1.3"},
 {stdlib,"ERTS  CXC 138 10","1.11.4.3"},
 {ch_app,"Channel allocator","1"}]</pre></div>
    <p>If the application is not already loaded, the application
      controller will first load it using <span class="code">application:load/1</span>. It
      will check the value of the <span class="code">applications</span> key, to ensure
      that all applications that should be started before this
      application are running.</p>
    <a name="application_master"></a>
    <p>The application controller then creates an <strong>application master</strong> for the application. The application master is
      the group leader of all the processes in the application.
      The application master starts the application by calling
      the application callback function <span class="code">start/2</span> in the module,
      and with the start argument, defined by the <span class="code">mod</span> key in
      the <span class="code">.app</span> file.</p>
    <p>An application is stopped, but not unloaded, by calling:</p>
    <div class="example"><pre>
7&gt; <span class="bold_code">application:stop(ch_app).</span>
ok</pre></div>
    <p>The application master stops the application by telling the top
      supervisor to shutdown. The top supervisor tells all its child
      processes to shutdown etc. and the entire tree is terminated in
      reversed start order. The application master then calls
      the application callback function <span class="code">stop/1</span> in the module
      defined by the <span class="code">mod</span> key.</p>
  

  <h3><a name="id74278">7.8 
        Configuring an Application</a></h3>
    
    <p>An application can be configured using <strong>configuration parameters</strong>. These are a list of <span class="code">{Par, Val}</span> tuples
      specified by a key <span class="code">env</span> in the <span class="code">.app</span> file.</p>
    <div class="example"><pre>
{application, ch_app,
 [{description, "Channel allocator"},
  {vsn, "1"},
  {modules, [ch_app, ch_sup, ch3]},
  {registered, [ch3]},
  {applications, [kernel, stdlib, sasl]},
  {mod, {ch_app,[]}},
  {env, [{file, "/usr/local/log"}]}
 ]}.</pre></div>
    <p><span class="code">Par</span> should be an atom, <span class="code">Val</span> is any term.
      The application can retrieve the value of a configuration
      parameter by calling <span class="code">application:get_env(App, Par)</span> or a
      number of similar functions, see <span class="code">application(3)</span>.</p>
    <p>Example:</p>
    <div class="example"><pre>
% <span class="bold_code">erl</span>
Erlang (BEAM) emulator version 5.2.3.6 [hipe] [threads:0]

Eshell V5.2.3.6  (abort with ^G)
1&gt; <span class="bold_code">application:start(ch_app).</span>
ok
2&gt; <span class="bold_code">application:get_env(ch_app, file).</span>
{ok,"/usr/local/log"}</pre></div>
    <p>The values in the <span class="code">.app</span> file can be overridden by values
      in a <strong>system configuration file</strong>. This is a file which
      contains configuration parameters for relevant applications:</p>
    <div class="example"><pre>
[{Application1, [{Par11,Val11},...]},
 ...,
 {ApplicationN, [{ParN1,ValN1},...]}].</pre></div>
    <p>The system configuration should be called <span class="code">Name.config</span> and
      Erlang should be started with the command line argument
      <span class="code">-config Name</span>. See <span class="code">config(4)</span> for more information.</p>
    <p>Example: A file <span class="code">test.config</span> is created with the following
      contents:</p>
    <div class="example"><pre>
[{ch_app, [{file, "testlog"}]}].</pre></div>
    <p>The value of <span class="code">file</span> will override the value of <span class="code">file</span>
      as defined in the <span class="code">.app</span> file:</p>
    <div class="example"><pre>
% <span class="bold_code">erl -config test</span>
Erlang (BEAM) emulator version 5.2.3.6 [hipe] [threads:0]

Eshell V5.2.3.6  (abort with ^G)
1&gt; <span class="bold_code">application:start(ch_app).</span>
ok
2&gt; <span class="bold_code">application:get_env(ch_app, file).</span>
{ok,"testlog"}</pre></div>
    <p>If
      <span class="bold_code"><a href="release_handling.html#sys">release handling</a></span>
      is used, exactly one system configuration file should be used and
      that file should be called <span class="code">sys.config</span></p>
    <p>The values in the <span class="code">.app</span> file, as well as the values in a
      system configuration file, can be overridden directly from
      the command line:</p>
    <div class="example"><pre>
% <span class="bold_code">erl -ApplName Par1 Val1 ... ParN ValN</span></pre></div>
    <p>Example:</p>
    <div class="example"><pre>
% <span class="bold_code">erl -ch_app file '"testlog"'</span>
Erlang (BEAM) emulator version 5.2.3.6 [hipe] [threads:0]

Eshell V5.2.3.6  (abort with ^G)
1&gt; <span class="bold_code">application:start(ch_app).</span>
ok
2&gt; <span class="bold_code">application:get_env(ch_app, file).</span>
{ok,"testlog"}</pre></div>
  

  <h3><a name="id74477">7.9 
        Application Start Types</a></h3>
    
    <p>A <strong>start type</strong> is defined when starting the application:</p>
    <div class="example"><pre>
application:start(Application, Type)</pre></div>
    <p><span class="code">application:start(Application)</span> is the same as calling
      <span class="code">application:start(Application, temporary)</span>. The type can
      also be <span class="code">permanent</span> or <span class="code">transient</span>:</p>
    <ul>
      <li>If a permanent application terminates, all other
       applications and the runtime system are also terminated.</li>
      <li>If a transient application terminates with reason
      <span class="code">normal</span>, this is reported but no other applications are
       terminated. If a transient application terminates abnormally,
       that is with any other reason than <span class="code">normal</span>, all other
       applications and the runtime system are also terminated.</li>
      <li>If a temporary application terminates, this is reported but
       no other applications are terminated.</li>
    </ul>
    <p>It is always possible to stop an application explicitly by
      calling <span class="code">application:stop/1</span>. Regardless of the mode, no
      other applications will be affected.</p>
    <p>Note that transient mode is of little practical use, since when
      a supervision tree terminates, the reason is set to
      <span class="code">shutdown</span>, not <span class="code">normal</span>.</p>
  
</div>
<div class="footer">
<hr>
<p>Copyright © 1997-2012 Ericsson AB. All Rights Reserved.</p>
</div>
</div>
</div></body>
</html>