SNMP4J Синглтон Реализация
public class SNMPProxy implements Cloneable, Serializable
{
Snmp snmp = null;
/**
* The transport mapping helps to use either UDP or TCP .
*/
TransportMapping transport = null;
/**
* The snmp address of the robot.
*/
String address = null;
/**
* Community of each device
*/
private String community;
/**
* SNMP Version on each device
*/
private int snmpVersion;
//---------------------------------------------------------------------
// Constructors.
//---------------------------------------------------------------------
/**
* Constructor which takes in SNMP address of the device
* @param address SNMP Address of the bot
*/
public SNMPProxy(String address, String community, int snmpVersion)
{
this.address = address;
this.community = community;
this.snmpVersion = snmpVersion;
}
/**
* Empty Constructor
*/
public SNMPProxy()
{
}
//---------------------------------------------------------------------
// SNMP Methods.
//---------------------------------------------------------------------
/**
* Start the Snmp session. If you forget the listen() method you will not
* get any data because the communication is asynchronous
* and the listen() method listens for data.
* If you are only using SNMPv2c( MPv2c()), you can make a cleaner
* initialization that does not load cryptography related classes of v3
* @throws IOException
*/
public void start(MessageDispatcher disp) throws IOException
{
transport = new DefaultUdpTransportMapping();
snmp = new Snmp(disp,transport);
transport.listen();
}
/**
* Close the SNMP connection which will close the socket connection and the Thread.
*/
public void close() throws IOException
{
if (snmp != null)
{
snmp.close();
}
if (transport != null)
{
transport.close();
}
}
/**
* TODO pdu
*/
public PDU getPDU(OID... oids)
{
PDU pdu = new PDU();
pdu.setMaxRepetitions(512);
pdu.setNonRepeaters(0);
for (OID oid : oids)
{
pdu.add(new VariableBinding(oid));
}
pdu.setType(PDU.GET);
return pdu;
}
/**
* TODO ResponseEvent
*/
public ResponseEvent get(OID... oids) throws IOException
{
ResponseEvent response = snmp.send(getPDU(oids), getTarget(),null);
if (response != null)
{
return response;
}
throw new RuntimeException("GET timed out");
}
/**
* TODO
*/
public int dataToInt(ResponseEvent evn, int n)
{
return evn.getResponse().get(n).getVariable().toInt();
}
/**
* TODO
*/
public String dataToString(ResponseEvent evn, int n)
{
return evn.getResponse().get(n).getVariable().toString();
}
/**
* This method returns a Target, which contains information about
* where the data should be fetched and how.
* @return target
*/
public Target getTarget()
{
Address targetAddress = GenericAddress.parse(address);
CommunityTarget target = new CommunityTarget();
target.setCommunity(new OctetString(community));
target.setAddress(targetAddress);
target.setRetries(2);
target.setTimeout(1500);
target.setVersion(snmpVersion);
return target;
}
}
Я хочу создать экземпляр Singleton для SNMP. У меня есть несколько устройств, которые разговаривают с одним и тем же классом. Всякий раз, когда я говорю с этим классом, он создает экземпляр snmp и закрывает экземпляр после получения данных. Может ли кто-нибудь помочь мне с созданием одного экземпляра снаружи и вызывать один и тот же экземпляр каждый раз с любого устройства.