- java.lang.Object
-
- org.snmp4j.util.MultiThreadedMessageDispatcher
-
- All Implemented Interfaces:
MessageDispatcher,TransportListener
public class MultiThreadedMessageDispatcher extends java.lang.Object implements MessageDispatcher
TheMultiThreadedMessageDispatcherclass is a decorator for anyMessageDispatcherinstances that processes incoming message with a suppliedThreadPool. The processing is thus concurrent on up to the size of the supplied thread pool threads.In contrast to a
MessageDispatcherImplaMultiThreadedMessageDispatchercopies the incomingByteBufferforprocessMessage(TransportMapping, Address, ByteBuffer, TransportStateReference)to allow concurrent processing of the buffer.- Since:
- 1.0.2
- Version:
- 3.1
- Author:
- Frank Fock
-
-
Constructor Summary
Constructors Constructor Description MultiThreadedMessageDispatcher(WorkerPool workerPool, MessageDispatcher decoratedDispatcher)Creates a multi-threaded message dispatcher using the providedThreadPoolto concurrently process incoming messages that are forwarded to the supplied decoratedMessageDispatcher.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddCommandResponder(CommandResponder listener)Adds aCommandResponderinstance to the message dispatcher.voidaddCounterListener(CounterListener counterListener)Adds aCounterListenerto the dispatcher.voidaddMessageProcessingModel(MessageProcessingModel model)Adds aMessageProcessingModelto the dispatcher.voidaddTransportMapping(TransportMapping<? extends Address> transport)Adds aTransportMappingto the dispatcher.MessageProcessingModelgetMessageProcessingModel(int messageProcessingModel)Gets theMessageProcessingModelfor the supplied message processing model ID.intgetNextRequestID()Gets the next unique request ID.<A extends Address>
TransportMapping<? super A>getTransport(A destAddress)Returns a transport mapping that can handle the supplied address.<A extends Address>
TransportMapping<? super A>getTransport(A destAddress, TransportType transportType)Returns a transport mapping that can handle the supplied address.java.util.Collection<TransportMapping<? extends Address>>getTransportMappings()Gets theCollectionof transport mappings in this message dispatcher.<A extends Address>
voidprocessMessage(TransportMapping<? super A> sourceTransport, A incomingAddress, java.nio.ByteBuffer wholeMessage, TransportStateReference tmStateReference)Process an incoming SNMP message.voidreleaseStateReference(int messageProcessingModel, PduHandle pduHandle)Release any state references associated with the suppliedPduHandlein the specified message processing model.voidremoveCommandResponder(CommandResponder listener)Removes a previously addedCommandResponderinstance from the message dispatcher.CounterListenerremoveCounterListener(CounterListener counterListener)Removes a previously addedCounterListenerfrom the dispatcher.voidremoveMessageProcessingModel(MessageProcessingModel model)Removes a previously addedMessageProcessingModelfrom the dispatcher.TransportMapping<?>removeTransportMapping(TransportMapping<? extends Address> transport)Removes a previously addedTransportMappingfrom the dispatcher.<A extends Address>
intreturnResponsePdu(int messageProcessingModel, int securityModel, byte[] securityName, int securityLevel, PDU pdu, int maxSizeResponseScopedPDU, StateReference<A> stateReference, StatusInformation statusInformation)Returns a response PDU to the sender of the corresponding request PDU.<A extends Address>
PduHandlesendPdu(Target<A> target, PDU pdu, boolean expectResponse)Sends a PDU to the supplied transport address.<A extends Address>
PduHandlesendPdu(TransportMapping<? super A> transportMapping, Target<A> target, PDU pdu, boolean expectResponse)Sends a PDU to the supplied transport address.<A extends Address>
PduHandlesendPdu(TransportMapping<? super A> transportMapping, Target<A> target, PDU pdu, boolean expectResponse, PduHandleCallback<PDU> callback)Sends a PDU to the supplied transport address and returns thePduHandlethat uniquely identifies the request as response after the request has been sent and optional, if aPduHandleCallbackis given, it returns also thePduHandlejust before the request is sent through the the callback interface.
-
-
-
Constructor Detail
-
MultiThreadedMessageDispatcher
public MultiThreadedMessageDispatcher(WorkerPool workerPool, MessageDispatcher decoratedDispatcher)
Creates a multi-threaded message dispatcher using the providedThreadPoolto concurrently process incoming messages that are forwarded to the supplied decoratedMessageDispatcher.- Parameters:
workerPool- aWorkerPoolinstance (that can be shared). The worker pool has to be stopped externally.decoratedDispatcher- the decoratedMessageDispatcherthat must be multi-threading safe.
-
-
Method Detail
-
getNextRequestID
public int getNextRequestID()
Description copied from interface:MessageDispatcherGets the next unique request ID. The returned ID is unique across the last 2^31-1 IDs generated by this message dispatcher.- Specified by:
getNextRequestIDin interfaceMessageDispatcher- Returns:
- an integer value in the range 1..2^31-1. The returned ID can be used to map responses to requests send through this message dispatcher.
-
addMessageProcessingModel
public void addMessageProcessingModel(MessageProcessingModel model)
Description copied from interface:MessageDispatcherAdds aMessageProcessingModelto the dispatcher. In order to support a specific SNMP protocol version, the message dispatcher needs a message processing model to process messages before they can be dispatched.- Specified by:
addMessageProcessingModelin interfaceMessageDispatcher- Parameters:
model- aMessageProcessingModelinstance.
-
removeMessageProcessingModel
public void removeMessageProcessingModel(MessageProcessingModel model)
Description copied from interface:MessageDispatcherRemoves a previously addedMessageProcessingModelfrom the dispatcher.- Specified by:
removeMessageProcessingModelin interfaceMessageDispatcher- Parameters:
model- aMessageProcessingModelinstance.
-
getMessageProcessingModel
public MessageProcessingModel getMessageProcessingModel(int messageProcessingModel)
Description copied from interface:MessageDispatcherGets theMessageProcessingModelfor the supplied message processing model ID.- Specified by:
getMessageProcessingModelin interfaceMessageDispatcher- Parameters:
messageProcessingModel- a message processing model ID (seeMessageProcessingModel.getID()).- Returns:
- a MessageProcessingModel instance if the ID is known, otherwise
null
-
addTransportMapping
public void addTransportMapping(TransportMapping<? extends Address> transport)
Description copied from interface:MessageDispatcherAdds aTransportMappingto the dispatcher. The transport mapping is used to send and receive messages to/from the network.- Specified by:
addTransportMappingin interfaceMessageDispatcher- Parameters:
transport- aTransportMappinginstance.
-
removeTransportMapping
public TransportMapping<?> removeTransportMapping(TransportMapping<? extends Address> transport)
Description copied from interface:MessageDispatcherRemoves a previously addedTransportMappingfrom the dispatcher.- Specified by:
removeTransportMappingin interfaceMessageDispatcher- Parameters:
transport- aTransportMappinginstance.- Returns:
- the
TransportMappinginstance if it could be successfully removed,nullotherwise.
-
addCounterListener
public void addCounterListener(CounterListener counterListener)
Adds aCounterListenerto the dispatcher. The counter listener is informed about errors during message dispatching.- Specified by:
addCounterListenerin interfaceMessageDispatcher- Parameters:
counterListener- aCounterListenerinstance.- Since:
- 3.0
-
removeCounterListener
public CounterListener removeCounterListener(CounterListener counterListener)
Removes a previously addedCounterListenerfrom the dispatcher.- Specified by:
removeCounterListenerin interfaceMessageDispatcher- Parameters:
counterListener- aCounterListenerinstance.- Returns:
- the
CounterListenerinstance if it could be successfully removed,nullotherwise. - Since:
- 3.0
-
getTransportMappings
public java.util.Collection<TransportMapping<? extends Address>> getTransportMappings()
Description copied from interface:MessageDispatcherGets theCollectionof transport mappings in this message dispatcher.- Specified by:
getTransportMappingsin interfaceMessageDispatcher- Returns:
- a Collection with the registered transport mappings.
-
addCommandResponder
public void addCommandResponder(CommandResponder listener)
Description copied from interface:MessageDispatcherAdds aCommandResponderinstance to the message dispatcher. Successfully processed SNMP messages will be presented to all command responder (in the order in which they have been added) until a responder uses theCommandResponderEvent.setProcessed(boolean processed)to set the processed status of the event totrue.- Specified by:
addCommandResponderin interfaceMessageDispatcher- Parameters:
listener- aCommandResponderinstance.
-
removeCommandResponder
public void removeCommandResponder(CommandResponder listener)
Description copied from interface:MessageDispatcherRemoves a previously addedCommandResponderinstance from the message dispatcher.- Specified by:
removeCommandResponderin interfaceMessageDispatcher- Parameters:
listener- aCommandResponderinstance.
-
sendPdu
public <A extends Address> PduHandle sendPdu(Target<A> target, PDU pdu, boolean expectResponse) throws MessageException
Description copied from interface:MessageDispatcherSends a PDU to the supplied transport address. This method behaves like a call toMessageDispatcher.sendPdu(TransportMapping transportMapping, Target target, PDU pdu, boolean expectResponse)withtransportMappingset tonull.- Specified by:
sendPduin interfaceMessageDispatcher- Type Parameters:
A- the targetAddresstype.- Parameters:
target- the target which identifies, transport address, message processing model, security model, security name and level.pdu- the SNMP Protocol Data UnitexpectResponse-trueif a response is expected and a state reference should be saved (if needed for the supplied message processing model).- Returns:
- an
PduHandlethat uniquely identifies this request. - Throws:
MessageException- if sending of the PDU failed.
-
sendPdu
public <A extends Address> PduHandle sendPdu(TransportMapping<? super A> transportMapping, Target<A> target, PDU pdu, boolean expectResponse) throws MessageException
Description copied from interface:MessageDispatcherSends a PDU to the supplied transport address.- Specified by:
sendPduin interfaceMessageDispatcher- Type Parameters:
A- theAddresstype.- Parameters:
transportMapping- theTransportMappingto be used to send the PDU. IftransportMappingisnullthe message dispatcher will determine the appropriate transport mapping for the given transport address.target- the target which identifies, transport address, message processing model, security model, security name and level.pdu- the SNMP Protocol Data UnitexpectResponse-trueif a response is expected and a state reference should be saved (if needed for the supplied message processing model).- Returns:
- an
PduHandlethat uniquely identifies this request. - Throws:
MessageException- if sending of the PDU failed.
-
sendPdu
public <A extends Address> PduHandle sendPdu(TransportMapping<? super A> transportMapping, Target<A> target, PDU pdu, boolean expectResponse, PduHandleCallback<PDU> callback) throws MessageException
Description copied from interface:MessageDispatcherSends a PDU to the supplied transport address and returns thePduHandlethat uniquely identifies the request as response after the request has been sent and optional, if aPduHandleCallbackis given, it returns also thePduHandlejust before the request is sent through the the callback interface.- Specified by:
sendPduin interfaceMessageDispatcher- Type Parameters:
A- the targetAddresstype.- Parameters:
transportMapping- theTransportMappingto be used to send the PDU. IftransportMappingisnullthe message dispatcher will determine the appropriate transport mapping for the given transport address.target- the target which identifies, transport address, message processing model, security model, security name and level.pdu- the SNMP Protocol Data UnitexpectResponse-trueif a response is expected and a state reference should be saved (if needed for the supplied message processing model).callback- an optional callback instance that is informed (if notnull) about the newly assigned PduHandle just before the message is sent out.- Returns:
- an
PduHandlethat uniquely identifies this request. - Throws:
MessageException- if sending of the PDU failed.
-
returnResponsePdu
public <A extends Address> int returnResponsePdu(int messageProcessingModel, int securityModel, byte[] securityName, int securityLevel, PDU pdu, int maxSizeResponseScopedPDU, StateReference<A> stateReference, StatusInformation statusInformation) throws MessageException
Description copied from interface:MessageDispatcherReturns a response PDU to the sender of the corresponding request PDU.- Specified by:
returnResponsePduin interfaceMessageDispatcher- Type Parameters:
A- theAddresstype.- Parameters:
messageProcessingModel- the message processing model, seeMessageProcessingModelsecurityModel- the security model to use, seeSecurityModelsecurityName- the security namesecurityLevel- the security level for the message to be returned, seeSecurityLevelpdu- the protocol data unit (PDU) to send.maxSizeResponseScopedPDU- the maximum size agreed for the response scoped PDU.stateReference- the state reference to be used.statusInformation- the status information to be used.- Returns:
- an MP error status or
SnmpConstants.SNMP_MP_OKif the operation was successful. - Throws:
MessageException- if message processing fails with a fatal error.
-
releaseStateReference
public void releaseStateReference(int messageProcessingModel, PduHandle pduHandle)Description copied from interface:MessageDispatcherRelease any state references associated with the suppliedPduHandlein the specified message processing model.- Specified by:
releaseStateReferencein interfaceMessageDispatcher- Parameters:
messageProcessingModel- a message processing model ID.pduHandle- thePduHandlethat identifies a confirmed class message.- See Also:
MessageProcessingModel
-
getTransport
public <A extends Address> TransportMapping<? super A> getTransport(A destAddress)
Description copied from interface:MessageDispatcherReturns a transport mapping that can handle the supplied address.- Specified by:
getTransportin interfaceMessageDispatcher- Type Parameters:
A- theAddresstype to get aTransportMappingfor.- Parameters:
destAddress- an Address instance.- Returns:
- a
TransportMappinginstance that can be used to sent a SNMP message todestAddressornullif such a transport mapping does not exists.
-
getTransport
public <A extends Address> TransportMapping<? super A> getTransport(A destAddress, TransportType transportType)
Description copied from interface:MessageDispatcherReturns a transport mapping that can handle the supplied address.- Specified by:
getTransportin interfaceMessageDispatcher- Type Parameters:
A- theAddresstype to get aTransportMappingfor.- Parameters:
destAddress- an Address instance.transportType- specifies the SNMP application type that will use the returned transport mapping.transportTypemust be eitherTransportType.receiverorTransportType.sender. IfTransportType.anyis provided anIllegalArgumentExceptionwill be thrown.- Returns:
- a
TransportMappinginstance that can be used to sent a SNMP message todestAddressornullif such a transport mapping does not exists.
-
processMessage
public <A extends Address> void processMessage(TransportMapping<? super A> sourceTransport, A incomingAddress, java.nio.ByteBuffer wholeMessage, TransportStateReference tmStateReference)
Description copied from interface:MessageDispatcherProcess an incoming SNMP message. The message is processed and dispatched according to the message's content, the message processing models, and the command responder available to the dispatcher.- Specified by:
processMessagein interfaceMessageDispatcher- Specified by:
processMessagein interfaceTransportListener- Type Parameters:
A- theAddresstype.- Parameters:
sourceTransport- aTransportMappinginstance denoting the transport that received the message and that will be used to send any responses to this message. ThesourceTransporthas to support theincomingAddress's implementation class.incomingAddress- theAddressfrom which the message has been received.wholeMessage- anByteBuffercontaining the received SNMP message.tmStateReference- the transport model state reference as defined by RFC 5590.
-
-