- 浏览: 578015 次
- 性别:
- 来自: 广州杭州
文章分类
最新评论
-
bohc:
谢谢,搞了两天了,现在才算是找到问题所在,解决了。
文件在使用FileChannel.map后不能被删除(Windows上) -
zhang0000jun:
在jdk1.8中执行正好和楼主的结果相反,请指教
从Java视角理解CPU缓存(CPU Cache) -
在世界的中心呼喚愛:
forenroll 写道请问楼主的那个分析工具cachemis ...
从Java视角理解CPU缓存(CPU Cache) -
xgj1988:
我这里打出的结果是: 0 L1-dcache-load-mis ...
从Java视角理解CPU缓存(CPU Cache) -
thebye85:
请教下大神,为什么频繁的park会导致大量context sw ...
从Java视角理解CPU上下文切换(Context Switch)
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
和 []。它等同于
代码
- ch_app:start(normal, [])
will be called when the application should be started and
在应用启动时被调用且
代码
- 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
vsn
modules
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
systools
uses this list to detect name clashes between applications. Defaults to []. applications
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
ebin
beam
files. The .app
file is also placed here. priv
code:priv_dir/1
should be used to access this directory. include
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"}]
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 thannormal
, 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
.
发表评论
-
ubuntu安装otp R11B 的一些记录
2007-11-16 12:30 2837新的ubuntu系统会缺少一些工具 和lib. 用apt-ge ... -
emulator调试日志: driver篇
2007-10-08 16:35 2333--------- driver篇 ------------- ... -
修正Programming Erlang中linked driver实例的小问题
2007-10-08 14:50 2494也许很多人碰上过, 用example1_lid:sta ... -
emulator调试日志: port篇
2007-10-06 16:14 2420------------------ port 篇 ----- ... -
supervisor一小技巧
2007-09-04 13:20 1864simple_one_for_one可以让supervisor ... -
gen_server
2007-08-29 21:52 1952State用来存数据, 任何erlang term都行 ge ... -
application
2007-08-29 02:01 1786用pman 可以看出application controlle ... -
epmd源码学习
2007-07-26 10:14 2056注: 此处节点是指分布式中分布在各终端的点, 而结点是指存在数 ... -
Tracing和dbg
2007-07-15 21:49 2582代码不必用特殊的标记(比如debug_info)来编译,也可以 ... -
ets,dets与大数据存储
2007-07-15 12:49 4984ets与dets都是用来存大数据的机制 ets是Erl ... -
用telnet来与ejabberd交互
2007-07-11 15:41 3253看了一篇文章,觉得用telnet来调试ejabberd也是一种 ... -
ejabberd管理页面和客户端
2007-07-11 00:23 9793转战到97机器。在ejabber.config加上这么一行. ... -
ejabberd在linux平台的安装与配置
2007-07-05 21:17 11977这些天捣鼓了下ejabberd,准备研究它的代码,做为榜样~ ... -
mnesia相关笔记
2007-06-29 12:17 2369当前版本OTP 5.5的mensia建表的表名可以和记录名不一 ... -
erlang网络编程的几个性能调优和注意点
2007-06-26 09:56 17894前些天给echo_server写了 ... -
erlc
2007-06-24 15:08 3879erlc 命令 erlc 概要 编译器 描述 Th ... -
echo_server
2007-06-23 14:45 2475代码 -module(echo_server ... -
OTP设计原则:Supervisor行为
2007-06-22 12:15 27665 Supervisor Behaviour This s ... -
OTP设计原则:Gen_Event 行为
2007-06-22 11:59 20474 Gen_Event 行为 这一章应该与gen_event ... -
OTP设计原则:Gen_Fsm 行为
2007-06-22 11:56 27783 Gen_Fsm 行为 This chapter shou ...
相关推荐
OTP设计原则着重于实现高度并发、容错性和高效能。下面将详细讨论Erlang OTP中的关键概念和相关知识点。 1. 监督树(Supervision Tree): 监督树是OTP设计的核心概念,它是一种组织Erlang进程的方式。每个节点都...
OTP是基于Erlang的一套设计原则和库,它提供了标准的组件和模式,如GenServer、GenEvent和Supervisor等,用于构建可靠的分布式系统。OTP的核心理念是模块化、可复用和容错性,通过这些组件,开发者可以快速构建出...
OTP提供了一系列的设计原则、库和工具,使得开发者可以构建出类似于电信系统那样复杂而稳定的软件。Elixir语言虽然不是OTP的原生语言,但它基于Erlang VM(BEAM),因此也能充分利用OTP的优势。 在这个"awesome-otp...
为了做到这一点,他们需要熟悉Erlang编程语言,理解 OTP的设计原则,包括进程间的通信、错误处理和容错机制。同时,了解RabbitMQ的工作原理和配置也是必要的,以便有效地集成和利用其消息传递功能。 总结来说,"otp...
在OTP设计中,应用之间存在依赖关系,可以指定被包含的应用。 发布(Release)是将应用打包在一起的形式,它包括了运行系统所需的所有文件。发布过程涉及生成启动脚本、创建发布包以及安装和更新发布。发布是分布式...
了解Erlang的基本语法、并发编程模型和OTP的设计原则,将有助于构建高效且可靠的系统。 10. **Erlang生态系统**:Erlang生态系统中有许多开源项目,如 Cowboy(轻量级Web服务器),Phoenix(Elixir语言下的Web框架...
2. **OTP**:Open Telecom Platform,开放电信平台,是一套库、设计原则和开发工具的集合,旨在简化使用Erlang开发大型、高可用性的分布式系统的过程。它包括了进程管理、错误恢复、分布式计算、监控工具等核心组件...
2. **OTP设计原则**:OTP提供了一套设计原则和库,如行为(Behaviours)、应用(Applications)、发布和升级机制,以及用于实现容错和高可用性的模块。这些原则鼓励模块化、可重用性和健壮性。 3. **进程模型**:...
1. **并发性**:Erlang OTP设计了一个轻量级的进程模型,每个进程都有自己的内存空间,这使得进程间的通信快速且隔离,能处理大量并发任务。 2. **分布式**:Erlang OTP支持跨节点通信,允许在不同机器上运行的...
压缩包中的`Inside_Erlang_OTP.pptx`可能是一个详细的幻灯片教程,涵盖了Erlang OTP的各个方面,包括语言基础、 OTP设计原则以及如何利用OTP库来构建实际系统。`更多erlang资料下载.txt`可能是指向更多Erlang学习...
Erlang的这种设计哲学在Erlang/OTP设计原理中得到了充分的体现。接下来我将详细解释文档中提到的相关知识点。 1. 监督树(Supervision Trees):监督树是Erlang/OTP的核心概念之一,它是一种分层的组织结构,用于...
OTP包含了一系列的库、设计原则以及开发工具,使得开发者能够更容易地创建健壮且可靠的应用程序。在RabbitMQ的上下文中,OTP为RabbitMQ提供了基础的运行环境,因为RabbitMQ本身就是用Erlang编写的。 **RabbitMQ** ...
1. **Erlang OTP**:OTP是Erlang的核心部分,提供了一组设计原则、库和工具,用于构建大规模、高可用性的分布式系统。OTP包含以下组件: - **BEAM虚拟机**:Erlang的执行环境,能够高效地处理并发和错误恢复。 - *...
OTP是Elixir和Erlang中的关键概念,它提供了一组设计原则和库,帮助开发者构建容错、自愈的系统。主要组成部分包括: 1. **进程(Processes)**:轻量级并发实体,它们之间通过消息传递进行通信。 2. **监督树...
### OTP设计原则详解 #### 概览 OTP(Open Telecom Platform)是Erlang/OTP框架的核心组成部分之一,它提供了一套成熟的、可扩展的、容错的应用程序设计模式。OTP设计原则指导开发者如何构建稳定可靠的分布式系统...
5. **错误恢复和容错**:OTP提供了一系列的设计原则和库,如行为(Behaviours)和监督树(Supervision Trees),帮助开发者实现故障恢复和系统稳定性。 6. **Mnesia数据库**:Mnesia是Erlang OTP中的分布式数据库...
2. **OTP**:OTP是Erlang的一个重要组成部分,它提供了一套标准库、开发工具和设计原则,帮助开发者构建可伸缩、可靠且容错的系统。 OTP包括一系列的库应用、行为模式(如GenServer、GenEvent等)以及用于监控和管理...
OTP(Open Telecommunications Platform,开放电信平台)是Erlang编程语言的核心部分,它提供了一组库、设计原则和工具,用于构建高度可扩展、容错性和高效能的分布式系统。"otp_src_17.0.tar.gz" 是 OTP 的源代码包...
OTP是Ericsson公司开发的一个用于构建高可用性、分布式系统的重要框架,它包括了Erlang虚拟机(VM)、库函数、设计原则以及工具。Erlang是一种并发、容错、面向进程的编程语言,特别适合构建大规模、实时和容错的...