Package org.bluezoo.gumdrop.mqtt.client
Class MQTTClient
java.lang.Object
org.bluezoo.gumdrop.mqtt.client.MQTTClient
High-level MQTT client facade.
Provides a simple API for connecting to MQTT brokers. Internally
creates a TCPTransportFactory, ClientEndpoint, and
MQTTClientProtocolHandler, following the same pattern as
SMTPClient.
Usage
MQTTClient client = new MQTTClient("broker.example.com", 1883);
client.setClientId("myClient");
client.connect(new MQTTClientCallback() {
public void connected(boolean sessionPresent, int returnCode) {
client.subscribe("sensors/#", QoS.AT_LEAST_ONCE);
}
public void connectionLost(Exception cause) { ... }
public void subscribeAcknowledged(int id, int[] qos) { ... }
public void publishComplete(int id) { ... }
}, (topic, payload, qos, retain) -> {
System.out.println("Received: " + topic);
});
- Author:
- Chris Burdess
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionMQTTClient(String host, int port) MQTTClient(InetAddress host, int port) MQTTClient(SelectorLoop selectorLoop, String host, int port) MQTTClient(SelectorLoop selectorLoop, InetAddress host, int port) -
Method Summary
Modifier and TypeMethodDescriptionvoidconnect(MQTTClientCallback callback, MQTTMessageListener messageListener) Connects to the MQTT broker.voidSends DISCONNECT and closes the connection.intPublishes a message to a topic.intPublishes a UTF-8 string message.voidsetCleanSession(boolean cleanSession) voidsetClientId(String clientId) voidsetCredentials(String username, String password) voidsetKeepAlive(int keepAlive) voidsetKeystoreFile(Path path) voidsetKeystorePass(String pass) voidsetMessageStore(MQTTMessageStore messageStore) voidsetSecure(boolean secure) voidsetSSLContext(SSLContext context) voidsetTrustManager(X509TrustManager trustManager) voidsetVersion(MQTTVersion version) voidintSubscribes to multiple topic filters.intSubscribes to a single topic filter.intunsubscribe(String... topicFilters) Unsubscribes from topic filters.
-
Constructor Details
-
MQTTClient
-
MQTTClient
-
MQTTClient
-
MQTTClient
-
-
Method Details
-
setSecure
public void setSecure(boolean secure) -
setSSLContext
-
setTrustManager
-
setKeystoreFile
-
setKeystorePass
-
setVersion
-
setClientId
-
setCleanSession
public void setCleanSession(boolean cleanSession) -
setKeepAlive
public void setKeepAlive(int keepAlive) -
setCredentials
-
setMessageStore
-
setWill
-
connect
public void connect(MQTTClientCallback callback, MQTTMessageListener messageListener) throws IOException Connects to the MQTT broker.- Parameters:
callback- lifecycle callbackmessageListener- message delivery callback- Throws:
IOException- if the transport cannot be created
-
publish
Publishes a message to a topic.- Returns:
- the packet ID (0 for QoS 0)
-
publish
Publishes a UTF-8 string message. -
subscribe
Subscribes to a single topic filter.- Returns:
- the packet ID
-
subscribe
Subscribes to multiple topic filters.- Returns:
- the packet ID
-
unsubscribe
Unsubscribes from topic filters.- Returns:
- the packet ID
-
disconnect
public void disconnect()Sends DISCONNECT and closes the connection.
-