Receiving Notifications


Notifications can be received using both high-level API (Beans components) and low-level API packages.


Using High-Level API explains the usage of high-level API in receiving SNMP notifications.


Using Low-Level API explains the usage of low-level API in receiving SNMP notifications.


Trap Performance discusses the performance of notifications.


Using High-Level API


The SnmpTrapReceiver bean in the high-level API is used to receive v1/v2/v3 traps. This bean listens for traps on a particular port. When a trap is received from an agent, it checks the trap version. If the trap received is a v3 trap, it performs a v3 authentication and fires a trap event to all the registered listener objects.


The TrapEvent class contains the Trap-PDU. This trap event contains the trap data that is forwarded to the registered trap listener objects. The trap listener object then handles the received trap.


To implement the Trap Listener interface and instantiate the SnmpTrapReceiver bean:


public class SnmpTrapd implements TrapListener {

SnmpTrapReceiver trapreceiver = new SnmpTrapReceiver();


The following code sets the port in which the trap is received and registers the trap listener.



trapreceiver.addTrapListener(this); // register the listener for trap events

System.out.println("Waiting to receive traps .......");


The listener invokes the receivedTrap(TrapEvent) method when the trap is received by the TrapReceiver bean. The event TrapEvent contains the trap data. The traps can be received and results can be printed. View the complete example present in <tutorials/highlevelapi/>.


The trap applications listen for traps in the default port (162). When a trap is received, the PDU information, such as agent address, trap type, etc. is printed. Traps can also be received in a different port other than 162 by using the -p option.


In UNIX environment, port numbers below 1024 are reserved and can be used only by root (superuser). Therefore, if non-root users run the application, the following error message may be received.


" Permission Denied"


There are two ways to avoid this exception. One way is to login as root and run the program. The other way is to specify a port greater than 1023 using the -p option. For example, the following command listens for traps on port 2000.


java snmptrapd -p 2000


However, we need to make sure that the port specified is not used by any other application. Otherwise the following exception is thrown. Address already in use




  • If the setTrapAuthEnable() method is set to true, v1 and v2c traps are authenticated with the community strings. If set to false, all the traps are received irrespective of the value of the community.

  • The method setCommunityAuthEnable(boolean) enables or disables the authentication of community name. The default value is set to true.


Using Low-Level API


There are two ways by which an application can receive an SNMP trap message.

The interface SnmpClient is used by applications that wish to send and receive messages asynchronously. The SnmpClient interface implements callback, authentication, and debugging functions.


Receiving trap messages by using callback() in SnmpClient


The interface SnmpClient is used by applications to send and receive messages asynchronously. The SnmpClient interface implements callback, authentication, and debugging functions.


We implement SnmpClient in our main class.

We need to instantiate and start the SnmpAPI and open SnmpSession. We need to add the SnmpClient interface implementation to the session to invoke the callback, authentication, and debugging functions. By convention, the traps are received at the port 162. In this example, we set the local port to 8001 for receiving the traps.

Now, we have to implement three methods of the SnmpClient interface.


The authenticate() method should check the community received in the response PDU with the community of the particular session. The "community" argument in the authenticate() method is same as the and the "pdu" argument is the received PDU.

The callback() method should handle the PDU received for a particular session. The "session" argument is the session for which the PDU is received. The "pdu" argument is the PDU received. The "requestID" is the request ID of the sent PDU for which the response is received. The received trap information is handled here.

The debugprint is used for printing any debug information.

Trap Performance


WebNMS SNMP API can receive about 2000 to 2500 traps per second continuously without dropping packets. When a burst of traps is sent in a short time, it can receive more than the above-mentioned number.


The trap performance is dependent on various factors such as the type of the OS, network traffic, etc. As a result, the trap receiver applications might not always receive all the traps.


One main reason for loss of traps is the UDP buffer overflow. As of now, the trap pool is the UDP buffer. Therefore, the application gets all the traps from the UDP buffer. If the application performs some action each time it receives a trap, there is a chance that UDP buffer overflows resulting in loss of packets. One way to avoid this is to have a trap receiver that continuously receives the trap and puts it into a buffer. Then another job can be scheduled to process these traps from the buffer one at a time. This way, we can implement a buffering technique to process traps.


The snmptrapd example, which uses the low-level API, adopts the callback mechanism to receive traps. The performance might be degraded if additional processes are performed within callback().


Alternatively, we can use the setCallbackthread(boolean)link to javadocs method of SnmpSession class to implement the callback mechanism. By default, this method is set to false. Setting it true enables to run the callback in a separate thread. This is recommended only if any significant implementation is to be done in the callback. Calling the callback from a separate thread deteriorates the performance of the receiver thread in receiving responses or traps.


The trapreceiver example, which uses the high-level API, can store the received traps in a vector and process them in the main thread instead of using the receivedTrap method.

    1. // Trap application code snippet

      public class trapreceiverApp extends Thread


      Vector trapTable;

      // Remember to synchronize the enqueing and dequeing methods from this vector

      // Thread to continuously process the received traps and do all further manipulations.

      run ()


      // Read from the trapTable vector and do all processing and then deque.


      receivedTrap (TrapEvent trap)


      // Enque it in the trapTable vector and return



View the complete example present in <tutorials/lowlevelapi/>.



Copyright © 2012, ZOHO Corp. All Rights Reserved.