public interface XMPPConnection
XMPPTCPConnection or XMPPBOSHConnection). To create a connection to an XMPP server
a simple usage of this API might look like the following:
// Create a connection to the igniterealtime.org XMPP server.
XMPPTCPConnection con = new XMPPTCPConnection("igniterealtime.org");
// Connect to the server
con.connect();
// Most servers require you to login before performing other tasks.
con.login("jsmith", "mypass");
// Start a new conversation with John Doe and send him a message.
ChatManager chatManager = ChatManager.getInstanceFor(con);
chatManager.addIncomingListener(new IncomingChatMessageListener() {
public void newIncomingMessage(EntityBareJid from, Message message, Chat chat) {
// Print out any messages we get back to standard out.
System.out.println("Received message: " + message);
}
});
Chat chat = chatManager.chatWith("jdoe@igniterealtime.org");
chat.send("Howdy!");
// Disconnect from the server
con.disconnect();
Note that the XMPPConnection interface does intentionally not declare any methods that manipulate
the connection state, e.g. connect(), disconnect(). You should use the
most specific connection type, e.g. XMPPTCPConnection as declared type and use the
XMPPConnection interface when you don't need to manipulate the connection state.
XMPPConnections can be reused between connections. This means that an Connection may be connected, disconnected and then connected again. Listeners of the XMPPConnection will be retained across connections.
addAsyncStanzaListener(StanzaListener, StanzaFilter)addSyncStanzaListener(StanzaListener, StanzaFilter)addStanzaListener(StanzaListener, StanzaFilter)Asynchronous callbacks are run decoupled from the connections main event loop. Hence a callback triggered by stanza B may (appear to) invoked before a callback triggered by stanza A, even though stanza A arrived before B.
Synchronous callbacks are invoked concurrently, but it is ensured that the same callback is never run concurrently and that they are executed in order. That is, if both stanza A and B trigger the same callback, and A arrives before B, then the callback will be invoked with A first, and then B. Furthermore, those callbacks are not executed within the main loop. However it is still advisable that those callbacks do not block or only block briefly.
Other callbacks are run synchronous to the main event loop of a connection and are executed within the main loop. This means that if such a callback blocks, the main event loop also blocks, which can easily cause deadlocks. Therefore, you should avoid using those callbacks unless you know what you are doing.
| Modifier and Type | Interface and Description |
|---|---|
static class |
XMPPConnection.FromMode |
| Modifier and Type | Method and Description |
|---|---|
void |
addAsyncStanzaListener(StanzaListener stanzaListener,
StanzaFilter stanzaFilter)
Registers an asynchronous stanza listener with this connection.
|
void |
addConnectionListener(ConnectionListener connectionListener)
Adds a connection listener to this connection that will be notified when
the connection closes or fails.
|
void |
addMessageInterceptor(Consumer<MessageBuilder> messageInterceptor,
Predicate<Message> messageFilter)
Registers a stanza interceptor with this connection.
|
void |
addOneTimeSyncCallback(StanzaListener callback,
StanzaFilter stanzaFilter)
Add a callback that is called exactly once and synchronously with the incoming stanza that matches the given
stanza filter.
|
void |
addPresenceInterceptor(Consumer<PresenceBuilder> presenceInterceptor,
Predicate<Presence> presenceFilter)
Registers a stanza interceptor with this connection.
|
void |
addStanzaInterceptor(StanzaListener stanzaInterceptor,
StanzaFilter stanzaFilter)
Deprecated.
|
void |
addStanzaListener(StanzaListener stanzaListener,
StanzaFilter stanzaFilter)
Registers a stanza listener with this connection.
|
void |
addStanzaSendingListener(StanzaListener stanzaListener,
StanzaFilter stanzaFilter)
Registers a stanza listener with this connection.
|
void |
addSyncStanzaListener(StanzaListener stanzaListener,
StanzaFilter stanzaFilter)
Registers a synchronous stanza listener with this connection.
|
StanzaCollector |
createStanzaCollector(StanzaCollector.Configuration configuration)
Create a new stanza collector with the given stanza collector configuration.
|
StanzaCollector |
createStanzaCollector(StanzaFilter stanzaFilter)
Creates a new stanza collector for this connection.
|
StanzaCollector |
createStanzaCollectorAndSend(IQ request)
Creates a new stanza collector collecting IQ responses that are replies to the IQ
request. |
StanzaCollector |
createStanzaCollectorAndSend(StanzaFilter stanzaFilter,
Stanza stanza)
Creates a new stanza collector for this connection.
|
int |
getConnectionCounter()
Get the connection counter of this XMPPConnection instance.
|
default <F extends FullyQualifiedElement> |
getFeature(java.lang.Class<F> featureClass)
Get the feature stanza extensions for a given stream feature of the
server, or
null if the server doesn't support that feature. |
<F extends FullyQualifiedElement> |
getFeature(javax.xml.namespace.QName qname)
Get the feature stanza extensions for a given stream feature of the
server, or
null if the server doesn't support that feature. |
default <F extends FullyQualifiedElement> |
getFeature(java.lang.String element,
java.lang.String namespace)
Deprecated.
use
getFeature(Class) instead. |
XMPPConnection.FromMode |
getFromMode()
Get the currently active FromMode.
|
java.lang.String |
getHost()
Returns the host name of the server where the XMPP server is running.
|
long |
getLastStanzaReceived()
Returns the timestamp in milliseconds when the last stanza was received.
|
int |
getPort()
Returns the port number of the XMPP server for this connection.
|
long |
getReplyTimeout()
Returns the current value of the reply timeout in milliseconds for request for this
XMPPConnection instance.
|
StanzaFactory |
getStanzaFactory() |
java.lang.String |
getStreamId()
Returns the stream ID for this connection, which is the value set by the server
when opening an XMPP stream.
|
org.jxmpp.jid.EntityFullJid |
getUser()
Returns the full XMPP address of the user that is logged in to the connection or
null if not logged in yet. |
org.jxmpp.jid.DomainBareJid |
getXMPPServiceDomain()
Returns the XMPP Domain of the service provided by the XMPP server and used for this connection.
|
boolean |
hasFeature(javax.xml.namespace.QName qname)
Return true if the server supports the given stream feature.
|
default boolean |
hasFeature(java.lang.String element,
java.lang.String namespace)
Return true if the server supports the given stream feature.
|
boolean |
isAnonymous()
Returns true if currently authenticated anonymously.
|
boolean |
isAuthenticated()
Returns true if currently authenticated by successfully calling the login method.
|
boolean |
isConnected()
Returns true if currently connected to the XMPP server.
|
boolean |
isSecureConnection()
Returns true if the connection to the server has successfully negotiated encryption.
|
boolean |
isUsingCompression()
Returns true if network traffic is being compressed.
|
IQRequestHandler |
registerIQRequestHandler(IQRequestHandler iqRequestHandler)
Register an IQ request handler with this connection.
|
boolean |
removeAsyncStanzaListener(StanzaListener stanzaListener)
Removes an asynchronous stanza listener for received stanzas from this connection.
|
void |
removeConnectionListener(ConnectionListener connectionListener)
Removes a connection listener from this connection.
|
void |
removeMessageInterceptor(Consumer<MessageBuilder> messageInterceptor)
Removes a message interceptor.
|
void |
removePresenceInterceptor(Consumer<PresenceBuilder> presenceInterceptor)
Removes a presence interceptor.
|
void |
removeStanzaCollector(StanzaCollector collector)
Remove a stanza collector of this connection.
|
void |
removeStanzaInterceptor(StanzaListener stanzaInterceptor)
Deprecated.
|
boolean |
removeStanzaListener(StanzaListener stanzaListener)
Removes a stanza listener for received stanzas from this connection.
|
void |
removeStanzaSendingListener(StanzaListener stanzaListener)
Removes a stanza listener for sending stanzas from this connection.
|
boolean |
removeSyncStanzaListener(StanzaListener stanzaListener)
Removes a stanza listener for received stanzas from this connection.
|
<S extends Stanza> |
sendAsync(S stanza,
StanzaFilter replyFilter)
Send a stanza asynchronously, waiting for exactly one response stanza using the given reply filter.
|
<S extends Stanza> |
sendAsync(S stanza,
StanzaFilter replyFilter,
long timeout)
Send a stanza asynchronously, waiting for exactly one response stanza using the given reply filter.
|
<I extends IQ> |
sendIqRequestAndWaitForResponse(IQ request)
Send an IQ request and wait for the response.
|
SmackFuture<IQ,java.lang.Exception> |
sendIqRequestAsync(IQ request)
Send an IQ request asynchronously.
|
SmackFuture<IQ,java.lang.Exception> |
sendIqRequestAsync(IQ request,
long timeout)
Send an IQ request asynchronously.
|
void |
sendNonza(Nonza nonza)
Send a Nonza.
|
void |
sendStanza(Stanza stanza)
Sends the specified stanza to the server.
|
void |
setFromMode(XMPPConnection.FromMode fromMode)
Set the FromMode for this connection instance.
|
void |
setReplyTimeout(long timeout)
Set the stanza reply timeout in milliseconds.
|
boolean |
trySendStanza(Stanza stanza)
Try to send the given stanza.
|
boolean |
trySendStanza(Stanza stanza,
long timeout,
java.util.concurrent.TimeUnit unit)
Try to send the given stanza.
|
IQRequestHandler |
unregisterIQRequestHandler(IQRequestHandler iqRequestHandler)
Convenience method for
unregisterIQRequestHandler(String, String, org.jivesoftware.smack.packet.IQ.Type). |
IQRequestHandler |
unregisterIQRequestHandler(java.lang.String element,
java.lang.String namespace,
IQ.Type type)
Unregister an IQ request handler with this connection.
|
org.jxmpp.jid.DomainBareJid getXMPPServiceDomain()
java.lang.String getHost()
int getPort()
org.jxmpp.jid.EntityFullJid getUser()
null if not logged in yet. An XMPP address is in the form
username@server/resource.java.lang.String getStreamId()
null if not connected to the server.null if
not connected to the server.boolean isConnected()
boolean isAuthenticated()
boolean isAnonymous()
boolean isSecureConnection()
boolean isUsingCompression()
StanzaFactory getStanzaFactory()
void sendStanza(Stanza stanza) throws SmackException.NotConnectedException, java.lang.InterruptedException
stanza - the stanza to send.SmackException.NotConnectedException - if the connection is not connected.java.lang.InterruptedException - if the calling thread was interrupted.boolean trySendStanza(Stanza stanza) throws SmackException.NotConnectedException
true if the stanza was successfully put into the outgoing stanza
queue, otherwise, if false is returned, the stanza could not be scheduled for sending (for example
because the outgoing element queue is full). Note that this means that the stanza possibly was not put onto the
wire, even if true is returned, it just has been successfully scheduled for sending.
Note: Implementations are not required to provide that functionality. In that case this method is mapped
to sendStanza(Stanza) and will possibly block until the stanza could be scheduled for sending.
stanza - the stanza to send.true if the stanza was successfully scheduled to be send, false otherwise.SmackException.NotConnectedException - if the connection is not connected.boolean trySendStanza(Stanza stanza, long timeout, java.util.concurrent.TimeUnit unit) throws SmackException.NotConnectedException, java.lang.InterruptedException
true if the stanza was successfully put into the outgoing stanza
queue within the given timeout period, otherwise, if false is returned, the stanza could not be scheduled
for sending (for example because the outgoing element queue is full). Note that this means that the stanza
possibly was not put onto the wire, even if true is returned, it just has been successfully scheduled for
sending.
Note: Implementations are not required to provide that functionality. In that case this method is mapped
to sendStanza(Stanza) and will possibly block until the stanza could be scheduled for sending.
stanza - the stanza to send.timeout - how long to wait before giving up, in units of unit.unit - a TimeUnit determining how to interpret the timeout parameter.true if the stanza was successfully scheduled to be send, false otherwise.SmackException.NotConnectedException - if the connection is not connected.java.lang.InterruptedException - if the calling thread was interrupted.void sendNonza(Nonza nonza) throws SmackException.NotConnectedException, java.lang.InterruptedException
This method is not meant for end-user usage! It allows sending plain stream elements, which should not be done by a user manually. Doing so may result in a unstable or unusable connection. Certain Smack APIs use this method to send plain stream elements.
nonza - the Nonza to send.SmackException.NotConnectedException - if the XMPP connection is not connected.java.lang.InterruptedException - if the calling thread was interrupted.void addConnectionListener(ConnectionListener connectionListener)
connectionListener - a connection listener.void removeConnectionListener(ConnectionListener connectionListener)
connectionListener - a connection listener.<I extends IQ> I sendIqRequestAndWaitForResponse(IQ request) throws SmackException.NoResponseException, XMPPException.XMPPErrorException, SmackException.NotConnectedException, java.lang.InterruptedException
I - the type of the expected result IQ.request - the IQ requestSmackException.NoResponseException - if there was no response from the remote entity.XMPPException.XMPPErrorException - if there was an XMPP error returned.SmackException.NotConnectedException - if the XMPP connection is not connected.java.lang.InterruptedException - if the calling thread was interrupted.StanzaCollector createStanzaCollectorAndSend(IQ request) throws SmackException.NotConnectedException, java.lang.InterruptedException
request.
Does also send the request IQ. The stanza filter for the collector is an
IQReplyFilter, guaranteeing that stanza id and JID in the 'from' address have
expected values.request - the IQ request to filter responses fromSmackException.NotConnectedException - if the XMPP connection is not connected.java.lang.InterruptedException - if the calling thread was interrupted.StanzaCollector createStanzaCollectorAndSend(StanzaFilter stanzaFilter, Stanza stanza) throws SmackException.NotConnectedException, java.lang.InterruptedException
StanzaListener when you need to wait for
a specific result.stanzaFilter - the stanza filter to use.stanza - the stanza to send right after the collector got createdjava.lang.InterruptedException - if the calling thread was interrupted.SmackException.NotConnectedException - if the XMPP connection is not connected.StanzaCollector createStanzaCollector(StanzaFilter stanzaFilter)
StanzaListener
when you need to wait for a specific result.
Note: If you send a Stanza right after using this method, then
consider using
createStanzaCollectorAndSend(StanzaFilter, Stanza) instead.
Otherwise make sure cancel the StanzaCollector in every case, e.g. even
if an exception is thrown, or otherwise you may leak the StanzaCollector.
stanzaFilter - the stanza filter to use.StanzaCollector createStanzaCollector(StanzaCollector.Configuration configuration)
Please make sure to cancel the collector when it is no longer required. See also
createStanzaCollector(StanzaFilter).
configuration - the stanza collector configuration.void removeStanzaCollector(StanzaCollector collector)
collector - a stanza collectors which was created for this connection.void addStanzaListener(StanzaListener stanzaListener, StanzaFilter stanzaFilter)
stanzaListener - the stanza listener to notify of new received stanzas.stanzaFilter - the stanza filter to use.boolean removeStanzaListener(StanzaListener stanzaListener)
stanzaListener - the stanza listener to remove.void addSyncStanzaListener(StanzaListener stanzaListener, StanzaFilter stanzaFilter)
Important: This stanza listeners will be called in the same single thread that processes all
incoming stanzas. Only use this kind of stanza filter if it does not perform any XMPP activity that waits for a
response. Consider using addAsyncStanzaListener(StanzaListener, StanzaFilter) when possible, i.e. when
the invocation order doesn't have to be the same as the order of the arriving stanzas. If the order of the
arriving stanzas, consider using a StanzaCollector when possible.
stanzaListener - the stanza listener to notify of new received stanzas.stanzaFilter - the stanza filter to use.addStanzaInterceptor(StanzaListener, StanzaFilter)boolean removeSyncStanzaListener(StanzaListener stanzaListener)
stanzaListener - the stanza listener to remove.void addAsyncStanzaListener(StanzaListener stanzaListener, StanzaFilter stanzaFilter)
Unlike addAsyncStanzaListener(StanzaListener, StanzaFilter) stanza listeners added with this method will be
invoked asynchronously in their own thread. Use this method if the order of the stanza listeners must not depend
on the order how the stanzas where received.
stanzaListener - the stanza listener to notify of new received stanzas.stanzaFilter - the stanza filter to use.addStanzaInterceptor(StanzaListener, StanzaFilter)boolean removeAsyncStanzaListener(StanzaListener stanzaListener)
stanzaListener - the stanza listener to remove.void addStanzaSendingListener(StanzaListener stanzaListener, StanzaFilter stanzaFilter)
stanzaListener - the stanza listener to notify of sent stanzas.stanzaFilter - the stanza filter to use.void removeStanzaSendingListener(StanzaListener stanzaListener)
stanzaListener - the stanza listener to remove.@Deprecated void addStanzaInterceptor(StanzaListener stanzaInterceptor, StanzaFilter stanzaFilter)
addMessageInterceptor(Consumer, Predicate) or addPresenceInterceptor(Consumer, Predicate) instead.
NOTE: For a similar functionality on incoming stanzas, see addAsyncStanzaListener(StanzaListener, StanzaFilter).
stanzaInterceptor - the stanza interceptor to notify of stanzas about to be sent.stanzaFilter - the stanza filter to use.@Deprecated void removeStanzaInterceptor(StanzaListener stanzaInterceptor)
removeMessageInterceptor(Consumer) or removePresenceInterceptor(Consumer) instead.stanzaInterceptor - the stanza interceptor to remove.void addMessageInterceptor(Consumer<MessageBuilder> messageInterceptor, Predicate<Message> messageFilter)
NOTE: For a similar functionality on incoming stanzas, see addAsyncStanzaListener(StanzaListener, StanzaFilter).
messageInterceptor - the stanza interceptor to notify of stanzas about to be sent.messageFilter - the stanza filter to use.void removeMessageInterceptor(Consumer<MessageBuilder> messageInterceptor)
messageInterceptor - the message interceptor to remove.void addPresenceInterceptor(Consumer<PresenceBuilder> presenceInterceptor, Predicate<Presence> presenceFilter)
NOTE: For a similar functionality on incoming stanzas, see addAsyncStanzaListener(StanzaListener, StanzaFilter).
presenceInterceptor - the stanza interceptor to notify of stanzas about to be sent.presenceFilter - the stanza filter to use.void removePresenceInterceptor(Consumer<PresenceBuilder> presenceInterceptor)
presenceInterceptor - the stanza interceptor to remove.long getReplyTimeout()
void setReplyTimeout(long timeout)
SmackException.NoResponseException if no reply to a request was received within the timeout period.timeout - for a reply in millisecondsint getConnectionCounter()
2*Integer.MAX_VALUE instances as the counter could wrap.void setFromMode(XMPPConnection.FromMode fromMode)
fromMode - TODO javadoc me pleaseXMPPConnection.FromMode getFromMode()
XMPPConnection.FromMode@Deprecated default <F extends FullyQualifiedElement> F getFeature(java.lang.String element, java.lang.String namespace)
getFeature(Class) instead.null if the server doesn't support that feature.F - ExtensionElement type of the feature.element - TODO javadoc me pleasenamespace - TODO javadoc me pleasenull<F extends FullyQualifiedElement> F getFeature(javax.xml.namespace.QName qname)
null if the server doesn't support that feature.F - ExtensionElement type of the feature.qname - the qualified name of the XML element of feature.nulldefault <F extends FullyQualifiedElement> F getFeature(java.lang.Class<F> featureClass)
null if the server doesn't support that feature.F - ExtensionElement type of the feature.featureClass - the class of the feature.nulldefault boolean hasFeature(java.lang.String element,
java.lang.String namespace)
element - TODO javadoc me pleasenamespace - TODO javadoc me pleaseboolean hasFeature(javax.xml.namespace.QName qname)
qname - the qualified name of the XML element of feature.SmackFuture<IQ,java.lang.Exception> sendIqRequestAsync(IQ request)
request - the IQ request to send.SmackFuture<IQ,java.lang.Exception> sendIqRequestAsync(IQ request, long timeout)
request - the IQ request to send.timeout - the reply timeout in milliseconds.<S extends Stanza> SmackFuture<S,java.lang.Exception> sendAsync(S stanza, StanzaFilter replyFilter)
S - the type of the stanza to send.stanza - the stanza to send.replyFilter - the filter used for the response stanza.<S extends Stanza> SmackFuture<S,java.lang.Exception> sendAsync(S stanza, StanzaFilter replyFilter, long timeout)
S - the type of the stanza to send.stanza - the stanza to send.replyFilter - the filter used for the response stanza.timeout - the reply timeout in milliseconds.void addOneTimeSyncCallback(StanzaListener callback, StanzaFilter stanzaFilter)
callback - the callback invoked once the stanza filter matches a stanza.stanzaFilter - the filter to match stanzas or null to match all.IQRequestHandler registerIQRequestHandler(IQRequestHandler iqRequestHandler)
IQ request handler process incoming IQ requests, i.e. incoming IQ stanzas of type 'get' or 'set', and return a result.
iqRequestHandler - the IQ request handler to register.IQRequestHandler unregisterIQRequestHandler(IQRequestHandler iqRequestHandler)
unregisterIQRequestHandler(String, String, org.jivesoftware.smack.packet.IQ.Type).iqRequestHandler - TODO javadoc me pleaseIQRequestHandler unregisterIQRequestHandler(java.lang.String element, java.lang.String namespace, IQ.Type type)
element - the IQ element the IQ request handler is responsible for.namespace - the IQ namespace the IQ request handler is responsible for.type - the IQ type the IQ request handler is responsible for.long getLastStanzaReceived()