[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 ...
随机推荐
- [转] 实现winfrom进度条及进度信息提示,winfrom程序假死处理
china_xuhua 原文地址 1.方法一:使用线程 功能描述:在用c#做WinFrom开发的过程中.我们经常需要用到进度条(ProgressBar)用于显示进度信息.这时候我们可能就需要用到多线 ...
- leetcode671
class Solution { public: vector<int> V; void postTree(TreeNode* node) { if (node != NULL) { V. ...
- Mysql之数据库设计
一.三大范式 1.第一范式:消除一个字段包含多个数据库值,消除一个记录包含重复的组(单独的一列包含多个项目),即可满足1NF. 2.第二范式:消除部分依赖性即可转化为2NF.部分依赖性表示一个记录中包 ...
- delphi XE8 Android SDK SET
delphi XE Berlin Android SDK SET RAD>Tools>Options>SDk Manager> https://dl-ssl.google.co ...
- [Python Study Notes]pynput实现对鼠标控制
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ...
- EF CodeFirst简单实例
运行环境:VS2012,添加的EntityFramework为6.0.2 版本不用太关心,只要知道原理就行了: 基本代码就这几行: namespace ConsoleApplication1 { ...
- resultset 记录数
JDBC中的ResultSet API没有直接获取记录条数的方法,现介绍几个: 方法一:利用ResultSet的getRow方法来获得ResultSet的总行数 Java代码 ResultSet rs ...
- OpenGL位图函数
[OpenGL位图函数] 1.OpenGL中glBitmap用于绘制一个二值阵列. When drawn, the bitmap is positioned relative to the curre ...
- Python 生成MYSQL inser语句
背景: 一般来说,navicat生成的insert已经够用了 如果说一张表有2,30个字段,每个字段还得一一对上,其实是很难的.所以抽空写了个小程序.用它完全不用担心字段对不上了.因为没有时间,需要手 ...
- easyui图标大全
.icon-blank{ background:url('icons/blank.gif') no-repeat; } .icon-add{ background:url('icons/edit_ad ...