[SNMP4J] RE: TransportMapping/Snmp close() takes up to 1 sec tocomplete

Frank Fock fock at agentpp.com
Wed Feb 1 23:17:59 CET 2006


Hi Kevin,

OK, then call DefaultUdpTransportMapping.setSocketTimeout(0); and
your socket will be closed immediately.

Best regards,
Frank

Kevin Stretch wrote:

>That didn't take near as long as I thought
>This page has an excellent explanation of what is going wrong:
>http://builder.com.com/5100-6370-5144546.html
>
>It suggests using socket.close() and dealing with the thrown SocketException rather than using listener.interrupt().  If you really want to get fancy (and at the same time depend on later versions of java) you could catch ClosedByInterruptException.
>
>In any case can someone fix this?  It's going to be a pain to patch the code for the near while and I cannot afford the delay over thousands of independent transactions.
>
>-Kevin
>
>
>-----Original Message-----
>From: snmp4j-bounces at agentpp.org [mailto:snmp4j-bounces at agentpp.org] On Behalf Of Kevin Stretch
>Sent: Wednesday, February 01, 2006 1:51 PM
>To: snmp4j at agentpp.org
>Subject: [SNMP4J] RE: TransportMapping/Snmp close() takes up to 1 sec tocomplete
>
>I decided to attempt to fix the SNMP4J code rather than re-writing my code or throwing it out.
>
>I traced the issue to the join() function in the ListenThread class.
>
>Going further it looks like the code is waiting for something before it throws an uncaught SocketTimeoutException.
>
>Work continues to understand and correct the issue (if it's even possible).
>
>        try {
>System.out.println("3a");
>          socket.receive(packet);
>
><----------->
>
>        catch (SocketTimeoutException stex) {
>System.out.println("ee-aa\n");
>          // ignore
>        }
>
>-Kevin
>
>
>
>
>-----Original Message-----
>From: Kevin Stretch 
>Sent: Tuesday, January 31, 2006 4:35 PM
>To: 'snmp4j at agentpp.org'
>Subject: RE: TransportMapping/Snmp close() takes up to 1 sec to complete
>
>I still cannot figure this out.
>I've taken code directly from the website
>http://www.snmp4j.org/doc/org/snmp4j/Snmp.html
>And I still see this issue that Snmp.close() is taking up to a second to execute.
> 
>In fact another issue comes up now where in some instances it takes up to 5 minutes to create a new Snmp object.
> 
>I'm not sure if it's an issue with the SNMP4J code, my hardware, my methods or all three.
> 
>The code is below along with some other info.
>If anyone could help I would appreciate it.
>Otherwise I'll have to figure something else out.
> 
>-Kevin
> 
> 
> 
>[root at rpe1 src]# time java -cp ../lib/SNMP4J.jar:./ test
>16:12:07.072171 IP ssssssss.38540 > ssssss.snmp:  C=sssss GetRequest(39)  transmission.127.1.3.7.1.2.0.17.128.101.229.108[|snmp]
>16:12:07.073290 IP sssss.snmp > ssssssss.38540:  C=sssss GetResponse(39)  transmission.127.1.3.7.1.2.0.17.128.101.229.108=[|snmp]
>ANSWER <65573>
>1138749127098
>1138749128083
> 
>real    0m44.075s
>user    0m0.290s
>sys     0m0.016s
>[root at rpe1 src]#
>[root at rpe1 src]#
>[root at rpe1 src]#
>[root at rpe1 src]# time java -cp ../lib/SNMP4J.jar:./ test
>16:12:10.729002 IP ssssss.38542 > sssssss.snmp:  C=sss GetRequest(39)  transmission.127.1.3.7.1.2.0.17.128.101.229.108[|snmp]
>16:12:10.730447 IP ssssss.snmp > sssssss.38542:  C=ssssss GetResponse(39)  transmission.127.1.3.7.1.2.0.17.128.101.229.108=[|snmp]
>ANSWER <65573>
>1138749130755
>1138749131741
> 
>real    0m1.272s
>user    0m0.285s
>sys     0m0.022s
>[root at rpe1 src]#
>[root at rpe1 src]#
>[root at rpe1 src]# time java -cp ../lib/SNMP4J.jar:./ test
>16:17:22.443675 IP sssssss.38542 > sssssss.snmp:  C=ss GetRequest(39)  transmission.127.1.3.7.1.2.0.17.128.101.229.108[|snmp]
>16:17:22.444572 IP ssssss.snmp > sssssss.38542:  C=ssss GetResponse(39)  transmission.127.1.3.7.1.2.0.17.128.101.229.108=[|snmp]
>ANSWER <65573>
>1138749442469
>1138749443455
> 
>real    5m10.415s
>user    0m0.289s
>sys     0m0.015s
> 
>[root at rpe1 src]# uname -a
>Linux sssss 2.6.9-11.ELsmp #1 SMP Wed Jun 8 16:59:12 CDT 2005 x86_64 x86_64 x86_64 GNU/Linux
>[root at rpe1 src]#
>[root at rpe1 src]# cat /etc/redhat-release
>CentOS release 4.2 (Final)
> 
>[root at rpe1 src]# javac -cp ../lib/SNMP4J.jar test.java
>[root at rpe1 src]# vi test.java                    <<HASH OUT snmp.close() STATEMENT)>>
>[root at rpe1 src]# javac -cp ../lib/SNMP4J.jar test.java
>[root at rpe1 src]# java -cp ../lib/SNMP4J.jar:./ test
>ANSWER <65573>
>1138750441272
>1138750441272
> 
>[root at rpe1 src]# cat test.java  <<snmp.close() STATEMENT REINSERTED>>
>import java.util.*;
>import java.io.*;
>import org.snmp4j.*;
>import org.snmp4j.event.*;
>import org.snmp4j.mp.*;
>import org.snmp4j.smi.*;
>import org.snmp4j.security.*;
>import org.snmp4j.transport.*;
>import org.snmp4j.util.*;
>import org.snmp4j.AbstractTarget.*;
> 
> 
>public class test {
> 
>        public void go()  {
> 
>        try {
> 
>   Address targetAddress = GenericAddress.parse("udp:sssssss/161");
>   TransportMapping transport = new DefaultUdpTransportMapping();
>   Snmp snmp = new Snmp(transport);
>   MPv3 mpv3 =
>       (MPv3)snmp.getMessageProcessingModel(MessageProcessingModel.MPv3);
>   USM usm = new USM(SecurityProtocols.getInstance(),
>                     new OctetString(mpv3.createLocalEngineID()), 0);
>   SecurityModels.getInstance().addSecurityModel(usm);
>   transport.listen();
> 
>   // setting up target
>   CommunityTarget target = new CommunityTarget();
>   target.setCommunity(new OctetString("sssssss"));
>   target.setAddress(targetAddress);
>   target.setRetries(2);
>   target.setTimeout(1500);
>   target.setVersion(SnmpConstants.version1);
>   // creating PDU
>   PDU pdu = new PDU();
>   pdu.add(new VariableBinding(new OID(new int[] {1,3,6,1,2,1,10,127,1,3,7,1,2,0,17,128,101,229,108})));
>   pdu.setType(PDU.GET);
>   // sending request
>   // send the PDU
>   ResponseEvent response = snmp.send(pdu, target);
>   // extract the response PDU (could be null if timed out)
>   PDU responsePDU = response.getResponse();
> 
>   if (responsePDU != null) {
>   VariableBinding responseVB = responsePDU.get(0);
>   Variable variable = responseVB.getVariable();
>   System.out.println ("ANSWER <" + variable.toString() + ">");
>   } else if (response.getError() != null)
>      throw response.getError();
>   else
>      throw new Exception ("Request Timed Out");
> 
>Calendar cal = Calendar.getInstance();
>System.out.println (cal.getTimeInMillis());
>snmp.close();
>cal = Calendar.getInstance();
>System.out.println (cal.getTimeInMillis());
> 
>   } catch (Exception e) {
>      e.printStackTrace();
>   }
>}
> 
>        public static void main(String[] args) {
> 
>                test dd = new test();
>                dd.go();
> 
>        }
> 
> 
>}
>[root at rpe1 src]#
> 
> 
> 
>-----Original Message-----
>From: Kevin Stretch 
>Sent: Thursday, January 26, 2006 10:20 AM
>To: 'snmp4j at agentpp.org'
>Subject: TransportMapping/Snmp close() takes up to 1 sec to complete
> 
>I'm at a loss here.  If I don't close the snmp object I create I end up with a bunch of open ports each time I create a new object:
> 
>tcp        0      0 0.0.0.0:1235                0.0.0.0:*                   LISTEN      29149/java
>udp        0      0 0.0.0.0:34524               0.0.0.0:*                               29149/java
>udp        0      0 0.0.0.0:34525               0.0.0.0:*                               29149/java
>udp        0      0 0.0.0.0:34526               0.0.0.0:*                               29149/java
>udp        0      0 0.0.0.0:34527               0.0.0.0:*                               29149/java
>udp        0      0 0.0.0.0:34528               0.0.0.0:*                               29149/java
> 
>However If I attempt to use "close()" on either the Snmp object or TransportMapping object it takes up to 1 second to complete:
> 
>1138295085444
>1138295086439
> 
>1138295094187
>1138295095187
> 
>1138295104089
>1138295105090
> 
>The program is wrapped with the "Java Service Wrapper" to make it a daemon.
>The program uses multiple class variables to enable its use with a SAX parser (legacy code).
>It's running on Redhat Enterprise 4
>The request is a V1 request (but it must also handle v3 requests).
> 
>The problem is that it's taking to long to do 1000 SNMP requests right now.  Has anyone seen this behavior before and might have an idea what's taking so long?
>Below are a few pieces of sample code.
> 
>Thanks in advance,
>-Kevin
> 
> 
> 
>                        //TransportMapping transport = new DefaultUdpTransportMapping();
>                        transport = new DefaultUdpTransportMapping();
>                        snmp = new Snmp(transport);
> 
>                        MPv3 mpv3 = (MPv3)snmp.getMessageProcessingModel(MessageProcessingModel.MPv3);
>                        usm = new USM(SecurityProtocols.getInstance(), new OctetString(mpv3.createLocalEngineID()), 0);
> 
>                        SecurityModels.getInstance().addSecurityModel(usm);
>                        transport.listen();
> 
> 
>        public void close () throws IOException {
>//if (snmp != null) snmp.close();
>                if (transport != null ) {
>                        Calendar cal = Calendar.getInstance();
>                        System.out.println (cal.getTimeInMillis());
>                        transport.close();
>//trust me that this call doesn't take 1 second, I checked.
>                        cal = Calendar.getInstance();
>                        System.out.println (cal.getTimeInMillis());
>                }
>                }
> 
> 
>_______________________________________________
>SNMP4J mailing list
>SNMP4J at agentpp.org
>http://lists.agentpp.org/mailman/listinfo/snmp4j
>_______________________________________________
>SNMP4J mailing list
>SNMP4J at agentpp.org
>http://lists.agentpp.org/mailman/listinfo/snmp4j
>
>
>  
>


-- 
AGENT++
http://www.agentpp.com
http://www.mibexplorer.com
http://www.mibdesigner.com





More information about the SNMP4J mailing list