Я определил публичный модуль Maven с именем SZ-Common-MQTT, и эффект, который я хочу достичь, является то, что любой другой модуль, который зависит от Этот модуль может напрямую использовать компоненты сообщения, связанные с MQTT. Зависимость этого модуля Maven заключается в следующем < /p>
Код: Выделить всё
4.0.0
com.sz
sz-common
${revision}
sz-common-mqtt
org.springframework.boot
spring-boot-starter-integration
org.springframework.integration
spring-integration-mqtt
org.eclipse.paho
org.eclipse.paho.mqttv5.client
Код: Выделить всё
@MessagingGateway(defaultRequestChannel = "mqttOutboundChannel")
public interface MqttGateway {
void sendToMqtt(String data);
}
< /code>
package com.sz.mqtt.config;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.paho.mqttv5.client.MqttConnectionOptions;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.integration.annotation.IntegrationComponentScan;
import org.springframework.integration.dsl.IntegrationFlow;
import org.springframework.integration.dsl.MessageChannels;
import org.springframework.integration.mqtt.outbound.Mqttv5PahoMessageHandler;
import org.springframework.messaging.MessageHandler;
import org.springframework.messaging.converter.*;
import java.util.concurrent.Executors;
@Configuration
@IntegrationComponentScan("com.sz.mqtt.config")
@Slf4j
@ConfigurationProperties(prefix = "mqtt")
@Data
public class MqttConfig {
private String clientIdInbound;
private String clientIdOutbound;
private String url;
private String password;
private String username;
@Bean
public MqttConnectionOptions mqttConnectOptions(){
MqttConnectionOptions options = new MqttConnectionOptions();
options.setServerURIs(new String[] { url});
options.setUserName(username);
options.setPassword(password.getBytes());
options.setAutomaticReconnect(true);
return options;
}
@Bean
public SimpleMessageConverter simpleMessageConverter(){
return new SimpleMessageConverter();
}
@Bean
public MessageHandler mqttOutboundHandler(MqttConnectionOptions connectionOptions) {
Mqttv5PahoMessageHandler messageHandler = new Mqttv5PahoMessageHandler(connectionOptions,clientIdOutbound);
messageHandler.setAsync(true);
messageHandler.setDefaultTopic("defaultTopic");
messageHandler.setDefaultQos(0);
messageHandler.setConverter(simpleMessageConverter());
return messageHandler;
}
@Bean
public IntegrationFlow mqttOutboundFlow(MessageHandler mqttOutboundHandler){
return IntegrationFlow.from("mqttOutboundChannel")
.channel(MessageChannels.executor(Executors.newFixedThreadPool(5)))
.handle(mqttOutboundHandler)
.get();
}
}
Код: Выделить всё
com.sz
sz-common-mqtt
${revision}
< /code>
Тогда я непосредственно вводит компонент MQTtgateway, используя Spring@Component+Lombok < /p>
@Component
@Slf4j
@RequiredArgsConstructor
public class UnitClientManager {
private final Map SESSION_MAP = new ConcurrentHashMap();
private final MqttGateway mqttGateway;
private final IntegrationFlowContext integrationFlowContext;
.......... other info
}
< /code>
Когда я запускаю Springboot, я получаю следующую ошибку < /p>
***************************
APPLICATION FAILED TO START
***************************
Description:
Parameter 0 of constructor in sz.device.session.UnitClientManager required a bean of type 'com.sz.mqtt.config.MqttGateway' that could not be found.
Action:
Consider defining a bean of type 'com.sz.mqtt.config.MqttGateway' in your configuration.
Подробнее здесь: https://stackoverflow.com/questions/794 ... -component