In gen_event, what functional diff does it makes for gen_event:notify & gen_event:call

Ulf Wiger <>
Thu Feb 20 17:19:19 CET 2003

On Thu, 20 Feb 2003, Suresh S wrote:

>>> gen_event:call(EntMgr, Handler, Req) -->
>>> M:handle_event(Req, S)
>> No, a call is handled in M:handle_call(Query,State)
>i mean to ask for handle_call only , but in a hurry i
>Any how in case of M:handle_call(Req, S)
>How do we pass Handler = { M, Id }
>what is the need for Id?
>how do i pass the handler i have to handle?

>From the gen_event reference manual:

"Handler is the name of the callback module Module or a
tuple {Module,Id}, where Id is any term. The {Module,Id}
representation makes it possible to identify a specific
event handler when there are several event handlers using
the same callback module."

If you only have one instance of a specific handler, then
Handler becomes simply the module name.

The {Module,Id} construct is for the case that you have lots
of instances of similar handlers, and it's not practical, or
even desired, to implement a module for each instance. Id
can be anything that helps you tell the different handlers
apart in your application.

A very common way to identify the handler is to use the
?MODULE macro. Example from the OTP EVA application:

module eva_snmp_adaptation.erl:

start() ->
    gen_event:add_handler(alarm_handler, ?MODULE, []),
    register_events([{clear_alarm, alarmCleared,
                      snmpTrap, "standard trap",
                      {?MODULE, clear_alarm}}]).

register_events(Events) ->
    gen_event:call(alarm_handler, ?MODULE,
                   {reg_events, Events}).

handle_event({send_event, Event}, S) ->
    case mnesia:dirty_read({eva_snmp_map,
                            {event,}}) of
        [#eva_snmp_map{val = Data}] ->
            send_event(Data, Event);
        _ ->
    {ok, S};
handle_event(_, S) ->
    {ok, S}.

handle_call({reg_events, Events}, S) ->
    #state{event_struct = EStruct,
           next_event_index = Index} = S,
    {NEStruct, NIndex} = reg_events(Events, EStruct, Index),
    {ok, ok, S#state{event_struct = NEStruct,
next_event_index = NIndex}};

... where events are sent from eva_server.erl using
gen_event:sync_notify(alarm_handler, {send_event, Event})

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