[SNMP4J] How to "walk" mib in multi thread environment

justdoit.pli justdoit.pli at gmail.com
Thu Oct 13 16:58:16 CEST 2011


Hi Frank:
              I attempt to write a tool to get serval MIB values with hign-performance, if the number of MIB is too big I need to do that in multi thread environment.  
              I have complete function that "get" mib value with multi thread scene (with MultiThreadedMessageDispatcher) but I also need "walk" values of mib.  I don't know how to do that. Could you help me? thanks
              The "get" code is as follows:
              
public class commonSnmpSynchronousSender {
private ConcurrentHashMap<String, HashMap<String, String>> result = new ConcurrentHashMap<String, HashMap<String, String>>();
private Snmp snmp;
MessageDispatcher dispatcher;
private String threadPoolName = "default";
private int threadPoolSize = 10;
public commonSnmpSynchronousSender(String threadPoolName, int threadPoolSize) throws IOException{
this.threadPoolName = threadPoolName;
this.threadPoolSize = threadPoolSize;
dispatcher = new MultiThreadedMessageDispatcher(ThreadPool.create(threadPoolName, threadPoolSize),
                        new MessageDispatcherImpl());
snmp = new Snmp(dispatcher,new DefaultUdpTransportMapping());
    snmp.getMessageDispatcher().addMessageProcessingModel(new MPv1());
    snmp.getMessageDispatcher().addMessageProcessingModel(new MPv2c());
    snmp.getMessageDispatcher().addMessageProcessingModel(new MPv3());
snmp.listen();
}
public ConcurrentHashMap<String, HashMap<String, String>> getResult() throws IOException{
((MultiThreadedMessageDispatcher) dispatcher).stop();
snmp.close();
return result;
}
public void send(String ip, String community, String mibStr) throws IOException{
commonSnmpSynchronousListener listener = new commonSnmpSynchronousListener(this);
listener.setKey(ip+mibStr);
CommunityTarget target = new CommunityTarget();
target.setVersion(SnmpConstants.version1);
target.setCommunity(new OctetString(community));
target.setAddress(new UdpAddress(ip + "/161"));
PDU pdu = new PDU();
pdu.add(new VariableBinding(new OID(mibStr)));
pdu.setType(PDU.GET);
snmp.send(pdu, target, null, listener); 
}
public void callback(String key, HashMap<String, String> mibRes){
this.result.put(key, mibRes);
}
}

public class commonSnmpSynchronousListener implements ResponseListener{
    private String key;
    commonSnmpSynchronousSender sender;
    public commonSnmpSynchronousListener(commonSnmpSynchronousSender commonSender){
     sender = commonSender;
    }
public void onResponse(ResponseEvent event) {
((Snmp)event.getSource()).cancel(event.getRequest(), this);
PDU response = event.getResponse();
HashMap<String, String> mibRes = new HashMap<String, String>();
if(null != response){
for (Object item : response.getVariableBindings()){
if(null != item && item.toString().indexOf("=")!= -1){
int index = item.toString().indexOf("=");
mibRes.put(item.toString().substring(0,index), item.toString().substring(index + 1));
}
}
  sender.callback(key, mibRes); 
}
}
public void setKey(String key) {
this.key = key;
}

}





regards
pengli


More information about the SNMP4J mailing list