About the Handler in gen_event

Ulf Wiger etxuwig@REDACTED
Fri Feb 21 11:35:59 CET 2003


On Fri, 21 Feb 2003, Suresh S wrote:

>Hi ,
>
>Thank u,
>
>here i have an handler which handles three events,
>but when i notify the event by
>
>gen_event:notify(EMgr, E) --->. M:handle(E, S)
>
>the match of the event 'E'  should be handled ,
>no error is noticed but Event is not handled ,
>what may be the reason.


If you're not doing so already, you should run your tests
with the SASL application active. You can do this either by
starting erlang with 'erl -boot start_sasl', or by calling
application:start(sasl) in the shell of an already running
system.

With SASL active, you should get error reports every time a
fault occurs in a handler. The default behaviour of
gen_event is to remove the faulty handler.

There is support for making a handler supervised. This will
make the gen_event server send a message to a specified
process whenever a handler is removed. The simplest pattern
for re-installing the handler is to let the supervising
process exit, be restarted by its own supervisor, and
install the handler upon restart. This way, we reuse the
logic for max restart frequency and reporting in
supervisor.erl.

An example of such a process follows:

-module(handler_supervisor).
-behaviour(gen_server).

-export([start_link/4]).

%% gen_server callbacks
-export([init/1, handle_info/2, handle_call/3,
         terminate/2, code_change/3]).

start_link(EventMgr, Name, Module, Args) ->
    gen_server:start_link({local, Name}, ?MODULE,
                          {EventMgr, Module, Args}, []).

init({EventMgr, Module, Args}) ->
    install(EventMgr, Module, Args),
    {ok, {EventMgr, Module}}.

handle_call(Req,_From,S) ->
    {stop, unknown_call, S}.

handle_info({gen_event_EXIT, Mod, Reason}, State) ->
    {stop, {gen_event_EXIT, Mod, Reason}, State}.

terminate(Reason, {EventMgr, Module}) ->
    %% the handler will be terminated automatically
    ok.

code_change(OldVsn, State, Extra) ->
    {ok, State}.

install(EventMgr, Module, Args) ->
    ok = gen_event:add_sup_handler(EventMgr, Module, Args).

/Uffe
-- 
Ulf Wiger, Senior Specialist,
   / / /   Architecture & Design of Carrier-Class Software
  / / /    Strategic Product & System Management
 / / /     Ericsson AB, Connectivity and Control Nodes




More information about the erlang-questions mailing list