Sending MQTT Messages with Quarkus (Part 2)

Andreas Eberle
arconsis
Published in
3 min readMar 9, 2023

In this series, we have a look on how to integrate your Quarkus service with MQTT. The first post in this series gives an introduction into MQTT and illustrates how to receive messages from MQTT with Quarkus. Now, in the second post of this series, shows how to send messages to MQTT.

Sending Messages from Quarkus to MQTT

Video Tutorial

If you prefer a video tutorial instead of the text, have a look at our YouTube channel where we also cover the Quarkus MQTT integration.

Send Messages to MQTT with Quarkus

Project Setup

If you follow our MQTT blog series along, you don’t need to change anything in your project setup. If you’re a newjoiner, make sure to have the smallrye reactive messaging MQTT extension added to your Quarkus project:

implementation("io.quarkus:quarkus-smallrye-reactive-messaging-mqtt")

Connecting to MQTT

This time, we need to configure an outgoing connection to the broker in the Quarkus application.properties by adding the following lines:

mp.messaging.outgoing.hello-world.connector=smallrye-mqtt
mp.messaging.outgoing.hello-world.host=public.mqtthq.com
mp.messaging.outgoing.hello-world.port=1883
mp.messaging.outgoing.hello-world.topic=arconsis/twitch/hello-world
  • The type of the connection is outgoing as we want to send messages.
  • The connector’s name is specified as hello-world. This is the name we use inside Quarkus and can be freely chosen by you.
  • With the first line, the connector is specified as smallrye-mqtt to be able to connect to an MQTT broker. The smallrye messaging extension also provides connectors for other queues like Kafka or AMQP.
  • Host and port are needed to connect to the broker.
  • The topic is used to specify where the messages should go to. Listeners can subscribe to this topic or use wildcards like # or + to subscribe to multiple topics at the same time.
  • There exist some more properties e.g. for authentication. You can check them out here.

Sending MQTT Messages with Quarkus

To send messages to MQTT from within Quarkus, we can create a simple @ApplicationScoped bean into which Quarkus injects and Emitter<T>. For now, we only send simple string messages, so we use an Emitter<String> and annotate the field with @Channel("hello-world") to let Quarkus know we want to use the hello-world connector we specified in the application.properties. The following snippet shows the complete code we need in Kotlin. For debugging, we also add a simple logger.

@ApplicationScoped
class SimpleMqttSender(
@Channel("hello-world") private val emitter: Emitter<String>
) {

private val logger = Logger.getLogger(SimpleMqttSender::class.java.name)

fun sendSomeMessage(message: String) {
logger.info { "Sending message: $message" }
emitter.send(message)
}
}

With this, you can simply inject the SimpleMqttSender bean into any of your other beans and call its sendSomeMessage() method to send a message to the arconsis/twitch/hello-world topic.

To test it, we’ll create a small rest endpoint that we can easily call from our browser. For simplicity reasons, we will create a GET endpoint. Just add the following resource to your code:

@Path("/hello")
class HelloWorldTestResource(
private val simpleMqttSender: SimpleMqttSender
) {
@GET
@Path("/world/{message}")
fun helloWorld(@RestPath message: String) {
simpleMqttSender.sendSomeMessage("Hello World: $message")
}
}

This creates a GET endpoint under the path /hello/world/{message} where message can be freely chosen by you and is used as part of the message sent to the MQTT.

When you start Quarkus (e.g. ./gradlew quarkusDev) and open your browser with http://localhost:8080/hello/world/Quarkus, Quarkus will send the message to MQTT.If you also followed our first article, it will then receive the message and print it to the log. The result will look like this:

2023-02-28 11:18:51,883 INFO  [io.ver.mqt.imp.MqttClientImpl] (vert.x-eventloop-thread-0) Connection with public.mqtthq.com:1883 established successfully
2023-02-28 11:18:52,260 INFO [io.sma.rea.mes.mqtt] (vert.x-eventloop-thread-0) Subscription outcome: Future{result=0}
2023-02-28 11:18:56,265 INFO [com.arc.twi.mqt.sen.SimpleMqttSender] (executor-thread-0) Sending message: Hello World: Quarkus
2023-02-28 11:18:56,467 INFO [com.arc.twi.mqt.rec.SimpleMqttReceiver] (vert.x-eventloop-thread-0) Received raw message: Hello World: Quarkus

You can check out the code in our repository under the tag simple-message-sender.

To learn more about Quarkus and other great software engineering topics, make sure to follow us on medium. E.g. here is a great article testing ChatGPT’s React and Typescript abilities. Can it write the code for you?

--

--

Andreas Eberle
arconsis

Solutions Architect & Software Engineer @arconsis