问题:

当我们把一个项目中所有的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有什么好处?的更多相关文章

  1. Erlang application stop 调用死锁

    Erlang application stop 调用死锁(金庆的专栏)在application行为模块的start()中启动bson应用,在stop()中停止bson,结果application:st ...

  2. [Erlang 0105] Erlang Resources 小站 2013年1月~6月资讯合集

    很多事情要做,一件一件来; Erlang Resources 小站 2013年1月~6月资讯合集,方便检索.      小站地址: http://site.douban.com/204209/     ...

  3. Erlang环境用eclipse搭建

    erlide插件eclipse开发erlang   一.相关资料 Erlang 的官方网站是http://www.erlang.org.其左侧的连接指出了我们可以从这里获取的资源. 其中, Downl ...

  4. Erlang 内存泄漏分析

    随着项目越来越依赖Erlang,碰到的问题也随之增加.前段时间线上系统碰到内存高消耗问题,记录一下troubleshooting的分析过程.线上系统用的是Erlang R16B02版本. 问题描述 有 ...

  5. Erlang generic standard behaviours -- gen_server module

    在分析完gen module (http://www.cnblogs.com/--00/p/4271386.html)之后,就可以开始进入gen_server 的主体module 了.gen_serv ...

  6. Getting Started with Erlang

    Getting Started with Erlang Erlang is a great language that lets you build highly concurrent applica ...

  7. [Erlang30]Erlang shell是如何工作的?

    一些关于Erlang启动进程的分析:希望你会喜欢. 原英文地址:http://ferd.ca/repl-a-bit-more-and-less-than-that.html 研究Erlang shel ...

  8. [译]rabbitmq 2.4 Multiple tenants: virtual hosts and separation

    我对rabbitmq学习还不深入,这些翻译仅仅做资料保存,希望不要误导大家. With exchanges, bindings, and queues under your belt, you mig ...

  9. Flex性能调优相关的一些总结

    1.Performace包含4点:(1)Latency反应时间(2)Scalability:可伸缩性(3)Reliablity:稳定性(4)Availability:可用性2.运行时生命周期:Flex ...

随机推荐

  1. 关于jquery.noConflict()的学习记录

    今天无意中看到了jquery.noConfict()的实现方法 代码如下: var // Map over jQuery in case of overwrite _jQuery = window.j ...

  2. Dijkstra 调度场算法 Python实现 一

    调度场算法(Shunting Yard Algorithm)是一个用于将中缀表达式转换为后缀表达式的经典算法,由 Edsger Wybe Dijkstra 引入,因其操作类似于火车编组场而得名.  — ...

  3. tornado 自定义session (二)

    有了上一步的基础,我们将session改造成一个模块,这样我们就可以通过配置,来使用不同方式(redis,数据库等)的session. 添加一个新目录:TornadoSession conf.py是配 ...

  4. mysql 挑选列导入

    insert into boleht_development.`htprojects`(id,pname,`general`,imgsrc,whatwedo,howwedo,bp) select ci ...

  5. IDEA debug

    版权声明: 本文转自:https://blog.csdn.net/qq_27093465/article/details/64124330 1,rerun XXX,这个就是直接重新跑某个程序.2,这个 ...

  6. WOW研究资料收集

    1,模拟服务器:trinity core, sunwell core等 参考:逍遥魔兽 2,大芒果:通用网游框架,带了WOW的模拟模块 参考资料: 大芒果论坛http://www.mangoscn.c ...

  7. 【原】Coursera—Andrew Ng机器学习—Week 7 习题—支持向量机SVM

    [1] [2] Answer: B. 即 x1=3这条垂直线. [3] Answer: B 因为要尽可能小.对B,右侧红叉,有1/2 * 2  = 1 ≥ 1,左侧圆圈,有1/2 * -2  = -1 ...

  8. percona xtradb cluster test

    docker run --rm -ti -e CLUSTER_NAME=test -e MYSQL_ALLOW_EMPTY_PASSWORD=1 --entrypoint="bash&quo ...

  9. 【LA3211 训练指南】飞机调度 【2-sat】

    题意 有n嫁飞机需要着陆.每架飞机都可以选择“早着陆”和“晚着陆”两种方式之一,且必须选择一种.第i架飞机的早着陆时间为Ei,晚着陆时间为Li,不得在其他时间着陆.你的任务是为这些飞机安排着陆方式,使 ...

  10. jmeter 读取mysql数据库

    业务背景 当我们用jmeter进行压测,或者造数据的时候,我们可能希望每次请求的参数都是随机的.如果从一个文件里读取,很难达到要求.jmeter提供了一套读取数据库的组件,能满足部分要求.但性能不好, ...