fix:测试demo完善

This commit is contained in:
waner 2025-05-20 17:12:54 +08:00
parent a919287719
commit 0f46865151

View File

@ -23,7 +23,7 @@ public class SnmpTrapSender implements AutoCloseable{
private String community; private String community;
private int version; private int version;
public SnmpTrapSender(Map<String, String> map) throws IOException { public SnmpTrapSender(Map<String, String> map) throws Exception {
loadConfig(map); loadConfig(map);
initializeSNMP(); initializeSNMP();
} }
@ -36,13 +36,13 @@ public class SnmpTrapSender implements AutoCloseable{
version = Integer.parseInt(map.getOrDefault("snmp.version", "1")); version = Integer.parseInt(map.getOrDefault("snmp.version", "1"));
} }
private void initializeSNMP() throws IOException { private void initializeSNMP() throws Exception {
TransportMapping<?> transport = new DefaultUdpTransportMapping(); TransportMapping<?> transport = new DefaultUdpTransportMapping();
snmp = new Snmp(transport); snmp = new Snmp(transport);
snmp.listen(); snmp.listen();
} }
public ResponseEvent sendTrap(String message, int severity) throws IOException { public ResponseEvent sendTrap(String message, int severity) throws Exception {
ResponseEvent event = null; ResponseEvent event = null;
switch (version) { switch (version) {
case SnmpConstants.version1: case SnmpConstants.version1:
@ -57,7 +57,7 @@ public class SnmpTrapSender implements AutoCloseable{
return event; return event;
} }
private ResponseEvent sendV1Trap(String message, int severity) throws IOException { private ResponseEvent sendV1Trap(String message, int severity) throws Exception {
CommunityTarget target = createCommunityTarget(SnmpConstants.version1); CommunityTarget target = createCommunityTarget(SnmpConstants.version1);
PDUv1 pdu = new PDUv1(); PDUv1 pdu = new PDUv1();
configureCommonTrap(pdu, message, severity); configureCommonTrap(pdu, message, severity);
@ -65,7 +65,7 @@ public class SnmpTrapSender implements AutoCloseable{
return snmp.send(pdu, target); return snmp.send(pdu, target);
} }
private ResponseEvent sendV2Trap(String message, int severity) throws IOException { private ResponseEvent sendV2Trap(String message, int severity) throws Exception {
CommunityTarget target = createCommunityTarget(SnmpConstants.version2c); CommunityTarget target = createCommunityTarget(SnmpConstants.version2c);
PDU pdu = new PDU(); PDU pdu = new PDU();
configureCommonTrap(pdu, message, severity); configureCommonTrap(pdu, message, severity);
@ -96,7 +96,7 @@ public class SnmpTrapSender implements AutoCloseable{
} }
@Override @Override
public void close() throws IOException { public void close() throws Exception {
if (snmp != null) { if (snmp != null) {
snmp.close(); snmp.close();
snmp = null; snmp = null;