Package org.jivesoftware.smack
Class StanzaCollector
java.lang.Object
org.jivesoftware.smack.StanzaCollector
- All Implemented Interfaces:
AutoCloseable
Provides a mechanism to collect Stanzas into a result queue that pass a
specified filter/matcher. The collector lets you perform blocking and polling
operations on the result queue. So, a StanzaCollector is more suitable to
use than a
StanzaListener when you need to wait for a specific
result.
Each stanza collector will queue up a configured number of packets for processing before
older packets are automatically dropped. The default number is retrieved by
SmackConfiguration.getStanzaCollectorSize().
-
Nested Class Summary
Nested Classes -
Method Summary
Modifier and TypeMethodDescriptionvoidcancel()Explicitly cancels the stanza collector so that no more results are queued up.voidclose()intGet the number of collected stanzas this stanza collector has collected so far.Return a list of all collected stanzas.Returns the stanza filter associated with this stanza collector.Get a new stanza collector configuration instance.<P extends Stanza>
PReturns the next available packet.<P extends Stanza>
PnextResult(long timeout) Returns the next available stanza.<P extends Stanza>
PReturns the next available packet.<P extends Stanza>
PReturns the next available stanza.<P extends Stanza>
PnextResultOrThrow(long timeout) Returns the next available stanza.<P extends Stanza>
PPolls to see if a stanza is currently available and returns it, or immediately returnsnullif no packets are currently in the result queue.<P extends Stanza>
PPolls to see if a stanza is currently available and returns it, or immediately returnsnullif no packets are currently in the result queue.toString()
-
Method Details
-
cancel
public void cancel()Explicitly cancels the stanza collector so that no more results are queued up. Once a stanza collector has been cancelled, it cannot be re-enabled. Instead, a new stanza collector must be created. -
getStanzaFilter
Returns the stanza filter associated with this stanza collector. The stanza filter is used to determine what stanzas are queued as results.- Returns:
- the stanza filter.
-
pollResult
Polls to see if a stanza is currently available and returns it, or immediately returnsnullif no packets are currently in the result queue.- Type Parameters:
P- type of the result stanza.- Returns:
- the next stanza result, or
nullif there are no more results.
-
pollResultOrThrow
Polls to see if a stanza is currently available and returns it, or immediately returnsnullif no packets are currently in the result queue.Throws an XMPPErrorException in case the polled stanzas did contain an XMPPError.
- Type Parameters:
P- type of the result stanza.- Returns:
- the next available packet.
- Throws:
XMPPException.XMPPErrorException- in case an error response.
-
nextResultBlockForever
Returns the next available packet. The method call will block (not return) until a stanza is available.- Type Parameters:
P- type of the result stanza.- Returns:
- the next available packet.
- Throws:
InterruptedException- if the calling thread was interrupted.
-
nextResult
Returns the next available packet. The method call will block until the connection's default timeout has elapsed.- Type Parameters:
P- type of the result stanza.- Returns:
- the next available packet.
- Throws:
InterruptedException- if the calling thread was interrupted.
-
nextResult
Returns the next available stanza. The method call will block (not return) until a stanza is available or thetimeouthas elapsed or if the connection was terminated because of an error. If the timeout elapses without a result or if there was an connection error,nullwill be returned.- Type Parameters:
P- type of the result stanza.- Parameters:
timeout- the timeout in milliseconds.- Returns:
- the next available stanza or
nullon timeout or connection error. - Throws:
InterruptedException- if the calling thread was interrupted.
-
nextResultOrThrow
public <P extends Stanza> P nextResultOrThrow() throws SmackException.NoResponseException, XMPPException.XMPPErrorException, InterruptedException, SmackException.NotConnectedExceptionReturns the next available stanza. The method in equivalent tonextResultOrThrow(long)where the timeout argument is the default reply timeout of the connection associated with this collector.- Type Parameters:
P- type of the result stanza.- Returns:
- the next available stanza.
- Throws:
XMPPException.XMPPErrorException- in case an error response was received.SmackException.NoResponseException- if there was no response from the server.InterruptedException- if the calling thread was interrupted.SmackException.NotConnectedException- if the XMPP connection is not connected.- See Also:
-
nextResultOrThrow
public <P extends Stanza> P nextResultOrThrow(long timeout) throws SmackException.NoResponseException, XMPPException.XMPPErrorException, InterruptedException, SmackException.NotConnectedException Returns the next available stanza. The method call will block until a stanza is available or thetimeouthas elapsed. This method does also cancel the collector in every case.Three things can happen when waiting for an response:
- A result response arrives.
- An error response arrives.
- An timeout occurs.
- The thread is interrupted
in which this method will
- return with the result.
- throw an
XMPPException.XMPPErrorException. - throw an
SmackException.NoResponseException. - throw an
InterruptedException.
Additionally the method will throw a
SmackException.NotConnectedExceptionif no response was received and the connection got disconnected.- Type Parameters:
P- type of the result stanza.- Parameters:
timeout- the amount of time to wait for the next stanza in milliseconds.- Returns:
- the next available stanza.
- Throws:
SmackException.NoResponseException- if there was no response from the server.XMPPException.XMPPErrorException- in case an error response was received.InterruptedException- if the calling thread was interrupted.SmackException.NotConnectedException- if there was no response and the connection got disconnected.
-
getCollectedStanzasAfterCancelled
Return a list of all collected stanzas. This method must be invoked after the collector has been cancelled.- Returns:
- a list of collected stanzas.
- Since:
- 4.3.0
-
getCollectedCount
public int getCollectedCount()Get the number of collected stanzas this stanza collector has collected so far.- Returns:
- the count of collected stanzas.
- Since:
- 4.1
-
toString
-
newConfiguration
Get a new stanza collector configuration instance.- Returns:
- a new stanza collector configuration.
-
close
public void close()- Specified by:
closein interfaceAutoCloseable
-