1 import snmp.*; 2 import java.net.*; 3 4 5 6 public class SNMPMultithreadTest 7 implements Runnable  8 { 9 10 SNMPv1CommunicationInterface comInterface; 11 12 public class RetrieveThread extends Thread  13 { 14 public int i; 15 16 public RetrieveThread(Runnable r, int i) 17 { 18 super(r); 19 this.i = i; 20 } 21 22 } 23 24 25 public static void main(String args[]) 26 { 27 SNMPMultithreadTest app = new SNMPMultithreadTest(); 28 } 29 30 31 public SNMPMultithreadTest() 32 { 33 34 try 35 { 36 37 InetAddress hostAddress = InetAddress.getByName("10.0.1.1"); 43 String community = "public"; 44 int version = 0; 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 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 String 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 SNMPVarBindList newVars = comInterface.getMIBEntry(itemID); 86 87 SNMPSequence pair = (SNMPSequence)(newVars.getSNMPObjectAt(0)); 90 91 SNMPObjectIdentifier snmpOID = (SNMPObjectIdentifier)pair.getSNMPObjectAt(0); 93 94 SNMPObject snmpValue = pair.getSNMPObjectAt(1); 96 97 System.out.println("Thread " + threadIndex + ": Retrieved value: type " + snmpValue.getClass().getName() + ", value " + snmpValue.toString()); 99 100 101 } 102 catch(Exception e) 103 { 104 System.out.println("Exception during SNMP operation: " + e + "\n"); 105 } 106 107 } 108 109 } 110 111 } | Popular Tags |