Reputation: 21
I will Spring Boot MQTT Integration with JAVA DSL Configuration. To publish a message I would like to use the MessageHandler as defined
@Slf4j
@Configuration
@EnableIntegration
public class MqttJavaApplicationConfig {
@Value( "${mqtt.broker.url}" )
private String brokerUrl;
@Bean
public IntegrationFlow mqttOutFlow() {
Mqttv5PahoMessageHandler messageHandler = new Mqttv5PahoMessageHandler(brokerUrl, "mqttv5SIout");
MqttHeaderMapper mqttHeaderMapper = new MqttHeaderMapper();
mqttHeaderMapper.setOutboundHeaderNames("some_user_header", MessageHeaders.CONTENT_TYPE);
messageHandler.setHeaderMapper(mqttHeaderMapper);
messageHandler.setAsync(true);
messageHandler.setAsyncEvents(true);
messageHandler.setDefaultTopic("testTopic");
return f -> f.handle(messageHandler);
}
}
What is working is to define the MessageGateway
@MessagingGateway
public interface MqttProduceGateway {
@Gateway(requestChannel = "mqttOutFlow.input")
void publishToMQTT(String data);
}
autowire the gateway bean
@Getter
@Setter
@NoArgsConstructor
@Component
public class PublishService {
@Autowired
MqttProduceGateway mqttProduceGateway;
public boolean publishToMqttBroker(String data){
mqttProduceGateway.publishToMQTT(data);
return true;
}
}
and send message within my app.
But to keep it as simple as possible, I prefer to use the Message Handler directly, like
@Slf4j
@ComponentScan(basePackages = { "de.diag.testprototypes.projectscv.mqtt.client"})
@IntegrationComponentScan
@SpringBootApplication
public class MqttJavaApplication {
@Autowired
@Qualifier("mqttOutFlow.input")
private DirectChannel mqttOutFlowInput;
But in this case autowiring failed
No qualifying bean of type 'org.springframework.integration.channel.DirectChannel' available:
but the bean exists:
Object object = context.getBean("mqttOutFlow.input");
log.info("type " + object.getClass().getName());
2024-02-22T16:21:25.280+01:00 INFO 26304 --- [ main] d.k.d.t.p.m.client.MqttJavaApplication : type org.springframework.integration.channel.DirectChannel
I got this option from the text.
My question is, why autowiring does not work and what is the "best" option publish MQTT Messages via Spring Integration?
Upvotes: 1
Views: 68