KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SNMPMultithreadTest


1 import snmp.*;
2 import java.net.*;
3
4
5
6 public class SNMPMultithreadTest
7                     implements Runnable JavaDoc
8 {
9
10     SNMPv1CommunicationInterface comInterface;
11     
12     public class RetrieveThread extends Thread JavaDoc
13     {
14         public int i;
15         
16         public RetrieveThread(Runnable JavaDoc r, int i)
17         {
18             super(r);
19             this.i = i;
20         }
21         
22     }
23     
24     
25     public static void main(String JavaDoc args[])
26     {
27         SNMPMultithreadTest app = new SNMPMultithreadTest();
28     }
29     
30     
31     public SNMPMultithreadTest()
32     {
33
34         try
35         {
36
37             // create a communications interface to a remote SNMP-capable device;
38
// need to provide the remote host's InetAddress and the community
39
// name for the device; in addition, need to supply the version number
40
// for the SNMP messages to be sent (the value 0 corresponding to SNMP
41
// version 1)
42
InetAddress hostAddress = InetAddress.getByName("10.0.1.1");
43             String JavaDoc community = "public";
44             int version = 0; // SNMPv1
45

46             comInterface = new SNMPv1CommunicationInterface(version, hostAddress, community);
47             
48             comInterface.setSocketTimeout(500);
49             
50             for (int i = 0; i < 1000; i++)
51             {
52                 RetrieveThread retrievalThread = new RetrieveThread(this, i);
53                 retrievalThread.start();
54             }
55             
56             
57         }
58         catch(Exception JavaDoc e)
59         {
60             System.out.println("Exception during SNMP operation: " + e + "\n");
61         }
62         
63     }
64     
65     
66     public void run()
67     {
68         int threadIndex = ((RetrieveThread)Thread.currentThread()).i;
69         
70         while (true)
71         {
72             try
73             {
74     
75                 // now send an SNMP GET request to retrieve the value of the SNMP variable
76
// corresponding to OID 1.3.6.1.2.1.2.1.0; this is the OID corresponding to
77
// the device identifying string, and the type is thus SNMPOctetString
78
String JavaDoc itemID = "1.3.6.1.2.1.1.1.0";
79                 
80                 System.out.println("Thread " + threadIndex + ": Retrieving value corresponding to OID " + itemID);
81                 
82                 // the getMIBEntry method of the communications interface returns an SNMPVarBindList
83
// object; this is essentially a Vector of SNMP (OID,value) pairs. In this case, the
84
// returned Vector has just one pair inside it.
85
SNMPVarBindList newVars = comInterface.getMIBEntry(itemID);
86                 
87                 // extract the (OID,value) pair from the SNMPVarBindList; the pair is just a two-element
88
// SNMPSequence
89
SNMPSequence pair = (SNMPSequence)(newVars.getSNMPObjectAt(0));
90                 
91                 // extract the object identifier from the pair; it's the first element in the sequence
92
SNMPObjectIdentifier snmpOID = (SNMPObjectIdentifier)pair.getSNMPObjectAt(0);
93                 
94                 // extract the corresponding value from the pair; it's the second element in the sequence
95
SNMPObject snmpValue = pair.getSNMPObjectAt(1);
96                 
97                 // print out the String representation of the retrieved value
98
System.out.println("Thread " + threadIndex + ": Retrieved value: type " + snmpValue.getClass().getName() + ", value " + snmpValue.toString());
99                 
100                     
101             }
102             catch(Exception JavaDoc e)
103             {
104                 System.out.println("Exception during SNMP operation: " + e + "\n");
105             }
106             
107         }
108         
109     }
110
111 }
Popular Tags