Java
Hermes is a MQTT v5.0 compatible broker, so any compatible Java library can be used.
The widely used Eclipse Paho library can be imported into any Maven or Gradle project.
<repositories>
<repository>
<id>Eclipse Paho Repo</id>
<url>https://repo.eclipse.org/content/repositories/paho-releases/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>org.eclipse.paho</groupId>
<artifactId>org.eclipse.paho.mqttv5.client</artifactId>
<version>1.2.5</version>
</dependency>
</dependencies>
For Gradle repositories, use the below
// Groovy script
repositories {
maven {
url "https://repo.eclipse.org/content/repositories/paho-releases/"
}
}
// Kotlin script
repositories {
maven {
url = uri("https://repo.eclipse.org/content/repositories/paho-releases/")
}
}
Connecting to MQTT broker
var persistence = new MemoryPersistence();
var client = new MqttClient(broker, clientID, persistence);
var connOpts = new MqttConnectionOptions();
// Setting clean start to "false" enables the client
// to receive offline messages send while it was disconnected.
connOpts.setCleanStart(false);
client.connect(connOpts);
Publishing messages
// Ensure client has already connected
var content = "Hello World";
var topic = "my-topic";
var message = new MqttMessage(content.getBytes());
client.publish(topic,message);
Subscribing to incoming messages
// Do this before connecting
client.setCallback(new MqttCallback(){
@Override
public void messageArrived(String topic, MqttMessage message) throws Exception{
// Do something awesome
}
// other implemented methods
});
client.connect(connOpts);
// Provide a topic and Quality of Service (QoS)
client.subscribe("my-topic", 0);
Closing the connection
client.disconnect();
Spring Integration
Spring Integration provides inbound and outbound channel adapters to support the MQTT protocol.
The following dependencies can be used for Maven and Gradle respectively
<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-mqtt</artifactId>
<version>5.4.2</version>
</dependency>
compile "org.springframework.integration:spring-integration-mqtt:5.4.2"
Inbound Channel Adapters
Inbound adapters allow Spring applications to subscribe to topics and respond to incoming MQTT messages.
@Bean
public IntegrationFlow mqttInbound() {
var broker = "tcp://localhost:1883";
var clientID = "client-id";
var topic = "my-topic";
var adapter = new MqttPahoMessageDrivenChannelAdapter(broker, clientID, topic);
return IntegrationFlows.from(adapter).handle(m -> handleMsg(m)).get();
}
public void handleMsg(MqttMessage message) {
// Do something awesome
}
Outbound Channel Adapters
Inbound adapters allow Spring applications to publish MQTT messages onto topics.
@Bean
public IntegrationFlow mqttOutboundFlow() {
var broker = "tcp://localhost:1883";
var clientID = "client-id";
return f -> f.handle(new MqttPahoMessageHandler(broker, clientID));
}
More information regarding Spring MQTT integration can be found below on the Spring MQTT Support Homepage