import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import org.snmp4j.CommunityTarget;
import org.snmp4j.PDU;
import org.snmp4j.Snmp;
import org.snmp4j.mp.SnmpConstants;
import org.snmp4j.smi.OID;
import org.snmp4j.smi.OctetString;
import org.snmp4j.smi.UdpAddress;
import org.snmp4j.transport.DefaultUdpTransportMapping;
import org.snmp4j.util.DefaultPDUFactory;
import org.snmp4j.util.TableEvent;
import org.snmp4j.util.TableListener;
import org.snmp4j.util.TableUtils;
public class TableUtilAsyn {
public static void walk() {
try {
Snmp snmp = new Snmp(new DefaultUdpTransportMapping()); // 构造一个UDP
snmp.listen(); // 开始监听snmp消息
CommunityTarget target = new CommunityTarget();
target.setCommunity(new OctetString(“public”));// snmpv2的团体名
target.setVersion(SnmpConstants.version2c); // snmp版本
target.setAddress(new UdpAddress(“172.16.5.2/161”));
target.setTimeout(60000); // 时延
target.setRetries(3); // 重传
final CountDownLatch latch = new CountDownLatch(1);
TableUtils utils = new TableUtils(snmp, new DefaultPDUFactory(PDU.GETBULK));// GETNEXT
// GETBULK
utils.setMaxNumRowsPerPDU(5); // only for GETBULK, set
// max-repetitions, default is 10
OID[] columnOIDs = new OID[] { new OID(“1.3.6.1.2.1.2.2.1.1”)};
TableListener listener=new TableListener() {
@Override
public boolean next(TableEvent arg0) {
// TODO Auto-generated method stub
System.out.println(arg0.toString()+”—-next”);
return true;
}
@Override
public boolean isFinished() {
// TODO Auto-generated method stub
System.out.println(“finished—“);
return false;
}
@Override
public void finished(TableEvent arg0) {
// TODO Auto-generated method stub
System.out.println(arg0.toString()+”–finished”);
}
};
utils.getTable(target, columnOIDs, listener, null, null, null);
boolean wait;
try {
wait = latch.await(30, TimeUnit.SECONDS);
System.out.println(“latch.await =:” + wait);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String args[]) {
walk();
}
}