XMPP
Since Camel 1.0
Both producer and consumer are supported
The XMPP component implements an XMPP (Jabber) transport.
Maven users will need to add the following dependency to their pom.xml for this component:
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-xmpp</artifactId>
<version>x.x.x</version>
<!-- use the same version as your Camel core version -->
</dependency> URI format
xmpp://[login@]hostname[:port][/participant][?Options]
The component supports both room based and private person-person conversations.
The component supports both producer and consumer (you can get messages from XMPP or send messages to XMPP). Consumer mode supports rooms starting.
Configuring Options
Camel components are configured on two separate levels:
-
component level
-
endpoint level
Configuring Component Options
At the component level, you set general and shared configurations that are, then, inherited by the endpoints. It is the highest configuration level.
For example, a component may have security settings, credentials for authentication, urls for network connection and so forth.
Some components only have a few options, and others may have many. Because components typically have pre-configured defaults that are commonly used, then you may often only need to configure a few options on a component; or none at all.
You can configure components using:
-
the Component DSL.
-
in a configuration file (
application.properties,*.yamlfiles, etc). -
directly in the Java code.
Configuring Endpoint Options
You usually spend more time setting up endpoints because they have many options. These options help you customize what you want the endpoint to do. The options are also categorized into whether the endpoint is used as a consumer (from), as a producer (to), or both.
Configuring endpoints is most often done directly in the endpoint URI as path and query parameters. You can also use the Endpoint DSL and DataFormat DSL as a type safe way of configuring endpoints and data formats in Java.
A good practice when configuring options is to use Property Placeholders.
Property placeholders provide a few benefits:
-
They help prevent using hardcoded urls, port numbers, sensitive information, and other settings.
-
They allow externalizing the configuration from the code.
-
They help the code to become more flexible and reusable.
The following two sections list all the options, firstly for the component followed by the endpoint.
Component Options
The XMPP component supports the following options which are listed below.
| Name | Description | Default | Type |
|---|---|---|---|
Allows for bridging the consumer to the Camel routing Error Handler, which mean any exceptions (if possible) occurred while the Camel consumer is trying to pickup incoming messages, or the likes, will now be processed as a message and handled by the routing Error Handler. Important: This is only possible if the 3rd party component allows Camel to be alerted if an exception was thrown. Some components handle this internally only, and therefore bridgeErrorHandler is not possible. In other situations we may improve the Camel component to hook into the 3rd party component and make this possible for future releases. By default the consumer will use the org.apache.camel.spi.ExceptionHandler to deal with exceptions, that will be logged at WARN or ERROR level and ignored. | false | boolean | |
Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a producer may otherwise fail during starting and cause the route to fail being started. By deferring this startup to be lazy then the startup failure can be handled during routing messages via Camel’s routing error handlers. Beware that when the first message is processed then creating and starting the producer may take a little time and prolong the total processing time of the processing. | false | boolean | |
Whether autowiring is enabled. This is used for automatic autowiring options (the option must be marked as autowired) by looking up in the registry to find if there is a single instance of matching type, which then gets configured on the component. This can be used for automatic configuring JDBC data sources, JMS connection factories, AWS Clients, etc. | true | boolean |
Endpoint Options
The XMPP endpoint is configured using URI syntax:
xmpp:host:port/participant
With the following path and query parameters:
Path Parameters
| Name | Description | Default | Type |
|---|---|---|---|
Required The host name or IP address of the XMPP server. It is also the XMPP domain when the serviceName option is not set. Not used when the connectionConfig option is set. | String | ||
The TCP port of the XMPP server for client connections. Not used when the connectionConfig option is set. | 5222 | int | |
The JID (Jabber ID) of the person to chat with: the producer sends the chat messages to it, and the consumer routes only the chat messages from it (or of every sender, without a participant). The consumer adds the XMPP domain of the server to a name without a domain, while the producer needs the full JID. The room option takes precedence over this option. | String |
Query Parameters
| Name | Description | Default | Type |
|---|---|---|---|
Whether to log in as the user after connecting. Used only when the user option is set; without a user, the connection always logs in anonymously. If false, the connection is not logged in, so it can neither send nor receive messages. | true | boolean | |
The nickname to enter the room with. Used only with the room option. If not set, the user option is used. | String | ||
Whether to work with XEP-0060 Publish-Subscribe. The consumer also routes the messages of type headline and normal, such as pubsub event notifications, and the producer, without a room, sends the body as a Smack PubSub packet. Setting it to true also sets the doc option to true. | false | boolean | |
The XEP-0045 Multi-User Chat room to enter, as a room JID with the domain of the chat service, for example conference.jabber.org. A name without a domain is completed with the first Multi-User Chat service that the server announces. The producer sends the messages to the room, and the consumer routes the messages of the room and the chat messages of every sender. | String | ||
The XMPP domain of the server (its service name), for example example.org when the host is xmpp.example.org. If not set, the host option is used. Not used when the connectionConfig option is set. | String | ||
Whether the consumer and the chat and room producers connect to the XMPP server when they start, so that a server that cannot be reached fails the start. If false, such a producer connects at the first message, and the consumer tries again every connectionPollDelay seconds until it is started. The doc and pubsub producers always connect at the first message. | true | boolean | |
Whether to register the user as a new account on the server (XEP-0077 In-Band Registration), with the user and password options, before logging in. The registration is done for each new connection, and the connection fails when the server refuses it, for example because the account exists already. | false | boolean | |
The resource part of the full JID (the part after the slash) that the connection logs in with. Used only when the user option is set. | Camel | String | |
The delay in seconds between the checks of the consumer’s connection, which connect and log in again a lost connection and enter the room again. It is also the delay between the attempts of a consumer that could not connect at startup (testConnectionOnStartup=false). | 10 | int | |
Whether the consumer puts the received Smack Message (org.jivesoftware.smack.packet.Message) in the body and in the CamelXmppDoc header, instead of the text of the message. The producer, without a room or pubsub, sends the body as it is: a Smack Stanza or an array of them. | false | boolean | |
Allows for bridging the consumer to the Camel routing Error Handler, which mean any exceptions (if possible) occurred while the Camel consumer is trying to pickup incoming messages, or the likes, will now be processed as a message and handled by the routing Error Handler. Important: This is only possible if the 3rd party component allows Camel to be alerted if an exception was thrown. Some components handle this internally only, and therefore bridgeErrorHandler is not possible. In other situations we may improve the Camel component to hook into the 3rd party component and make this possible for future releases. By default the consumer will use the org.apache.camel.spi.ExceptionHandler to deal with exceptions, that will be logged at WARN or ERROR level and ignored. | false | boolean | |
To let the consumer use a custom ExceptionHandler. Notice if the option bridgeErrorHandler is enabled then this option is not in use. By default the consumer will deal with exceptions, that will be logged at WARN or ERROR level and ignored. | ExceptionHandler | ||
Sets the exchange pattern when the consumer creates an exchange. Enum values:
| ExchangePattern | ||
Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a producer may otherwise fail during starting and cause the route to fail being started. By deferring this startup to be lazy then the startup failure can be handled during routing messages via Camel’s routing error handlers. Beware that when the first message is processed then creating and starting the producer may take a little time and prolong the total processing time of the processing. | false | boolean | |
To use a custom Smack connection configuration, which must be an org.jivesoftware.smack.tcp.XMPPTCPConnectionConfiguration (XMPP over TCP), for example to change the TLS or SASL settings. When set, the host, port and serviceName options are not used to connect. Endpoints share a connection only when they refer to the same instance. | ConnectionConfiguration | ||
To use a custom HeaderFilterStrategy to filter the headers. The producer adds the headers that pass as Jive properties to the XMPP message (the subject and language headers set the subject and language of the message), and the consumer turns the Jive properties of a received message into headers. | HeaderFilterStrategy | ||
The password of the user. It can also be set in the user info of the URI, as user:password. With createAccount, it is also the password of the new account. | String | ||
The password to enter a password-protected room. | String | ||
The user name to log in with: the localpart of the JID, without the domain. It can also be set in the user info of the URI. If not set, the connection logs in anonymously (SASL ANONYMOUS), which needs a connectionConfig that enables it with performSaslAnonymousAuthentication(). | String |
Message Headers
The XMPP component supports the following message header(s), which is/are listed below:
| Name | Description | Default | Type |
|---|---|---|---|
| Constant: | The received Smack message, set by the consumer when the doc option is true. The pubsub producer sets it to the PubSub packet that it sends. | Message |
Message Body
The message body at each side of the XMPP component: Consumer is the body of the messages the consumer creates, Consumer reply the bodies a consumer that answers its caller can reply with, Producer input the bodies the producer can send or write, and Producer output the body after the producer.
| Side | Body | Description |
|---|---|---|
Consumer |
| The text of the chat message; the Smack Message when it has no body text, such as a pubsub notification. With doc, the Smack Message. The subject, thread and sender are in the CamelXmpp* headers.
|
Producer input |
| The body is converted to text and sent as the chat message (a Map or a POJO as its toString); the headers become message properties. Without a room, doc sends a Smack Stanza (or an array of them) and pubsub a Smack PubSub packet as they are; another body fails.
|
Producer output |
|
The bodies are the ones with the default options. A side that is not listed is not known, and tooling does not guess it. The carriers, the sides and the tools that use them are described in Message Body Types.
Usage
Connections
The consumers and producers of one account (the same host, port, service name, user, password, resource and login settings) share one XMPP connection, also across endpoints with different participants or rooms. With the connectionConfig option, endpoints share a connection only when they refer to the same ConnectionConfiguration instance, for example the same bean in the registry, because that class has no equals().
Receiving chat messages
A consumer with a participant routes only the chat messages from that participant (any resource of its JID). A consumer without a participant routes the chat messages of every sender. The JID of the sender is in the CamelXmppFrom header. A consumer with a room routes the messages of the room, and the chat messages of every sender.
Examples
User superman to join room krypton at jabber server with password, secret:
xmpp://superman@jabber.org/?room=krypton@conference.jabber.org&password=secret
User superman to send messages to joker:
xmpp://superman@jabber.org/joker@jabber.org?password=secret
Routing example:
-
Java
-
XML
-
YAML
from("timer://kickoff?period=10000")
.setBody(constant("I will win! Your Superman."))
.to("xmpp://superman@jabber.org/joker@jabber.org?password=secret"); <route>
<from uri="timer://kickoff?period=10000"/>
<setBody>
<constant>I will win! Your Superman.</constant>
</setBody>
<to uri="xmpp://superman@jabber.org/joker@jabber.org?password=secret"/>
</route> - route:
from:
uri: timer:kickoff
parameters:
period: 10000
steps:
- setBody:
constant: "I will win! Your Superman."
- to:
uri: xmpp://superman@jabber.org/joker@jabber.org
parameters:
password: secret Consumer configuration, which writes all messages from joker into the queue, evil.talk.
-
Java
-
XML
-
YAML
from("xmpp://superman@jabber.org/joker@jabber.org?password=secret")
.to("activemq:evil.talk"); <route>
<from uri="xmpp://superman@jabber.org/joker@jabber.org?password=secret"/>
<to uri="activemq:evil.talk"/>
</route> - route:
from:
uri: xmpp://superman@jabber.org/joker@jabber.org
parameters:
password: secret
steps:
- to:
uri: activemq:evil.talk Consumer configuration, which listens to room messages:
-
Java
-
XML
-
YAML
from("xmpp://superman@jabber.org/?password=secret&room=krypton@conference.jabber.org")
.to("activemq:krypton.talk"); <route>
<from uri="xmpp://superman@jabber.org/?password=secret&room=krypton@conference.jabber.org"/>
<to uri="activemq:krypton.talk"/>
</route> - route:
from:
uri: xmpp://superman@jabber.org/
parameters:
password: secret
room: "krypton@conference.jabber.org"
steps:
- to:
uri: activemq:krypton.talk Room in short notation (no domain part):
-
Java
-
XML
-
YAML
from("xmpp://superman@jabber.org/?password=secret&room=krypton")
.to("activemq:krypton.talk"); <route>
<from uri="xmpp://superman@jabber.org/?password=secret&room=krypton"/>
<to uri="activemq:krypton.talk"/>
</route> - route:
from:
uri: xmpp://superman@jabber.org/
parameters:
password: secret
room: krypton
steps:
- to:
uri: activemq:krypton.talk When connecting to the Google Chat service, you’ll need to specify the serviceName as well as your credentials:
-
Java
-
XML
-
YAML
from("direct:start")
.to("xmpp://talk.google.com:5222/touser@gmail.com?serviceName=gmail.com&user=fromuser&password=secret")
.to("mock:result"); <route>
<from uri="direct:start"/>
<to uri="xmpp://talk.google.com:5222/touser@gmail.com?serviceName=gmail.com&user=fromuser&password=secret"/>
<to uri="mock:result"/>
</route> - route:
from:
uri: direct:start
steps:
- to:
uri: xmpp://talk.google.com:5222/touser@gmail.com
parameters:
serviceName: gmail.com
user: fromuser
password: secret
- to:
uri: mock:result