0

I create a Spring project that listens to an MQTT topic. The message is received in a callback. However, in the callback @Autowired service is null.

Main

@SpringBootApplication
public class AuthenticationWithJwtApplication {

    public static void main(String[] args) {
        MqttUtil mqttUtil = new MqttUtil();
        mqttUtil.subscribeTopic("temperature");
        SpringApplication.run(AuthenticationWithJwtApplication.class, args);
    }
}

MqttUtil

@Component
public class MqttUtil {
//    @Autowired
//    SimpleCallback simpleCallback;

    public MqttClient connect() throws MqttException {
        String broker = "tcp://test.mosquitto.org:1883";
        MqttConnectOptions connOpts = new MqttConnectOptions();
        connOpts.setCleanSession(true);
        System.out.println("Connecting to broker: " + broker);
        System.out.println("Connected");
        MqttClient mqttClient = new MqttClient(broker, "clientid1");
        mqttClient.connect(connOpts);
        return mqttClient;
    }

    public void subscribeTopic(String topic) {
        try {
            MqttClient mqttClient = connect();
            mqttClient.subscribe(topic, 1);
            mqttClient.setCallback(new SimpleCallback());
//            mqttClient.setCallback(simpleCallback);
        } catch (MqttException me) {
            System.out.println("reason " + me.getReasonCode());
            System.out.println("msg " + me.getMessage());
            System.out.println("loc " + me.getLocalizedMessage());
            System.out.println("cause " + me.getCause());
            System.out.println("excep " + me);
        }
    }
}

deviceService in SimpleCallback is null.

SimpleCallback

package com.example.authentiHeadingcation_with_jwt.mqtt;

import com.example.authentication_with_jwt.services.DeviceService;
import com.example.authentication_with_jwt.services.TemperatureDataService;
import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttCallback;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


@Component
public class SimpleCallback implements MqttCallback {
    @Autowired
    private TemperatureDataService temperatureDataService;

    @Override
    public void connectionLost(Throwable cause) {

    }

    @Override
    public void messageArrived(String topic, MqttMessage message) throws Exception {
        System.out.println(temperatureDataService);
    }

    @Override
    public void deliveryComplete(IMqttDeliveryToken token) {

    }
}

Device Service

@Service
public class DeviceService {
    @Autowired
    DeviceRepository deviceRepository;

    @Autowired
    TemperatureDataRepository temperatureDataRepository;

    public Device getDeviceBySerial(String deviceSerial) throws Exception {
        Device device = deviceRepository.findBySeri(deviceSerial);
        if (device == null) {
            throw new Exception("Device not found");
        }
        return device;
    }
}

I tried this then I cannot receive messge.

@Component
public class MqttUtil {
    @Autowired
    SimpleCallback simpleCallback;

    public MqttClient connect() throws MqttException {
        String broker = "tcp://test.mosquitto.org:1883";
        MqttConnectOptions connOpts = new MqttConnectOptions();
        connOpts.setCleanSession(true);
        System.out.println("Connecting to broker: " + broker);
        System.out.println("Connected");
        MqttClient mqttClient = new MqttClient(broker, "clientid1");
        mqttClient.connect(connOpts);
        return mqttClient;
    }

    public void subscribeTopic(String topic) {
        try {
            MqttClient mqttClient = connect();
            mqttClient.subscribe(topic, 1);
            mqttClient.setCallback(simpleCallback);
        } catch (MqttException me) {
            System.out.println("reason " + me.getReasonCode());
            System.out.println("msg " + me.getMessage());
            System.out.println("loc " + me.getLocalizedMessage());
            System.out.println("cause " + me.getCause());
            System.out.println("excep " + me);
        }
    }
}

Here is my project: https://github.com/duyleomessi/c1907E-spring.

Thanks!

duy le
  • 43
  • 7
  • 1
    Your `MqttUtil` class is annotated with `@Component`. It means that Spring should create the bean for you. You should not create it yourself in main method before `run`. There are two options: (1) use `@PostContruct` annotation on the subscribe method or (2) do something like this in main merhod: `ConfigurableApplicationContext run = SpringApplication.run(StackoverflowApplication.class, args); Object(MqttUtil) mqttUtil = run.getBean("MqttUtil"); mqttUtil.subscribe();` – c3R1cGFy Sep 08 '21 at 12:35

0 Answers0