What Spring annotation I need to use with Mqttcallback messageArrived

Asked

Viewed 68 times

0

I am new to Spring and am trying to insert in my Mysql database from the message received from MQTT. I created my application as follows:

 public static void main(String[] args) throws InterruptedException {
        ConfigurableApplicationContext context = SpringApplication.run(MqttServiceApplication.class, args);
        context.registerShutdownHook();

        ProductionSubscribe();
    }

  public static void ProductionSubscribe() {
        try {
            IMqttClient client = new MqttClient("tcp://192.168.0.201:1883", new Date().getTime() + "");
            client.connect();

            ProductionCallBack productionCallBack = new ProductionCallBack();
            client.setCallback(productionCallBack);

            client.subscribe("/InputCounter/#");
        } catch (MqttException e) {
            e.printStackTrace();
        }
    }

So I implemented Mqttcallback

@Component
public class ProductionCallBack implements MqttCallback {

    @Autowired
    private MachineService machineService;

    @Override
    public void connectionLost(Throwable throwable) {
        System.out.println(throwable);
    }

    @Override
    public void messageArrived(String s, MqttMessage mqttMessage) throws Exception {
        String name = s.replace("/InputCounter/", "").split("/")[0];
        Date now = new Date();

        EasyIOModel easyIOModel = new ObjectMapper().readValue(mqttMessage.toString(), EasyIOModel.class);

        // Get Machine
        Machine machine = machineService.getMachineByNameAndInputNumber(easyIOName);
        if (machine == null) {
            System.out.println("Error.");
        }
    }

    @Override
    public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) {
        System.out.println("deliveryComplete");
    }
}

Everything is going fine until the messageArrived call, but when I use the machineService service, it is null. And I can’t access your methods

If I use REST controllers, my services work well... But I don’t know how to do, or what annotations to use, in the case of MQTT, to call my services.

Edit

SERVICE INTERFACE:

public interface MachineService {
    Machine getMachineByNameAndInputNumber(String machineName);
}

SERVICE:

@Service
public class MachineServiceImpl implements MachineService {

    @Inject
    private MachineRepository repository;

    @Override
    public Machine getMachineByNameAndInputNumber(String machineName) {
        return repository.getMachineByNameAndInputNumber(machineName);
    }
}

REPOSITORY:

@Repository
public interface MachineRepository extends JpaRepository<Machine, Long> {
    @Query(nativeQuery = true, value = "SELECT * FROM machine WHERE name = :machineName LIMIT 1")
    Machine getMachineByNameAndInputNumber(String machineName);
}

Thank you,

  • Where are you setting the machineService ? It was missing to declare it in this component private Machineservice machineService for you to be able to use, remembering that Machineservice must be managed by spring, the class must have noted with @service

  • Hey, Rnd, what’s up? In fact I did not declare, because what I wanted was something similar to when I use REST, and with Requestmapping ends up not having to declare. And yes, Machineservice is annotated @Service.

  • When I do what I said, declaring within my method, it works... The problem is that within the service, my @Repository is also null. And I end up having the same problem there. Only in this case I can’t just declare it. Since Repository is actually an interface that extends JPA.

  • https://stackoverflow.com/a/36663131/10534900 Example of what I mean...

  • Post your service and Repository to help with analysis

  • Okay, I edited the post with them...

Show 1 more comment

1 answer

2


You need to inject service in class:

@Component
public class ProductionCallBack implements MqttCallback {

   @Autowired
   private MachineService machineService;

}

And the Repository in service:

@Service
public class MachineServiceImpl implements MachineService {

    @Autowired
    private MachineRepository repository;

}

Edit

@SpringBootApplication
public class MqttServiceApplication implements AsyncConfigurer, CommandLineRunner {

    @Autowired
    ProductionCallBack productionCallBack;

    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(MqttServiceApplication.class, args);
        context.registerShutdownHook();
    }

    @Override
    public void run(String... args) throws Exception {
        try {
            IMqttClient client = new MqttClient("tcp://192.168.0.201:1883", new Date().getTime() + "");
            client.connect();


            client.setCallback(productionCallBack);

            client.subscribe("/InputCounter/#");
        } catch (MqttException e) {
            e.printStackTrace();
        }

    }

}
  • brendonmiranda, my mistake, sorry... I put the wrong part of the code. I already did that and I just corrected the question. He is as he said and yet I get null

  • you are injecting the Repository with @Autowired?

  • I did tests with @Autowired and @Inject and both have the same problem. But I can’t even get into the service to call the Repository... For the Service comes null

  • The interface is annotated with @Component ? Put the bug here tbm

  • None of them is noted as @Component... Which one should? Machineservice?

  • That, write down the Machineservice interface with @ Component and post the error here too if it persists.

  • I just put @Component in Machineservice and the same error persists. This way I have @ Component in Machineservice interface and @ Service in Machineserviceimpl

  • You can post the full class that supports your main method ?

  • the following is a print of the class > http://prntscr.com/r0cowz

Show 5 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.