Spring MQTT Client
Я пытаюсь настроить MQTT Client . вот моя конфигурация:
@Configuration
public class MQTTConfig {
@Bean
public MqttPahoClientFactory mqttClientFactory() {
DefaultMqttPahoClientFactory factory = new DefaultMqttPahoClientFactory();
factory.setServerURIs("tcp://xx.yy.com:12345");
factory.setUserName("abc");
factory.setPassword("123431");
return factory;
}
@Bean
@ServiceActivator(inputChannel = "mqttOutboundChannel")
public MessageHandler mqttOutbound() {
MqttPahoMessageHandler messageHandler =
new MqttPahoMessageHandler("Client_Jan", mqttClientFactory());
messageHandler.setAsync(true);
messageHandler.setDefaultTopic("home/garden/fountain");
return messageHandler;
}
@Bean
public MessageChannel mqttOutboundChannel() {
return new DirectChannel();
}
@MessagingGateway(defaultRequestChannel = "mqttOutboundChannel")
public interface MyGateway {
void sendToMqtt(String data);
}
Я использую инструмент MQTT FX для отправки сообщений на сервер и пытаюсь подписать его с помощью следующего кода.
@Service
public class MQTTReciever implements InitializingBean,DisposableBean{
private final static Logger logger = LoggerFactory.getLogger(MQTTReciever.class);
@Autowired
private MqttPahoClientFactory mqttClientFactory;
private MqttAsyncClient mqttClient=null;
public void establishMQTTConnection(){
try{
mqttClient= mqttClientFactory.getAsyncClientInstance("tcp://xx.yy.com:18583", "Client_Jan");
mqttClient.connect();
mqttClient.subscribe("home/garden/fountain", 1);
if(mqttClient.isConnected()){
logger.info("Connection to MQTT Server established");
//TODO if connection is there , process further
}
}
catch(Exception exception){
logger.info("Connection to MQTT Server failed because :" +exception.getMessage());
}
}
@Override
public void afterPropertiesSet() throws Exception {
this.establishMQTTConnection();
}
Трассировки стека
ERROR [STDERR] at org.eclipse.paho.client.mqttv3.internal.ExceptionHelper.createMqttException(ExceptionHelper.java:31)
15:55:38,674 ERROR [STDERR] at org.eclipse.paho.client.mqttv3.internal.ClientComms.sendNoWait(ClientComms.java:177)
15:55:38,674 ERROR [STDERR] at org.eclipse.paho.client.mqttv3.MqttAsyncClient.subscribe(MqttAsyncClient.java:923)
15:55:38,675 ERROR [STDERR] at org.eclipse.paho.client.mqttv3.MqttAsyncClient.subscribe(MqttAsyncClient.java:875)
15:55:38,675 ERROR [STDERR] at com.ametras.rentconcept.webcore.service.MQTTReciever.establishMQTTConnection(MQTTReciever.java:30)
15:55:38,675 ERROR [STDERR] at com.ametras.rentconcept.webcore.service.MQTTReciever.afterPropertiesSet(MQTTReciever.java:44)
15:55:38,675 ERROR [STDERR] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1573)
15:55:38,675 ERROR [STDERR] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1511)
15:55:38,675 ERROR [STDERR] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:521)
15:55:38,676 ERROR [STDERR] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458)
15:55:38,676 ERROR [STDERR] at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:293)
15:55:38,676 ERROR [STDERR] at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
15:55:38,676 ERROR [STDERR] at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:290)
15:55:38,676 ERROR [STDERR] at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:191)
15:55:38,676 ERROR [STDERR] at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:921)
15:55:38,676 ERROR [STDERR] at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:864)
15:55:38,677 ERROR [STDERR] at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:779)
15:55:38,677 ERROR [STDERR] at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:503)
15:55:38,677 ERROR [STDERR] at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
15:55:38,677 ERROR [STDERR] at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:295)
15:55:38,677 ERROR [STDERR] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1148)
15:55:38,677 ERROR [STDERR] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
15:55:38,677 ERROR [STDERR] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458)
15:55:38,677 ERROR [STDERR] at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:293)
15:55:38,678 ERROR [STDERR] at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
15:55:38,678 ERROR [STDERR] at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:290)
15:55:38,678 ERROR [STDERR] at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:191)
Я получаю исключение, что нет связи с клиентом. Моя цель - только получать сообщения и сохранять их.
Совет пожалуйста.
Спасибо