[Erlang08] 使用Erlang application有什么好处?
问题:
当我们把一个项目中所有的supervision tree通过一个简单的函数game: start(),会发现这个树结构特别复杂,只能有一个根节点,然后一直扩展。

那么有时,我们会需要:有些功能模块不启动,有些启动,如果再去改这颗树的结构,就会很麻烦。这里,这就是application出现的原因,设计一个可以随时开关的子块(application).比如:上图中的log app, db app ,game app, connect app ..
这样对这些应用的开关管理就非常方便啦,【试想你如果用supervisor,运行时还要手动去停进程树,然后还要移除监控树,还要做clean工作,下次启动还要做start工作…】,这些定义好application后,自然会把这个当成一个单元处理啦!这大概就是编程思想的体现吧。
如何构造一个典型的erlang application? 下面我们通过把[Erl_Question07] Erlang 做图形化编程的尝试:纯Erlang做2048游戏 的游戏改为application启动来做示范
原来的通过erl Script 启动是可以的,变成application有什么好处呢?
that can be started and stopped as a unit, and which can be re-used in other systems as well. 使用application方便随时只启动或关闭以application为单位的应用,其它application完全不受影响,这可以方便的管理一个大项目的各个功能,把这些功能都分成一个个小应用,又有层次又方便管理。
步骤:
1. 定义 .app文件,格式如下
%% game2048.app
{application, game2048, [
{description, "pure erlang game 2048 for fun"}, %%description应用说明,默认为""
{vsn, "1"}, %% 版本号
{modules, []},%%All modules introduced by this application,systools使用这个list来生成boot script and tar file tar,这个module必须只能被一个且只能是一个application定义过
{registerd,[]},%%All names of registered processes in the application. systools uses this list to detect name clashes between applications. Defaults to [].
{applications, [
kernel,
stdlib
]},%%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.
{mod, {game2048_app, []}},%% call game2048_app:start(normal, []) ,game2048_app:stop([])
{env, []}
]}.
2.给游戏加入监控进程:game2048_sup.erl
init([]) ->
RestartStrategy = one_for_one,
MaxRestarts = 1000,
MaxSecondsBetweenRestarts = 3600, SupFlags = {RestartStrategy, MaxRestarts, MaxSecondsBetweenRestarts}, Restart = permanent,
Shutdown = 2000,
Type = worker, AChild = {'game2048', {'game2048', start, []}, %%监控的是game2048:start()
Restart, Shutdown, Type, []}, {ok, {SupFlags, [AChild]}}.
3. 因原来的game2048: start()返回值改为{ok,PID}模式,这是supervisor规范要求的。
4. 重新编译代码,改造application工作完成。
你可以通过以下方式启动 application client.
1. 启动一个erlang shell :
erl -name test -pa "app所在目录" -pa "ebin目录"
>application:start(game2048).
>application: stop(game2048).
2.当然你可以把application和其它的application共同使用,【不久我会把lager application也用来这里面来,大材小用学习下优秀代码也好:)】
变成application,好开心,居然看到和kernel并在一起,是不是高级点【使用observer:start().查看:

Tip: 你使用 erl 启动一个Shell时是不会启动 net_kernel模块的,导致分布式出错,如果加上 –name 指定节点名就会启动啦。
[Erlang08] 使用Erlang application有什么好处?的更多相关文章
- Erlang application stop 调用死锁
Erlang application stop 调用死锁(金庆的专栏)在application行为模块的start()中启动bson应用,在stop()中停止bson,结果application:st ...
- [Erlang 0105] Erlang Resources 小站 2013年1月~6月资讯合集
很多事情要做,一件一件来; Erlang Resources 小站 2013年1月~6月资讯合集,方便检索. 小站地址: http://site.douban.com/204209/ ...
- Erlang环境用eclipse搭建
erlide插件eclipse开发erlang 一.相关资料 Erlang 的官方网站是http://www.erlang.org.其左侧的连接指出了我们可以从这里获取的资源. 其中, Downl ...
- Erlang 内存泄漏分析
随着项目越来越依赖Erlang,碰到的问题也随之增加.前段时间线上系统碰到内存高消耗问题,记录一下troubleshooting的分析过程.线上系统用的是Erlang R16B02版本. 问题描述 有 ...
- Erlang generic standard behaviours -- gen_server module
在分析完gen module (http://www.cnblogs.com/--00/p/4271386.html)之后,就可以开始进入gen_server 的主体module 了.gen_serv ...
- Getting Started with Erlang
Getting Started with Erlang Erlang is a great language that lets you build highly concurrent applica ...
- [Erlang30]Erlang shell是如何工作的?
一些关于Erlang启动进程的分析:希望你会喜欢. 原英文地址:http://ferd.ca/repl-a-bit-more-and-less-than-that.html 研究Erlang shel ...
- [译]rabbitmq 2.4 Multiple tenants: virtual hosts and separation
我对rabbitmq学习还不深入,这些翻译仅仅做资料保存,希望不要误导大家. With exchanges, bindings, and queues under your belt, you mig ...
- Flex性能调优相关的一些总结
1.Performace包含4点:(1)Latency反应时间(2)Scalability:可伸缩性(3)Reliablity:稳定性(4)Availability:可用性2.运行时生命周期:Flex ...
随机推荐
- [转] C#调用外部DLL
原文地址 每种编程语言调用DLL的方法都不尽相同,在此只对用C#调用DLL的方法进行介绍.首先,您需要了解什么是托管,什么是非托管.一般可以认为:非托管代码主要是基于win 32平台开发的DLL,ac ...
- halcon连续采集图像
dev_close_window()dev_update_window('off')create_bar_code_model ([], [], BarCodeHandle)dev_open_wind ...
- c++builder自定义控件
c++builder自定义控件 http://docwiki.embarcadero.com/CodeExamples/XE8/en/RegisterComponents_%28C%2B%2B%29 ...
- oracle、sqlserver、mysql常用函数对比[to_char、to_number、to_date]
Oracle --> MySQL to_char(sysdat ...
- HTML怎么让img 等比例缩放
在img标签里面只设置宽,不设置高,图片就会等比例缩放.
- C++异常处理(转)
引言 异常,让一个函数可以在发现自己无法处理的错误时抛出一个异常,希望它的调用者可以直接或者间接处理这个问题.而传统错误处理技术,检查到一个局部无法处理的问题时: 1.终止程序(例如atol,atoi ...
- Redis 非关系型数据库 ( Nosql )
简介: Redis 是一个开源的,高性能的 key-value 系统,可以用来缓存或存储数据. Redis 数据可以持久化,并且支持多种数据类型:字符串(string),列表(list),哈希(has ...
- CBCentralManager Class 的相关分析
Overview 总体概述 CBCentralManager objects are used to manage discovered or connected remote peripheral ...
- [iOS]UIScrollView左右拨动,第二页宽度只有一半问题
用UIScrollView动态加入新View,而这个View是Xib方式创建,如果设置view的frame,这个view的宽度却只有设置的一半,很奇怪.于是我只设置view的frame的x值,不设置整 ...
- plsql程序中循环语句的使用