java.lang.Object
org.snmp4j.util.MultiThreadedMessageDispatcher
- All Implemented Interfaces:
MessageDispatcher
,TransportListener
The
MultiThreadedMessageDispatcher
class is a decorator
for any MessageDispatcher
instances that processes incoming
message with a supplied ThreadPool
. The processing is thus
concurrent on up to the size of the supplied thread pool threads.
In contrast to a MessageDispatcherImpl
a
MultiThreadedMessageDispatcher
copies the incoming
ByteBuffer
for processMessage(TransportMapping, Address, ByteBuffer, TransportStateReference)
to allow concurrent processing of the buffer.
- Since:
- 1.0.2
- Version:
- 3.5.0
- Author:
- Frank Fock
-
Constructor Summary
ConstructorDescriptionMultiThreadedMessageDispatcher
(WorkerPool workerPool, MessageDispatcher decoratedDispatcher) Creates a multithreaded message dispatcher using the providedThreadPool
to concurrently process incoming messages that are forwarded to the supplied decoratedMessageDispatcher
. -
Method Summary
Modifier and TypeMethodDescriptionvoid
addCommandResponder
(CommandResponder listener) Adds aCommandResponder
instance to the message dispatcher.void
addCounterListener
(CounterListener counterListener) Adds aCounterListener
to the dispatcher.void
Adds aMessageProcessingModel
to the dispatcher.void
addTransportMapping
(TransportMapping<? extends Address> transport) Adds aTransportMapping
to the dispatcher.Returns the wrapped dispatcher.getMessageProcessingModel
(int messageProcessingModel) Gets theMessageProcessingModel
for the supplied message processing model ID.int
Gets the next unique request ID.<A extends Address>
TransportMapping<? super A>getTransport
(A destAddress) Deprecated.<A extends Address>
TransportMapping<? super A>getTransport
(A destAddress, TransportType transportType) Returns a transport mapping that can handle the supplied address.Collection<TransportMapping<? extends Address>>
Gets theCollection
of transport mappings in this message dispatcher.<A extends Address>
voidprocessMessage
(TransportMapping<? super A> sourceTransport, A incomingAddress, ByteBuffer wholeMessage, TransportStateReference tmStateReference) Process an incoming SNMP message.void
releaseStateReference
(int messageProcessingModel, PduHandle pduHandle) Release any state references associated with the suppliedPduHandle
in the specified message processing model.void
removeCommandResponder
(CommandResponder listener) Removes a previously addedCommandResponder
instance from the message dispatcher.removeCounterListener
(CounterListener counterListener) Removes a previously addedCounterListener
from the dispatcher.void
Removes a previously addedMessageProcessingModel
from the dispatcher.removeTransportMapping
(TransportMapping<? extends Address> transport) Removes a previously addedTransportMapping
from 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.Sends a PDU to the supplied transport address.sendPdu
(TransportMapping<? super A> transportMapping, Target<A> target, PDU pdu, boolean expectResponse) Sends a PDU to the supplied transport address.sendPdu
(TransportMapping<? super A> transportMapping, Target<A> target, PDU pdu, boolean expectResponse, PduHandleCallback<PDU> callback) Sends a PDU to the supplied transport address and returns thePduHandle
that uniquely identifies the request as response after the request has been sent and optional, if aPduHandleCallback
is given, it returns also thePduHandle
just before the request is sent through the the callback interface.void
stop()
Stops this message dispatcher and free all resources (i.e.
-
Constructor Details
-
MultiThreadedMessageDispatcher
Creates a multithreaded message dispatcher using the providedThreadPool
to concurrently process incoming messages that are forwarded to the supplied decoratedMessageDispatcher
.- Parameters:
workerPool
- aWorkerPool
instance (that can be shared). The worker pool has to be stopped externally.decoratedDispatcher
- the decoratedMessageDispatcher
that must be multi-threading safe.
-
-
Method Details
-
getDispatcher
Returns the wrapped dispatcher.- Returns:
- the
MessageDispatcher
instance called by the threads of the worker pool. - Since:
- 3.5.0
-
getNextRequestID
public int getNextRequestID()Description copied from interface:MessageDispatcher
Gets the next unique request ID. The returned ID is unique across the last 2^31-1 IDs generated by this message dispatcher.- Specified by:
getNextRequestID
in 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
Description copied from interface:MessageDispatcher
Adds aMessageProcessingModel
to 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:
addMessageProcessingModel
in interfaceMessageDispatcher
- Parameters:
model
- aMessageProcessingModel
instance.
-
removeMessageProcessingModel
Description copied from interface:MessageDispatcher
Removes a previously addedMessageProcessingModel
from the dispatcher.- Specified by:
removeMessageProcessingModel
in interfaceMessageDispatcher
- Parameters:
model
- aMessageProcessingModel
instance.
-
getMessageProcessingModel
Description copied from interface:MessageDispatcher
Gets theMessageProcessingModel
for the supplied message processing model ID.- Specified by:
getMessageProcessingModel
in interfaceMessageDispatcher
- Parameters:
messageProcessingModel
- a message processing model ID (seeMessageProcessingModel.getID()
).- Returns:
- a MessageProcessingModel instance if the ID is known, otherwise
null
-
addTransportMapping
Description copied from interface:MessageDispatcher
Adds aTransportMapping
to the dispatcher. The transport mapping is used to send and receive messages to/from the network.- Specified by:
addTransportMapping
in interfaceMessageDispatcher
- Parameters:
transport
- aTransportMapping
instance.
-
removeTransportMapping
Description copied from interface:MessageDispatcher
Removes a previously addedTransportMapping
from the dispatcher.- Specified by:
removeTransportMapping
in interfaceMessageDispatcher
- Parameters:
transport
- aTransportMapping
instance.- Returns:
- the
TransportMapping
instance if it could be successfully removed,null
otherwise.
-
addCounterListener
Adds aCounterListener
to the dispatcher. The counter listener is informed about errors during message dispatching.- Specified by:
addCounterListener
in interfaceMessageDispatcher
- Parameters:
counterListener
- aCounterListener
instance.- Since:
- 3.0
-
removeCounterListener
Removes a previously addedCounterListener
from the dispatcher.- Specified by:
removeCounterListener
in interfaceMessageDispatcher
- Parameters:
counterListener
- aCounterListener
instance.- Returns:
- the
CounterListener
instance if it could be successfully removed,null
otherwise. - Since:
- 3.0
-
getTransportMappings
Description copied from interface:MessageDispatcher
Gets theCollection
of transport mappings in this message dispatcher.- Specified by:
getTransportMappings
in interfaceMessageDispatcher
- Returns:
- a Collection with the registered transport mappings.
-
addCommandResponder
Description copied from interface:MessageDispatcher
Adds aCommandResponder
instance 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:
addCommandResponder
in interfaceMessageDispatcher
- Parameters:
listener
- aCommandResponder
instance.
-
removeCommandResponder
Description copied from interface:MessageDispatcher
Removes a previously addedCommandResponder
instance from the message dispatcher.- Specified by:
removeCommandResponder
in interfaceMessageDispatcher
- Parameters:
listener
- aCommandResponder
instance.
-
sendPdu
public <A extends Address> PduHandle sendPdu(Target<A> target, PDU pdu, boolean expectResponse) throws MessageException Description copied from interface:MessageDispatcher
Sends a PDU to the supplied transport address. This method behaves like a call toMessageDispatcher.sendPdu(TransportMapping transportMapping, Target target, PDU pdu, boolean expectResponse)
withtransportMapping
set tonull
.- Specified by:
sendPdu
in interfaceMessageDispatcher
- Type Parameters:
A
- the targetAddress
type.- Parameters:
target
- the target which identifies, transport address, message processing model, security model, security name and level.pdu
- the SNMP Protocol Data UnitexpectResponse
-true
if a response is expected and a state reference should be saved (if needed for the supplied message processing model).- Returns:
- an
PduHandle
that 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:MessageDispatcher
Sends a PDU to the supplied transport address.- Specified by:
sendPdu
in interfaceMessageDispatcher
- Type Parameters:
A
- theAddress
type.- Parameters:
transportMapping
- theTransportMapping
to be used to send the PDU. IftransportMapping
isnull
the 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
-true
if a response is expected and a state reference should be saved (if needed for the supplied message processing model).- Returns:
- an
PduHandle
that 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:MessageDispatcher
Sends a PDU to the supplied transport address and returns thePduHandle
that uniquely identifies the request as response after the request has been sent and optional, if aPduHandleCallback
is given, it returns also thePduHandle
just before the request is sent through the the callback interface.- Specified by:
sendPdu
in interfaceMessageDispatcher
- Type Parameters:
A
- the targetAddress
type.- Parameters:
transportMapping
- theTransportMapping
to be used to send the PDU. IftransportMapping
isnull
the 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
-true
if 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
PduHandle
that 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:MessageDispatcher
Returns a response PDU to the sender of the corresponding request PDU.- Specified by:
returnResponsePdu
in interfaceMessageDispatcher
- Type Parameters:
A
- theAddress
type.- Parameters:
messageProcessingModel
- the message processing model, seeMessageProcessingModel
securityModel
- the security model to use, seeSecurityModel
securityName
- the security namesecurityLevel
- the security level for the message to be returned, seeSecurityLevel
pdu
- 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_OK
if the operation was successful. - Throws:
MessageException
- if message processing fails with a fatal error.
-
releaseStateReference
Description copied from interface:MessageDispatcher
Release any state references associated with the suppliedPduHandle
in the specified message processing model.- Specified by:
releaseStateReference
in interfaceMessageDispatcher
- Parameters:
messageProcessingModel
- a message processing model ID.pduHandle
- thePduHandle
that identifies a confirmed class message.- See Also:
-
getTransport
Deprecated.Description copied from interface:MessageDispatcher
Returns a transport mapping that can handle the supplied address.- Specified by:
getTransport
in interfaceMessageDispatcher
- Type Parameters:
A
- theAddress
type to get aTransportMapping
for.- Parameters:
destAddress
- an Address instance.- Returns:
- a
TransportMapping
instance that can be used to sent a SNMP message todestAddress
ornull
if such a transport mapping does not exists.
-
getTransport
public <A extends Address> TransportMapping<? super A> getTransport(A destAddress, TransportType transportType) Description copied from interface:MessageDispatcher
Returns a transport mapping that can handle the supplied address.- Specified by:
getTransport
in interfaceMessageDispatcher
- Type Parameters:
A
- theAddress
type to get aTransportMapping
for.- Parameters:
destAddress
- an Address instance.transportType
- specifies the SNMP application type that will use the returned transport mapping.transportType
must be eitherTransportType.receiver
orTransportType.sender
. IfTransportType.any
is provided anIllegalArgumentException
will be thrown.- Returns:
- a
TransportMapping
instance that can be used to sent a SNMP message todestAddress
ornull
if such a transport mapping does not exists.
-
processMessage
public <A extends Address> void processMessage(TransportMapping<? super A> sourceTransport, A incomingAddress, ByteBuffer wholeMessage, TransportStateReference tmStateReference) Description copied from interface:MessageDispatcher
Process 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:
processMessage
in interfaceMessageDispatcher
- Specified by:
processMessage
in interfaceTransportListener
- Type Parameters:
A
- theAddress
type.- Parameters:
sourceTransport
- aTransportMapping
instance denoting the transport that received the message and that will be used to send any responses to this message. ThesourceTransport
has to support theincomingAddress
's implementation class.incomingAddress
- theAddress
from which the message has been received.wholeMessage
- anByteBuffer
containing the received SNMP message.tmStateReference
- the transport model state reference as defined by RFC 5590.
-
stop
public void stop()Description copied from interface:MessageDispatcher
Stops this message dispatcher and free all resources (i.e. stop/join internal threads, etc.)- Specified by:
stop
in interfaceMessageDispatcher
-