Wiki source code of Why do I get timeouts when using a Snmp instance per request?
Last modified by Frank Fock on 2024/05/25 21:03
Hide last authors
author | version | line-number | content |
---|---|---|---|
![]() |
1.1 | 1 | Creating a TransportMapping and Snmp instance for each request (or even a few requests) like shown below, generates a big overhead and IO latency through creating and closing UDP (or TCP) ports on the operating system. This overhead will let the approach below causing timeouts and showing very low performance. |
2 | |||
3 | **Bad example:** | ||
4 | |||
5 | {{code language="java"}} | ||
6 | public ResponseEvent doSingleRequest(PDU pdu) { | ||
7 | CommunityTarget target = new CommunityTarget(); | ||
8 | Address addr = new UdpAddress("127.0.0.1/161"); | ||
9 | target.setCommunity(new OctetString("public")); | ||
10 | target.setAddress(addr); | ||
11 | target.setVersion(SnmpConstants.version2c); | ||
12 | |||
13 | TransportMapping transport = new DefaultUdpTransportMapping(); | ||
14 | Snmp snmp = new Snmp(transport); | ||
15 | snmp.listen(); | ||
16 | ResponseEvent response = snmp.send(pdu, target); | ||
17 | snmp.close(); | ||
18 | return responseEvent; | ||
19 | } | ||
20 | {{/code}} | ||
21 | |||
22 | **Better example:** | ||
23 | |||
24 | {{code language="java"}} | ||
25 | class SnmpRequestProcessor { | ||
26 | |||
27 | ... | ||
28 | |||
29 | public SnmpRequestProcessor() { | ||
30 | .. | ||
31 | } | ||
32 | |||
33 | public void setup() { | ||
34 | CommunityTarget target = new CommunityTarget(); | ||
35 | Address addr = new UdpAddress("127.0.0.1/161"); | ||
36 | target.setCommunity(new OctetString("public")); | ||
37 | target.setAddress(addr); | ||
38 | target.setVersion(SnmpConstants.version2c); | ||
39 | |||
40 | transport = new DefaultUdpTransportMapping(); | ||
41 | snmp = new Snmp(transport); | ||
42 | snmp.listen(); | ||
43 | } | ||
44 | |||
45 | public ResponseEvent doSingleRequest(PDU pdu) { | ||
46 | ResponseEvent response = snmp.send(pdu, target); | ||
47 | return responseEvent; | ||
48 | } | ||
49 | |||
50 | public void shutDown() { | ||
51 | snmp.close(); | ||
52 | } | ||
53 | {{/code}} |