[SNMP4J] SNMP.close() not working for UdpTransport

Adam Brons abrons at us.ibm.com
Wed Aug 15 17:28:27 CEST 2007


I'm wondering if anyone has had a problem with releasing UPD resources 
when calling a Snmp.close().

I'm attempting to listen to both UDP and TCP port 162 on IP 0.0.0.0.  When 
I call Snmp.close() the TCP resources are immediately returned, however 
even after 65 seconds the UDP resources are still held.  Once the 
application exists and the JVM stops, the resources are released. 
Unfortunately, I need to be able to "restart" the listening on the TCP and 
UDP ports via user intervention.  Also it should be noted that I'm created 
a new Snmp object for each start.  I'm using version 1.8.1 of the SNMP4J 
API.

Here are some code snippets:

Constructor: 
        protected SNMPListener(SnmpConduitVariables variables, String 
conduitType) throws UnknownHostException { 
                this.variables = variables;
                this.conduitType = conduitType;
 
                if (variables != null && variables.getUsers() != null) {
                        configSnmpUsers(variables.getUsers());
                }
 
                // Create a thread pool to handle inbound SNMP events
                threadPool = ThreadPool.create("SNMPConduit", 
DISPATHER_THREAD_NUMBER);
                mtDispatcher = 
                        new MultiThreadedMessageDispatcher(threadPool, new 
MessageDispatcherImpl());
 
                // Add the SNMP versions we wish to monitor for
                mtDispatcher.addMessageProcessingModel(new MPv1());
                mtDispatcher.addMessageProcessingModel(new MPv2c());
                mtDispatcher.addMessageProcessingModel(new MPv3());
 
                // Setup Authentication and Encryption protocols for 
SNMPv3
                SecurityProtocols.getInstance().addDefaultProtocols();
 
                /*
                 * Builds the User Based Security Model up containing all 
supported security
                 * protocols, and setting a default engineID.
                 * 
                 * MPv3.createLocalEngineID might be something we allow 
the user to specify as part of the 
                 * Sensor or Conduit configuration.
                 */
            usm = new USM(SecurityProtocols.getInstance(),
              new OctetString(MPv3.createLocalEngineID()), 0);
            SecurityModels.getInstance().addSecurityModel(usm);


Listen Method:
        public synchronized void listen() throws IOException, 
UnknownHostException {
                if (variables == null) {
                        logger.error("No bind address or bind port were 
specificed");
                        throw new 
UnknownHostException("SnmpConduitVariables is null therefore no bind 
address and port can be determined");
                }
 
                // Create Snmp4j Processor using our supplied 
MessageProcessingModel
                snmp = new Snmp(mtDispatcher);
 
                // Make a tcp and udp based address for listening
                UdpAddress udpAddr = new 
UdpAddress(variables.getBindAddress() + "/" + variables.getBindPort());
                TcpAddress tcpAddr = new 
TcpAddress(variables.getBindAddress() + "/" + variables.getBindPort());
                SNMPEventDispatcher dispatcher = new 
SNMPEventDispatcher(variables, conduitType);
 
                // Add transport mapping and command handler to Snmp 
Engine
                if (snmp.addNotificationListener(udpAddr, dispatcher) &&
                        snmp.addNotificationListener(tcpAddr, dispatcher)) 
{
 
                        // Start listening
                        snmp.listen();
                        logger.info("SNMP Conduit Listening on 
UDP:"+udpAddr.toString()+"...");
                        logger.info("SNMP Conduit Listening on 
TCP:"+tcpAddr.toString()+"...");
 
                        // Just wait for something to happen.
                        try { 
                                synchronized (mutex) {
                                        if (logger.isDebugEnabled()) 
logger.debug("SnmpListener '"+variables.getBindAddress()+"' Waiting...");
                                        mutex.wait();
                                }
                        }
                    catch (InterruptedException ex) {
                        if (logger.isDebugEnabled()) {
                                logger.debug("SNMPConduit Caught 
Interrupted Exception: ", ex);
                        }
                    } 
                } else {
                        logger.error("Unable to bind using 
address("+udpAddr.toString()+")");
                        throw new IOException("Unable to bind using 
address("+udpAddr.toString()+")");
                }
        }

Close Method:
        protected void close() {
                synchronized (mutex) {
                        if (logger.isDebugEnabled()) 
logger.debug("SnmpListener '"+variables.getBindAddress()+"' stopping...");
                        mutex.notify();
                        try {
                                snmp.close();
                                mutex.wait(65000);
                                if (logger.isDebugEnabled()) 
logger.debug("SnmpListener: '"+variables.getBindAddress()+"' has closed");
                        }
                        catch(IOException ioe) {}
                        catch(InterruptedException ie) {}
                }
        }

Adam Brons
TSOM Software Engineer
IBM Tivoli Software
(404) 591-8246 office


More information about the SNMP4J mailing list