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
– rnd_rss
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.
– Fernando Muniz Erthal
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.
– Fernando Muniz Erthal
https://stackoverflow.com/a/36663131/10534900 Example of what I mean...
– Fernando Muniz Erthal
Post your service and Repository to help with analysis
– brendonmiranda
Okay, I edited the post with them...
– Fernando Muniz Erthal