MQTT Cloud Connector
MQTT (Message Queue Telemetry Transport) is a lightweight publish-subscribe messaging protocol.
In order to connect to a MQTT broker, the MQTTRAR-0.1.0-SNAPSHOT.rar
has to
be deployed as shown in the
Installing a connector section of the Cloud Connectors overview.
In order to make use of this connector in an application, the following maven dependency is needed:
<dependency>
<groupId>fish.payara.cloud.connectors.mqtt</groupId>
<artifactId>MQTT-JCA-API</artifactId>
<version>0.1.0-SNAPSHOT</version>
<type>jar</type>
<scope>provided</scope>
</dependency>
Note that this dependency have scope provided since the types within this
dependency are globally available to every application deployed to Payara Micro
after the MQTTRAR-0.1.0-SNAPSHOT.rar
was deployed.
Sending messages
Sending messages to a MQTT broker can be done via the JCA and an MQTT specific API. In order to start using this API to send messages, a resource has to be defined via the JCA API; a connection factory.
The connection factory has to be given a name, which can be any name that is
valid for JNDI. The java:app
namespace is typically recommended to be used.
The type of the connection factory to be used for MQTT is
fish.payara.cloud.connectors.mqtt.api.MQTTConnectionFactory
, and we have to
specify the resource adapter name which is here MQTTRAR-0.1.0-SNAPSHOT.rar
.
The following gives an example:
@ConnectionFactoryDefinition (
name = "java:app/mqtt/factory",
interfaceName = "fish.payara.cloud.connectors.mqtt.api.MQTTConnectionFactory",
resourceAdapter = "MQTTRAR-0.1.0-SNAPSHOT"
properties = "cleanSession=true")
)
With the above shown definition in place the following code shows an example of sending a message:
@Singleton
@Startup
public class SendMQTTMessage {
@Resource(lookup = "java:app/mqtt/factory")
private MQTTConnectionFactory factory;
@PostConstruct
public void init() {
try (MQTTConnection connection = factory.createConnection()) {
connection.publish("test", "{\"test\": \"Hello World\"}".getBytes(), 0, false);
}
catch (Exception ex) {
}
}
}
Receiving messages
Messages can be received from an MQTT broker by creating an MDB (Message Driven
Bean) that implements the fish.payara.cloud.connectors.mqtt.api.MQTTListener
marker interface and has a single method annotated with @OnMQTTMessage
and the
method signature void method(String topic, MqttMessage message)
.
The following gives an example:
@MessageDriven(activationConfig = {
@ActivationConfigProperty(propertyName = "topicFilter", propertyValue = "test")
})
public class ReceiveMQTTMessage implements MQTTListener {
@OnMQTTMessage
public void receiveMessage(String topic, MqttMessage message) {
// Handle message
}
}
Config Property Name | Type | Default | Notes |
---|---|---|---|
serverURIs |
String |
tcp://localhost:1883 |
Server URIs for connection, comma separated |
cleanSession |
Boolean |
false |
Sets whether the client and server should remember state across reconnects |
automaticReconnect |
Boolean |
true |
Sets whether the client will automatically reconnect to the server if the connection is lost |
filePersistance |
Boolean |
false |
Whether the client should use file persistence for unacked messages |
persistenceDirectory |
String |
. |
Directory to use for file persistence |
connectionTimeout |
Integer |
30 |
Sets the connection timeout value in seconds |
maxInflight |
Integer |
10 |
Sets the maximum messages that can be sent without acknowledgements |
keepAliveInterval |
Integer |
60 |
Sets the keep alive interval in seconds |
userName |
String |
None |
The user name for the connection. |
password |
String |
None |
The password for the connection. |
topicFilter |
String |
None |
Topic Filter (For MDBs only) |
qos |
String |
0 |
Quality of Service for the subscription (For MDBs only) |