`
coderplay
  • 浏览: 576805 次
  • 性别: Icon_minigender_1
  • 来自: 广州杭州
社区版块
存档分类
最新评论

OTP设计原则:应用

阅读更多

7 Applications


This chapter should be read in conjunction with app(4) and application(3).

7.1 Application Concept

When we have written code implementing some specific functionality, we might want to make the code into an application, that is a component that can be started and stopped as a unit, and which can be re-used in other systems as well.

To do this, we create an application callback module, where we describe how the application should be started and stopped.

Then, an application specification is needed, which is put in an application resource file. Among other things, we specify which modules the application consists of and the name of the callback module.

If we use systools, the Erlang/OTP tools for packaging code (see Releases), the code for each application is placed in a separate directory following a pre-defined directory structure.

7.2 Application Callback Module

How to start and stop the code for the application, i.e. the supervision tree, is described by two callback functions:

start(StartType, StartArgs) -> {ok, Pid} | {ok, Pid, State}
stop(State)

start 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 State, which defaults to []. This term is passed as-is to stop.

StartType is usually the atom normal. It has other values only in the case of a takeover or failover, see Distributed Applications. StartArgs is defined by the key mod in the application resource file file.

stop/1 is called after 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 Starting and Stopping Applications.

Example of an application callback module for packaging the supervision tree from the Supervisor chapter:

-module(ch_app).
-behaviour(application).

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

start(_Type, _Args) ->
ch_sup:start_link().

stop(_State) ->
ok.

A library application, which can not be started or stopped, does not need any application callback module.<!----><!---->

7.3 Application Resource File

To define an application, we create an application specification which is put in an application resource file, or in short .app file:

{application, Application, [Opt1,...,OptN]}.

Application, an atom, is the name of the application. The file must be named Application.app.
Application, 一个原子, 是此应用的名称. 文件名必须为Application.app.

Each Opt is a tuple {Key, Value} which define a certain property of the application. All keys are optional. Default values are used for any omitted keys.

The contents of a minimal .app file for a library application libapp looks like this:

{application, libapp, []}.

The contents of a minimal .app file ch_app.app for a supervision tree application like ch_app looks like this:

{application, ch_app,
[{mod, {ch_app,[]}}]}.

The key mod defines the callback module and start argument of the application, in this case ch_app and [], respectively. This means that
mod 键定义了应用的回调模块和start 参数,  在这儿分别是ch_app 和 []。它等同于

代码
  1. ch_app:start(normal, [])  

will be called when the application should be started and
在应用启动时被调用且

代码
  1. ch_app:stop([])  


will be called when the application has been stopped.
在应用停止时被调用.

When using systools, the Erlang/OTP tools for packaging code (see Releases), the keys description, vsn, modules, registered and applications should also be specified:

{application, ch_app,
[{description, "Channel allocator"},
{vsn, "1"},
{modules, [ch_app, ch_sup, ch3]},
{registered, [ch3]},
{applications, [kernel, stdlib, sasl]},
{mod, {ch_app,[]}}
]}.

description
A short description, a string. Defaults to "".
vsn
Version number, a string. Defaults to "".
modules
All modules introduced by this application. systools uses this list when generating boot scripts and tar files. A module must be defined in one and only one application. Defaults to [].
registered
All names of registered processes in the application. systools uses this list to detect name clashes between applications. Defaults to [].
applications
All applications which must be started before this application is started. systools uses this list to generate correct boot scripts. Defaults to [], but note that all applications have dependencies to at least kernel and stdlib.

The syntax and contents of of the application resource file are described in detail in app(4).<!----><!---->

7.4 Directory Structure

When packaging code using systools, the code for each application is placed in a separate directory lib/Application-Vsn, where Vsn is the version number.

This may be useful to know, even if systools 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 code(3)) will automatically use code from the directory with the highest version number, if there are more than one version of an application present.

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.

The application directory have the following sub-directories:

  • src
  • ebin
  • priv
  • include

src
Contains the Erlang source code.
ebin
Contains the Erlang object code, the beam files. The .app file is also placed here.
priv
Used for application specific files. For example, C executables are placed here. The function code:priv_dir/1 should be used to access this directory.
include
Used for include files.
<!----><!---->

7.5 Application Controller

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 application controller process, registered as application_controller.

All operations on applications are coordinated by the application controller. It is interfaced through the functions in the module application, see application(3). In particular, applications can be loaded, unloaded, started and stopped.<!---->

7.6 Loading and Unloading Applications

Before an application can be started, it must be loaded. The application controller reads and stores the information from the .app file.

1> application:load(ch_app).
ok
2> application:loaded_applications().
[{kernel,"ERTS CXC 138 10","2.8.1.3"},
{stdlib,"ERTS CXC 138 10","1.11.4.3"},
{ch_app,"Channel allocator","1"}]

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.

3> application:unload(ch_app).
ok
4> application:loaded_applications().
[{kernel,"ERTS CXC 138 10","2.8.1.3"},
{stdlib,"ERTS CXC 138 10","1.11.4.3"}]

Note!

Loading/unloading an application does not load/unload the code used by the application. Code loading is done the usual way.

<!----><!---->

7.7 Starting and Stopping Applications

An application is started by calling:

5> application:start(ch_app).
ok
6> application:which_applications().
[{kernel,"ERTS CXC 138 10","2.8.1.3"},
{stdlib,"ERTS CXC 138 10","1.11.4.3"},
{ch_app,"Channel allocator","1"}]

If the application is not already loaded, the application controller will first load it using application:load/1. It will check the value of the applications key, to ensure that all applications that should be started before this application are running.<!---->

The application controller then creates an application master 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 start/2 in the module, and with the start argument, defined by the mod key in the .app file.

An application is stopped, but not unloaded, by calling:

7> application:stop(ch_app).
ok

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 stop/1 in the module defined by the mod key.<!---->

7.8 Configuring an Application

An application can be configured using configuration parameters. These are a list of {Par, Val} tuples specified by a key env in the .app file.

{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"}]}
]}.

Par should be an atom, Val is any term. The application can retrieve the value of a configuration parameter by calling application:get_env(App, Par) or a number of similar functions, see application(3).

Example:

% erl
Erlang (BEAM) emulator version 5.2.3.6 [hipe] [threads:0]

Eshell V5.2.3.6 (abort with ^G)
1> application:start(ch_app).
ok
2> application:get_env(ch_app, file).
{ok,"/usr/local/log"}

The values in the .app file can be overridden by values in a system configuration file. This is a file which contains configuration parameters for relevant applications:

[{Application1, [{Par11,Val11},...]},
...,
{ApplicationN, [{ParN1,ValN1},...]}].

The system configuration should be called Name.config and Erlang should be started with the command line argument -config Name. See config(4) for more information.

Example: A file test.config is created with the following contents:

[{ch_app, [{file, "testlog"}]}].

The value of file will override the value of file as defined in the .app file:

% erl -config test
Erlang (BEAM) emulator version 5.2.3.6 [hipe] [threads:0]

Eshell V5.2.3.6 (abort with ^G)
1> application:start(ch_app).
ok
2> application:get_env(ch_app, file).
{ok,"testlog"}

If release handling is used, exactly one system configuration file should be used and that file should be called sys.config

The values in the .app file, as well as the values in a system configuration file, can be overridden directly from the command line:

% erl -ApplName Par1 Val1 ... ParN ValN

Example:

% erl -ch_app file '"testlog"'
Erlang (BEAM) emulator version 5.2.3.6 [hipe] [threads:0]

Eshell V5.2.3.6 (abort with ^G)
1> application:start(ch_app).
ok
2> application:get_env(ch_app, file).
{ok,"testlog"}

<!---->

7.9 Application Start Types

A start type is defined when starting the application:

application:start(Application, Type)

application:start(Application) is the same as calling application:start(Application, temporary). The type can also be permanent or transient:

  • If a permanent application terminates, all other applications and the runtime system are also terminated.
  • If a transient application terminates with reason normal, this is reported but no other applications are terminated. If a transient application terminates abnormally, that is with any other reason than normal, all other applications and the runtime system are also terminated.
  • If a temporary application terminates, this is reported but no other applications are terminated.

It is always possible to stop an application explicitly by calling application:stop/1. Regardless of the mode, no other applications will be affected.

Note that transient mode is of little practical use, since when a supervision tree terminates, the reason is set to shutdown, not normal.

分享到:
评论

相关推荐

    Erlang OTP设计原理文档 中文版本

    OTP设计原则着重于实现高度并发、容错性和高效能。下面将详细讨论Erlang OTP中的关键概念和相关知识点。 1. 监督树(Supervision Tree): 监督树是OTP设计的核心概念,它是一种组织Erlang进程的方式。每个节点都...

    图书:Erlang和OTP实战

    OTP是基于Erlang的一套设计原则和库,它提供了标准的组件和模式,如GenServer、GenEvent和Supervisor等,用于构建可靠的分布式系统。OTP的核心理念是模块化、可复用和容错性,通过这些组件,开发者可以快速构建出...

    awesome-otp-learning:一系列了解OTP内部知识的学习材料

    OTP提供了一系列的设计原则、库和工具,使得开发者可以构建出类似于电信系统那样复杂而稳定的软件。Elixir语言虽然不是OTP的原生语言,但它基于Erlang VM(BEAM),因此也能充分利用OTP的优势。 在这个"awesome-otp...

    otp-OTP-20.0.tar.gz

    为了做到这一点,他们需要熟悉Erlang编程语言,理解 OTP的设计原则,包括进程间的通信、错误处理和容错机制。同时,了解RabbitMQ的工作原理和配置也是必要的,以便有效地集成和利用其消息传递功能。 总结来说,"otp...

    Erlang OTP 设计原理 - 中文版

    在OTP设计中,应用之间存在依赖关系,可以指定被包含的应用。 发布(Release)是将应用打包在一起的形式,它包括了运行系统所需的所有文件。发布过程涉及生成启动脚本、创建发布包以及安装和更新发布。发布是分布式...

    otp-win32_20.1 32位

    了解Erlang的基本语法、并发编程模型和OTP的设计原则,将有助于构建高效且可靠的系统。 10. **Erlang生态系统**:Erlang生态系统中有许多开源项目,如 Cowboy(轻量级Web服务器),Phoenix(Elixir语言下的Web框架...

    erlang_otp_win64_25.0

    2. **OTP**:Open Telecom Platform,开放电信平台,是一套库、设计原则和开发工具的集合,旨在简化使用Erlang开发大型、高可用性的分布式系统的过程。它包括了进程管理、错误恢复、分布式计算、监控工具等核心组件...

    otp_src_25.0.2.tar.gz

    2. **OTP设计原则**:OTP提供了一套设计原则和库,如行为(Behaviours)、应用(Applications)、发布和升级机制,以及用于实现容错和高可用性的模块。这些原则鼓励模块化、可重用性和健壮性。 3. **进程模型**:...

    Inside_Erlang_OTP

    压缩包中的`Inside_Erlang_OTP.pptx`可能是一个详细的幻灯片教程,涵盖了Erlang OTP的各个方面,包括语言基础、 OTP设计原则以及如何利用OTP库来构建实际系统。`更多erlang资料下载.txt`可能是指向更多Erlang学习...

    Erlang_OTP_设计原理 中文版

    Erlang的这种设计哲学在Erlang/OTP设计原理中得到了充分的体现。接下来我将详细解释文档中提到的相关知识点。 1. 监督树(Supervision Trees):监督树是Erlang/OTP的核心概念之一,它是一种分层的组织结构,用于...

    otp_win64_20.3

    OTP包含了一系列的库、设计原则以及开发工具,使得开发者能够更容易地创建健壮且可靠的应用程序。在RabbitMQ的上下文中,OTP为RabbitMQ提供了基础的运行环境,因为RabbitMQ本身就是用Erlang编写的。 **RabbitMQ** ...

    Erlang otp_win64_21.2.exe

    1. **并发性**:Erlang OTP设计了一个轻量级的进程模型,每个进程都有自己的内存空间,这使得进程间的通信快速且隔离,能处理大量并发任务。 2. **分布式**:Erlang OTP支持跨节点通信,允许在不同机器上运行的...

    erlang安装包otp_win64_23.1

    1. **Erlang OTP**:OTP是Erlang的核心部分,提供了一组设计原则、库和工具,用于构建大规模、高可用性的分布式系统。OTP包含以下组件: - **BEAM虚拟机**:Erlang的执行环境,能够高效地处理并发和错误恢复。 - *...

    elixir-mix-otp-guide:构建完整的Elixir应用程序的官方指南的源代码,带有自己的监视树,配置,测试等,网址为

    OTP是Elixir和Erlang中的关键概念,它提供了一组设计原则和库,帮助开发者构建容错、自愈的系统。主要组成部分包括: 1. **进程(Processes)**:轻量级并发实体,它们之间通过消息传递进行通信。 2. **监督树...

    OTP Design Principles

    ### OTP设计原则详解 #### 概览 OTP(Open Telecom Platform)是Erlang/OTP框架的核心组成部分之一,它提供了一套成熟的、可扩展的、容错的应用程序设计模式。OTP设计原则指导开发者如何构建稳定可靠的分布式系统...

    otp_win64_24.0.rar

    5. **错误恢复和容错**:OTP提供了一系列的设计原则和库,如行为(Behaviours)和监督树(Supervision Trees),帮助开发者实现故障恢复和系统稳定性。 6. **Mnesia数据库**:Mnesia是Erlang OTP中的分布式数据库...

    (学成在线day05 RabbitMQ资料)Erlang/OTP20.3版本和RabbitMQ3.7.3

    2. **OTP**:OTP是Erlang的一个重要组成部分,它提供了一套标准库、开发工具和设计原则,帮助开发者构建可伸缩、可靠且容错的系统。 OTP包括一系列的库应用、行为模式(如GenServer、GenEvent等)以及用于监控和管理...

    otp_src_17.0.tar.gz

    OTP(Open Telecommunications Platform,开放电信平台)是Erlang编程语言的核心部分,它提供了一组库、设计原则和工具,用于构建高度可扩展、容错性和高效能的分布式系统。"otp_src_17.0.tar.gz" 是 OTP 的源代码包...

    otp_win64_24.0.zip

    OTP(Open Telecom Platform)是Erlang的核心组件,提供了一整套库、设计原则和工具,旨在帮助开发者构建高效、可靠的系统。 标题"otp_win64_24.0.zip"指的是OTP的Windows 64位版本的24.0发行版的压缩文件。这个...

Global site tag (gtag.js) - Google Analytics