我想收到mqtt消息,这就是我为mqtt使用spring实现的原因,包括paho library和mosquito brocker。
问题是当我在本地(在我自己的电脑上)测试mqtt频道时,一切都运行良好。但是当我将配置传输到服务器并在那里开始测试时,我收到了一个异常
ERROR taskScheduler-1 inbound.MqttPahoMessageDrivenChannelAdapter:289 - Exception while connecting and subscribing Client is disconnected (32101)
这种行为可能是什么原因?
我的配置文件如下:
@Configuration
@PropertySource("${configuration.properties.path}")
@ComponentScan({"package.1", "package.2", "package.3"})
@IntegrationComponentScan({"package.1", "package.2"})
@EnableIntegration
public class TransportMqttConfigBean {
@Autowired
Environment env;
@Bean
public MqttPahoClientFactory mqttClientFactory(){
return new DefaultMqttPahoClientFactory();
}
@Bean
@Description("Channel for mqtt messages that leave outbound adapter (should be received)")
public MessageChannel mqttInputChannel() {
return new DirectChannel();
}
@Bean
@Description("Channel for mqtt messages that come into inbound adapter (should be sent)")
public MessageChannel mqttOutputChannel() {
return new DirectChannel();
}
@Bean
@Description("Channel for mqtt messages with payload")
public MessageChannel mqttGeneralChannel() {
return new DirectChannel();
}
@Bean
@Description("Channel for mqtt errors")
public MessageChannel mqttErrorChannel() {
return new DirectChannel();
}
@Bean
@Description("mqtt inbound adapter: receives mqtt messages")
public MessageProducer mqttInboundAdapter() {
log.info("creating mqtt inbound adapter");
MqttPahoMessageDrivenChannelAdapter adapter =
new MqttPahoMessageDrivenChannelAdapter(
env.getProperty("mqtt.hostname")+":" +env.getProperty("mqtt.port"),
"name",
"#");
adapter.setCompletionTimeout(5000);
adapter.setConverter(new DefaultPahoMessageConverter());
adapter.setQos(1);
adapter.setOutputChannel(mqttInputChannel());
adapter.setErrorChannel(mqttErrorChannel());
return adapter;
}
@Bean
@Description("mqtt outbound adapter: sends mqtt messages")
@ServiceActivator(inputChannel = "mqttOutputChannel")
public MessageHandler mqttOutboundAdapter() {
log.info("creating mqtt outbound adapter");
MqttPahoMessageHandler messageHandler =
new MqttPahoMessageHandler(
env.getProperty("mqtt.hostname")+":" +env.getProperty("mqtt.port"),
"name2",
mqttClientFactory());
messageHandler.setAsync(true);
messageHandler.setDefaultTopic("escosTopic");
return messageHandler;
}
@Bean
public ApplicationListener<?> applicationListener(){
ApplicationEventListeningMessageProducer producer=new ApplicationEventListeningMessageProducer();
producer.setEventTypes(MqttConnectionFailedEvent.class);
producer.setOutputChannel(mqttErrorChannel());
return producer;
}
}
答案 0 :(得分:0)
ClientId必须是唯一的。发生这种情况是因为您的两个订户具有相同的客户端ID。首先在本地计算机上运行,然后在服务器上运行。 Second与Mqtt经纪人断开连接。
在您的代码中,您是这样做的
MqttPahoMessageHandler messageHandler =
new MqttPahoMessageHandler(
env.getProperty("mqtt.hostname")+":" +env.getProperty("mqtt.port"),
"name2",
mqttClientFactory());
看到您将其称为“ name2” 尝试使用该代码段
String url = env.getProperty("mqtt.hostname") + ":" + env.getProperty("mqtt.port");
String clientId = "subAt" + InetAddress.getLocalHost().getHostName();
MqttPahoMessageHandler messageHandler =
new MqttPahoMessageHandler(url,clientId,mqttClientFactory());
它将添加到运行您的软件的计算机的clientId主机名。