HiveMQ MQTT Client offers 3 different flavours of API:
Blocking
Asynchronous
Reactive
Each of them has its own interface, you can pick one that fits your requirements and it is possible to switch the API flavour at any time and use different API flavours of the same client concurrently. More info: API Flavours.
Check out the Quick Start chapter to get started with asynchronous API flavour. For example, it shows how to define a callback function in order to process received messages is shown in the chapter Subscribing to a topic:
Given a client which has successfully connected to a broker, you can setup your subscriptions:
client.subscribeWith()
.topicFilter("the/topic")
.callback(publish -> {
// Process the received message
})
.send()
.whenComplete((subAck, throwable) -> {
if (throwable != null) {
// Handle failure to subscribe
} else {
// Handle successful subscription, e.g. logging or incrementing a metric
}
});