How can i read a mesage from a topic in Java and return to a layout?

Hi i need to read a message from a topic in HiveMqCloud
I have a SpringBoot app and my idea is that when I receive a message from a topic I want to take that message and insert into a database.
The problem is that when I get my user id that is saved in the context of the application I can’t reach the content in the messageArrived method of the MqttCallback interface. I want to take that message and insert into my database along with my user id.

The other solution that I’ve thinking of is that when the app is running receive the message in a topic and show in an html page.

Do you know how can I do this?

Thank you so much!

Hi cao95,

please feel welcome to our MQTT community! Please share your code with us and point out where what does not work.

Many thanks!
Dasha from HiveMQ Team

This is my interface implementation. I’m implementing the methods of the MqttCallback interface. When I publish a message in my topic (test/topic) I receive the message in the messageArrived method.
But the method is a void method and I cannot get that message and return it in my html page.
For the other hand if I try to get my user data saved in my spring context in that method I cannot get that either. I know that is because I’m in another interface implementation method.
But there is any way to read the message and make the method return a String instead of a void? Or is a way to get my user data in that implementation so I can save the message in my DB along with my user Id?

I’ve been thinkin to create some kind of consumer, but I don’t know how to do it with the mqtt dependency I’m using.

Here is my method.

@Service
public class MqttPublisherServiceImpl implements MqttPublisherService, MqttCallback {

    public static void publishPayload(MqttClient client, Object object, String topic) throws MqttException {
        client.publish(
                topic,
                CultzymeUtils.objectToJson(object).getBytes(UTF_8),
                2, // QoS = 2
                false);
    }

    public MqttClient getMqttConnection() throws MqttException {
        MqttClient client = new MqttClient(
                "", // serverURI in format: "protocol://name:port"
                MqttClient.generateClientId(), // ClientId
                new MemoryPersistence()); // Persistence

        MqttConnectOptions mqttConnectOptions = new MqttConnectOptions();
        mqttConnectOptions.setUserName("my_user");
        mqttConnectOptions.setPassword("my_pass".toCharArray());
        mqttConnectOptions.setSocketFactory(SSLSocketFactory.getDefault());
        client.setCallback(this);
        client.connect(mqttConnectOptions);
        client.subscribe("test/topic");
        return client;
    }

    @Override
    public Boolean publishCultzymeData(CultzymeMqttActuatorsData cultzymeMqttActuatorsData, CultzymeMqttTargetTemperatureData cultzymeMqttTargetTemperatureData) {
        try {
            MqttClient client = this.getMqttConnection();
            publishPayload(client, cultzymeMqttActuatorsData, MqttPublisherTopics.CULTZYME_MQTT_ACTUATORS);
            publishPayload(client, cultzymeMqttTargetTemperatureData, MqttPublisherTopics.CULTZYME_MQTT_TARGET_TEMPERATURE);
            //client.disconnect();
        } catch (MqttException e) {
            return false;
        }
        return true;
    }

    @Override
    public void connectionLost(Throwable throwable) {

    }

    @Override
    public void messageArrived(String s, MqttMessage mqttMessage) throws Exception {
        System.out.println("Message arrived" + mqttMessage.toString());
    }

    @Override
    public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) {

    }
}

I’m using this dependency

        <dependency>
            <groupId>org.eclipse.paho</groupId>
            <artifactId>org.eclipse.paho.client.mqttv3</artifactId>
            <version>1.2.2</version>
        </dependency>

And also tried with this one

        <dependency>
            <groupId>org.springframework.integration</groupId>
            <artifactId>spring-integration-mqtt</artifactId>
            <version>5.5.2</version>
        </dependency>

Thank you so much! :slight_smile: