[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 ...
随机推荐
- TCP粘包拆包场景
TCP编程底层都有粘包和拆包机制,因为我们在C/S这种传输模型下,以TCP协议传输的时候,在网络中的byte其实就像是河水,TCP就像一个搬运工,将这流水从一端转送到另一端,这时又分两种情况: 1)如 ...
- jQuery自动触发事件
转自:https://blog.csdn.net/CY_LH/article/details/78982218 常用模拟 有时候,需要通过模拟用户操作,来达到单击的效果.例如在用户进入页面后,就触发c ...
- 跟我学算法-Logistic回归
虽然Logistic回归叫回归,但是其实它是一个二分类或者多分类问题 这里的话我们使用信用诈骗的数据进行分析 第一步:导入数据,Amount的数值较大,后续将进行(-1,1)的归一化 data = p ...
- Spring Boot启动流程详解
注:本文转自http://zhaox.github.io/java/2016/03/22/spring-boot-start-flow 环境 本文基于Spring Boot版本1.3.3, 使用了sp ...
- 引用rtmp编译报错:rtmp.obj : error LNK2001: 无法解析的外部符号 __imp__timeGetTime@0
如题vs下引用librtmp的时候报错:rtmp.obj : error LNK2001: 无法解析的外部符号 __imp__timeGetTime@0 在link 里加入 winmm.lib 就可以 ...
- UNITY IMGUI
这几天研究OPENGL时,想找一个UI库来用,发现了IMGUI,到网上搜索评估中,突然发现它似乎和UNITY有关系. UNITY文档中提到过IMGUI,不知道是不一个东西,仔细看了下文档,果然是.原文 ...
- MySQL 主从同步失败,数据表修复
问题描述: 接到报警称一台 MySQL 从库同步失败.登录服务器查看错误日志信息如下: Last_Error: Error 'Incorrect key file for table './bfcc/ ...
- springboot 中使用thymeleaf
Spring Boot支持FreeMarker.Groovy.Thymeleaf和Mustache四种模板解析引擎,官方推荐使用Thymeleaf. spring-boot-starter-thyme ...
- com.mysql.jdbc.exceptions.jdbc4.CommunicationsException/com.atomikos.datasource.ResourceException异常解决
tomcat+mysql部署,每天早晨第一次访问web项目,出现mysql的连接timeout异常:com.mysql.jdbc.exceptions.jdbc4.CommunicationsExce ...
- java 蓝桥杯算法提高 _3K好数
nums[i][j] 存的是i位数的时候,首位数字是j的K好数的数目,i从1位开始的结果,去算2位时的结果,去算3位时的结果...最后得到l位的结果.K进制只是一个范围. import java.ut ...