[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 ...
随机推荐
- django admin 中实现word文档下载
为了实现此功能,需要用到的知识点: (1)django admin 自定义字段: 参考:http://www.cnblogs.com/wumingxiaoyao/p/6928297.html ...
- ListView 中 的 分页
Django Pagination 简单分页 当博客上发布的文章越来越多时,通常需要进行分页显示,以免所有的文章都堆积在一个页面,影响用户体验.Django 内置的 Pagination 能够帮助我 ...
- JSP页面中的时间显示问题
在JSP页面中往往要将时间显示为指定格式的,如果传入的是Date类型很好解决 <fmt:formatDate value="${orderTime}" pattern=&qu ...
- ajax传递给后台数组参数方式
出自:http://blog.csdn.net/lingxyd_0/article/details/10428785 在项目上用到了批量删除与批量更改状态,前台使用了EasyUI的DataGrid,用 ...
- 自动删除 Elasticsearch 索引
#!/bin/bash # author: Wang XiaoQiang# crontab -e# 0 0 * * * /root/script/del_esindex.sh # auto delet ...
- shell脚本里切换用户执行命令的注意事项
功能说明:用root用户执行一个脚本,脚本里需要切换到普通用户DT去执行其他命令,其中就用到了EOF,用法如下: #!/bin/bash su - DT<<EOF cd apache-to ...
- freeswitch由于ext-sip-ip地址填写错误导致32秒拆线问题
通话32秒左右就断掉 检查 profile 的 ext-sip-ip 设置ext-rtp-ip和ext-sip-ip 可以直接设置为外网IP 自建stun-server, 更新后, 过了好几个小时出现 ...
- linux:alias
linux系统下常用一个“命令”ll,它实质上是一个别名,而非命令. 我们用它的前提是,在~/.bashrc文件里打开,默认有条记录: #alias ll=’ls -l’ 这就是别名的格式.把注释去掉 ...
- 【sdut2878】Circle
题目链接http://acm.sdut.edu.cn/onlinejudge2/index.php/Home/Index/problemdetail/pid/2878.html 题意 n个结点编号为0 ...
- 详细探究Spark的shuffle实现
Background 在MapReduce框架中,shuffle是连接Map和Reduce之间的桥梁,Map的输出要用到Reduce中必须经过shuffle这个环 节,shuffle的性能高低直接影响 ...