Java Dynamic Management Kit 3.2 Programming Guide
[ Previous ][ Fast Back ]Chapter 20. Developing SNMP Managers With the Java Dynamic Management Kit [ Next ]

SNMP Traps

The SNMP manager API provides classes that enable you to receive SNMP v1 and SNMP v2 trap PDUs.

SNMP v1 Traps

To receive SNMP v1 trap PDUs, you must instantiate an SnmpTrapAgent. The SnmpTrapAgent allows you to create a receiver/dispatcher for SNMP traps. It is run as a thread. The SnmpTrapListener interface defines one callback method that must be implemented for classes that are required to receive SNMP v1 trap PDUs. Example 20-6 shows the instantiation of a trap listener.

Example 20-6. Instantiating a Trap Listener
// Create a listener and dispatcher for SNMP Traps
// trapAgent is run as a thread and listens for traps in UDP port= 8086
// trapAgent will receive a callback when a valid trap PDU is received

SnmpTrapAgent trapAgent=newSnmpTrapAgent(8086);
trapAgent.addTrapListener(new TrapListenerImpl());
new Thread(trapAgent).start();

A class implementing the SnmpTrapListener interface is added to the SnmpTrapAgent. It is the callback object that is called when a valid SNMP v1 trap PDU is present. Example 20-7 shows the code for a class implementing the SnmpTrapListener interface.

Example 20-7. Implementing an SNMP v1 Trap Callback Class

// This class implements the SnmpTrapListener interface. The callback method
// processSnmpTrap is called when a valid SNMP v1 trap PDU is received.
public class TrapListenerImpl implements SnmpTrapListener {
   public void processSnmpTrap(SnmpPduTrap trap) {
      java.lang.System.out.println("NOTE: TrapListenerImpl received Trap :");
      java.lang.System.out.println("\tGeneric "+trap.genericTrap);
      java.lang.System.out.println("\tSpecific "+trap.specificTrap);
      java.lang.System.out.println("\tTimeStamp "+trap.timeStamp);
      java.lang.System.out.println("\tAgent address "
          +trap.agentAddr.stringValue());
   }
}

SNMP v2 Traps

To receive SNMP v2 trap PDUs, you must instantiate an SnmpTrapAgent. The SnmpTrapAgent allows you to create a receiver/dispatcher for SNMP traps. It is run as a thread. The SnmpV2TrapListener interface defines one callback method that must be implemented for classes that are required to receive SNMP v2 trap PDUs. Example 20-8 shows the instantiation of a trap listener.

Example 20-8. Instantiating a Trap Listener

// Create a listener and dispatcher for SNMP Traps
// trapAgent is run as a thread and listens for traps in UDP port= 8086
// trapAgent will receive a callback when a valid trap PDU is received

SnmpTrapAgent trapAgent=newSnmpTrapAgent(8086);
trapAgent.addTrapListener(new TrapListenerImpl());
new Thread(trapAgent).start();

A class implementing the SnmpV2TrapListener interface is added to the SnmpTrapAgent. It is the callback object that is called when a valid trap PDU is present. Example 20-9 shows the code for a class implementing the SnmpV2TrapListener interface.

Example 20-9. Implementing an SNMP v2 Trap Callback Class

// This class implements the SnmpV2TrapListener interface.
// The callback method processSnmpTrap is called when a
// valid SNMP v2 trap PDU is received.

public class TrapListenerImpl implements SnmpV2TrapListener {

   public void processSnmpTrap(SnmpPduRequest trap) {
   ...
   }
}


[ Previous ][ Home ][ Next ]
Loading Metadata Into the MibStore[ Up ]Polling