- 浏览: 571894 次
- 性别:
- 来自: 广州杭州
文章分类
最新评论
-
bohc:
谢谢,搞了两天了,现在才算是找到问题所在,解决了。
文件在使用FileChannel.map后不能被删除(Windows上) -
zhang0000jun:
在jdk1.8中执行正好和楼主的结果相反,请指教
从Java视角理解CPU缓存(CPU Cache) -
在世界的中心呼喚愛:
forenroll 写道请问楼主的那个分析工具cachemis ...
从Java视角理解CPU缓存(CPU Cache) -
xgj1988:
我这里打出的结果是: 0 L1-dcache-load-mis ...
从Java视角理解CPU缓存(CPU Cache) -
thebye85:
请教下大神,为什么频繁的park会导致大量context sw ...
从Java视角理解CPU上下文切换(Context Switch)
2 Gen_Server Behaviour
This chapter should be read in conjunction with gen_server(3)
, where all interface functions and callback functions are described in detail.
2.1 Client-Server Principles
The client-server model is characterized by a central server and an arbitrary number of clients. The client-server model is generally used for resource management operations, where several different clients want to share a common resource. The server is responsible for managing this resource.
Client-Server Model 客户-服务器模型
2.2 Example
An example of a simple server written in plain Erlang was given in Overview. The server can be re-implemented using gen_server
, resulting in this callback module:
java 代码
- -module(ch3).
- -behaviour(gen_server).
- -export([start_link/0]).
- -export([alloc/0, free/1]).
- -export([init/1, handle_call/3, handle_cast/2]).
- start_link() ->
- gen_server:start_link({local, ch3}, ch3, [], []).
- alloc() ->
- gen_server:call(ch3, alloc).
- free(Ch) ->
- gen_server:cast(ch3, {free, Ch}).
- init(_Args) ->
- {ok, channels()}.
- handle_call(alloc, _From, Chs) ->
- {Ch, Chs2} = alloc(Chs),
- {reply, Ch, Chs2}.
- handle_cast({free, Ch}, Chs) ->
- Chs2 = free(Ch, Chs),
- {noreply, Chs2}.
The code is explained in the next sections.
2.3 Starting a Gen_Server
In the example in the previous section, the gen_server is started by calling ch3:start_link()
:
java 代码
- start_link() ->
- gen_server:start_link({local, ch3}, ch3, [], []) => {ok, Pid}
start_link
calls the function gen_server:start_link/4
. This function spawns and links to a new process, a gen_server.
- The first argument
{local, ch3}
specifies the name. In this case, the gen_server will be locally registered asch3
.
If the name is omitted, the gen_server is not registered. Instead its pid must be used. The name could also be given as{global, Name}
, in which case the gen_server is registered usingglobal:register_name/2
.
- The second argument,
ch3
, is the name of the callback module, that is the module where the callback functions are located.
In this case, the interface functions (start_link
,alloc
andfree
) are located in the same module as the callback functions (init
,handle_call
andhandle_cast
). This is normally good programming practice, to have the code corresponding to one process contained in one module.
- The third argument, [], is a term which is passed as-is to the callback function
init
. Here,init
does not need any indata and ignores the argument.
- The fourth argument, [], is a list of options. See
gen_server(3)
for available options.
If name registration succeeds, the new gen_server process calls the callback function ch3:init([])
. init
is expected to return {ok, State}
, where State
is the internal state of the gen_server. In this case, the state is the available channels.
java 代码
- init(_Args) ->
- {ok, channels()}.
Note that gen_server:start_link
is synchronous. It does not return until the gen_server has been initialized and is ready to receive requests.
gen_server:start_link
must be used if the gen_server is part of a supervision tree, i.e. is started by a supervisor. There is another function gen_server:start
to start a stand-alone gen_server, i.e. a gen_server which is not part of a supervision tree.
2.4 Synchronous Requests - Call
The synchronous request alloc()
is implemented using gen_server:call/2
:
java 代码
- alloc() ->
- gen_server:call(ch3, alloc).
ch3
is the name of the gen_server and must agree with the name used to start it. alloc
is the actual request.
The request is made into a message and sent to the gen_server. When the request is received, the gen_server calls handle_call(Request, From, State)
which is expected to return a tuple {reply, Reply, State1}
. Reply
is the reply which should be sent back to the client, and State1
is a new value for the state of the gen_server.
java 代码
- handle_call(alloc, _From, Chs) ->
- {Ch, Chs2} = alloc(Chs),
- {reply, Ch, Chs2}.
In this case, the reply is the allocated channel Ch
and the new state is the set of remaining available channels Chs2
.
Thus, the call ch3:alloc()
returns the allocated channel Ch
and the gen_server then waits for new requests, now with an updated list of available channels.
2.5 Asynchronous Requests - Cast
The asynchronous request free(Ch)
is implemented using gen_server:cast/2
:
java 代码
- free(Ch) ->
- gen_server:cast(ch3, {free, Ch}).
ch3
is the name of the gen_server. {free, Ch}
is the actual request.
The request is made into a message and sent to the gen_server. cast
, and thus free
, then returns ok
.
When the request is received, the gen_server calls handle_cast(Request, State)
which is expected to return a tuple {noreply, State1}
. State1
is a new value for the state of the gen_server.
java 代码
- handle_cast({free, Ch}, Chs) ->
- Chs2 = free(Ch, Chs),
- {noreply, Chs2}.
In this case, the new state is the updated list of available channels Chs2
. The gen_server is now ready for new requests.
2.6 Stopping
2.6.1 In a Supervision Tree
If the gen_server is part of a supervision tree, no stop function is needed. The gen_server will automatically be terminated by its supervisor. Exactly how this is done is defined by a shutdown strategy set in the supervisor.
If it is necessary to clean up before termination, the shutdown strategy must be a timeout value and the gen_server must be set to trap exit signals in the init
function. When ordered to shutdown, the gen_server will then call the callback function terminate(shutdown, State)
:
java 代码
- init(Args) ->
- ...,
- process_flag(trap_exit, true),
- ...,
- {ok, State}.
- ...
- terminate(shutdown, State) ->
- ..code for cleaning up here..
- ok.
2.6.2 Stand-Alone Gen_Servers
If the gen_server is not part of a supervision tree, a stop function may be useful, for example:
java 代码
- ...
- export([stop/0]).
- ...
- stop() ->
- gen_server:cast(ch3, stop).
- ...
- handle_cast(stop, State) ->
- {stop, normal, State};
- handle_cast({free, Ch}, State) ->
- ....
- ...
- terminate(normal, State) ->
- ok.
The callback function handling the stop
request returns a tuple {stop, normal, State1}
, where normal
specifies that it is a normal termination and State1
is a new value for the state of the gen_server. This will cause the gen_server to call terminate(normal,State1)
and then terminate gracefully.
2.7 Handling Other Messages
If the gen_server should be able to receive other messages than requests, the callback function handle_info(Info, State)
must be implemented to handle them. Examples of other messages are exit messages, if the gen_server is linked to other processes (than the supervisor) and trapping exit signals.
java 代码
- handle_info({'EXIT', Pid, Reason}, State) ->
- ..code to handle exits here..
- {noreply, State1}.
发表评论
-
ubuntu安装otp R11B 的一些记录
2007-11-16 12:30 2779新的ubuntu系统会缺少一些工具 和lib. 用apt-ge ... -
emulator调试日志: driver篇
2007-10-08 16:35 2273--------- driver篇 ------------- ... -
修正Programming Erlang中linked driver实例的小问题
2007-10-08 14:50 2445也许很多人碰上过, 用example1_lid:sta ... -
emulator调试日志: port篇
2007-10-06 16:14 2370------------------ port 篇 ----- ... -
supervisor一小技巧
2007-09-04 13:20 1776simple_one_for_one可以让supervisor ... -
gen_server
2007-08-29 21:52 1898State用来存数据, 任何erlang term都行 ge ... -
application
2007-08-29 02:01 1715用pman 可以看出application controlle ... -
epmd源码学习
2007-07-26 10:14 1998注: 此处节点是指分布式中分布在各终端的点, 而结点是指存在数 ... -
Tracing和dbg
2007-07-15 21:49 2531代码不必用特殊的标记(比如debug_info)来编译,也可以 ... -
ets,dets与大数据存储
2007-07-15 12:49 4930ets与dets都是用来存大数据的机制 ets是Erl ... -
用telnet来与ejabberd交互
2007-07-11 15:41 3195看了一篇文章,觉得用telnet来调试ejabberd也是一种 ... -
ejabberd管理页面和客户端
2007-07-11 00:23 9719转战到97机器。在ejabber.config加上这么一行. ... -
ejabberd在linux平台的安装与配置
2007-07-05 21:17 11919这些天捣鼓了下ejabberd,准备研究它的代码,做为榜样~ ... -
mnesia相关笔记
2007-06-29 12:17 2310当前版本OTP 5.5的mensia建表的表名可以和记录名不一 ... -
OTP设计原则:应用
2007-06-27 00:32 19047 Applications This chapter sh ... -
erlang网络编程的几个性能调优和注意点
2007-06-26 09:56 17846前些天给echo_server写了 ... -
erlc
2007-06-24 15:08 3798erlc 命令 erlc 概要 编译器 描述 Th ... -
echo_server
2007-06-23 14:45 2434代码 -module(echo_server ... -
OTP设计原则:Supervisor行为
2007-06-22 12:15 27035 Supervisor Behaviour This s ... -
OTP设计原则:Gen_Event 行为
2007-06-22 11:59 19844 Gen_Event 行为 这一章应该与gen_event ...
相关推荐
otp_win64_24.1.7.exe
OTP 20(及更高版本)中gen_statem的惯用Elixir包装器。 完整文档可。 您可以在在Hex上找到该软件包。 gen_statem和此包装器之间的一个重要区别是,您在此包装器use GenStateMachine回调模式声明为use ...
Flutter软件包,将帮助您生成具有精美设计和动画的PIN码字段。 可用于OTP或Pin码输入 :nerd_face: :nerd_face: 特征 :green_heart: 自动将下一个字段集中在键入上,将上一个字段集中在删除上 游标支持 :high_...
erl最新版本,erl 23版本 和24版本 属于大版本更新了,有些23版本高性能模块已经不适用24版本,我博客有说明,需要的可以去主页看下。
otp_win64_24.3.exe
Erlang是一种通用的面向并发的编程语言,它有瑞典电信设备制造商爱立信所辖的CS-Lab开发, 目的是创造一种可以应对大规模并发活动的编程语言和运行环境。
Erlang/OTP
otp_win64_20.3.exe百度网盘下载(官网下载实在是太慢了),亲测有效!! otp_win64_20.3.exe otp_win64_20.3.exe otp_win64_20.3.exe
Generate & Verify OTP Server
Erlang/OTP
react-native-otp-fieldReact本机OTP字段 :keycap_1: :keycap_2: :keycap_3: :keycap_4: :keycap_5:安装npm i react-native-otp-fieldRN <0.63 npm i react-native-otp-field@0.0.7一个可在android和iOS上运行的...
otp_win64_26.0安装包
DX0906VX 显示屏的OTP烧录程序,用来在出厂时设置屏幕的对比度。
我主页还有相关的rabbitmq-server-3.8.5.exe可以下载,地址https://download.csdn.net/download/qq_39403734/12570527,如果这两个在安装的过程中有什么不懂得可以私聊我,或者我有空的时候出个博客给大家。
erlang_otp_win64_25.0
官网下载超级慢,公司网络也有点封锁,这是我用流量下载的,有需要的可以下载,这是适合Centos的版本21.1
可能这个实现代码量相对没那么少,但是这样是比较OTP的做法。 ##Installation 在你的rebar.config添加: {gs_tcp, ".*", {git, "https://github.com/roowe/gs_tcp", "master"}} 之后执行 rebar get-deps接着 rebar ...
自己自学RabbitMQ整理出来的资料,windows环境下的erlang和RabbitMQ安装包,版本分别是otp_22.3_for_windows_64和rabbitmq-server-3.8.5(保证不会出现版本冲突) 在官网上下载简直是龟速,所以分享给大家便于下载,...
rabbitmq-server3.7版本和otp_win64安装包,下载解压后,先安装otp_win64.exe 在安装rabbitmq-server ,具体安装步骤可自行百度
otp_win64_20.2.exe rabbitmq erlang,rabbitmq安装必备之erlang