Spring Integration - MQTT订阅问题

时间:2015-11-23 04:50:53

标签: spring spring-boot spring-integration mqtt

我正在尝试使用MQTT实现Spring集成。我使用Mosquitto作为MQTT经纪人。参考以下link中提供的文档。我创建了一个项目并添加了所有必需的jar文件。当我执行MQTTJavaApplication时。

public class MqttJavaApplication {

     public static void main(String[] args) {
            new SpringApplicationBuilder(MqttJavaApplication.class)
                    .web(false)
                    .run(args);
        }

        @Bean
        public MessageChannel mqttInputChannel() {
            return new DirectChannel();
        }

        @Bean
        public MqttPahoMessageDrivenChannelAdapter inbound() {
            MqttPahoMessageDrivenChannelAdapter adapter =
                    new MqttPahoMessageDrivenChannelAdapter("tcp://localhost:1883", "test",
                                                     "sample");
            adapter.setCompletionTimeout(5000);
            adapter.setConverter(new DefaultPahoMessageConverter());
            adapter.setQos(1);
            adapter.setOutputChannel(mqttInputChannel());
            return adapter;
        }

        @Bean
        @ServiceActivator(inputChannel = "mqttInputChannel")
        public MessageHandler handler() {
            return new MessageHandler() {

                @Override
                public void handleMessage(Message<?> message) {
                    System.out.println("Test##########"+message.getPayload());
                }

            };
        }
}

当我通过MQTT代理发布消息时,我收到以下错误。

[2015-11-23 10:08:19.545] boot - 8100  INFO [main] --- AnnotationConfigApplicationContext: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@2d0e1c: startup date [Mon Nov 23 10:08:19 IST 2015]; root of context hierarchy
[2015-11-23 10:08:19.831] boot - 8100  INFO [main] --- PropertiesFactoryBean: Loading properties file from URL [jar:file:/D:/IoTWorkspace/MQTTTest/WebContent/WEB-INF/lib/spring-integration-core-4.2.1.RELEASE.jar!/META-INF/spring.integration.default.properties]
[2015-11-23 10:08:20.001] boot - 8100  INFO [main] --- DefaultLifecycleProcessor: Starting beans in phase 1073741823
[2015-11-23 10:08:20.104] boot - 8100  INFO [main] --- MqttPahoMessageDrivenChannelAdapter: started inbound
[2015-11-23 10:08:20.112] boot - 8100  INFO [main] --- MqttJavaApplication: Started MqttJavaApplication in 1.602 seconds (JVM running for 2.155)
[2015-11-23 10:13:04.564] boot - 8100 ERROR [MQTT Call: test] --- MqttPahoMessageDrivenChannelAdapter: Unhandled exception for GenericMessage [payload=Testing Subscription, headers={timestamp=1448253784563, id=cd5be974-3b19-8317-47eb-1c139725be24, mqtt_qos=0, mqtt_topic=sample, mqtt_retained=false, mqtt_duplicate=false}]
org.springframework.messaging.MessageDeliveryException: Dispatcher has no subscribers for channel 'unknown.channel.name'.; nested exception is org.springframework.integration.MessageDispatchingException: Dispatcher has no subscribers
    at org.springframework.integration.channel.AbstractSubscribableChannel.doSend(AbstractSubscribableChannel.java:81)
    at org.springframework.integration.channel.AbstractMessageChannel.send(AbstractMessageChannel.java:442)
    at org.springframework.integration.channel.AbstractMessageChannel.send(AbstractMessageChannel.java:392)
    at org.springframework.messaging.core.GenericMessagingTemplate.doSend(GenericMessagingTemplate.java:115)
    at org.springframework.messaging.core.GenericMessagingTemplate.doSend(GenericMessagingTemplate.java:45)
    at org.springframework.messaging.core.AbstractMessageSendingTemplate.send(AbstractMessageSendingTemplate.java:105)
    at org.springframework.integration.endpoint.MessageProducerSupport.sendMessage(MessageProducerSupport.java:105)
    at org.springframework.integration.mqtt.inbound.MqttPahoMessageDrivenChannelAdapter.messageArrived(MqttPahoMessageDrivenChannelAdapter.java:262)
    at org.eclipse.paho.client.mqttv3.internal.CommsCallback.handleMessage(CommsCallback.java:336)
    at org.eclipse.paho.client.mqttv3.internal.CommsCallback.run(CommsCallback.java:148)
    at java.lang.Thread.run(Thread.java:722)
Caused by: org.springframework.integration.MessageDispatchingException: Dispatcher has no subscribers
    at org.springframework.integration.dispatcher.UnicastingDispatcher.doDispatch(UnicastingDispatcher.java:153)
    at org.springframework.integration.dispatcher.UnicastingDispatcher.dispatch(UnicastingDispatcher.java:120)
    at org.springframework.integration.channel.AbstractSubscribableChannel.doSend(AbstractSubscribableChannel.java:77)
    ... 10 more
[2015-11-23 10:13:04.613] boot - 8100 ERROR [MQTT Call: test] --- MqttPahoMessageDrivenChannelAdapter: Lost connection:MqttException; retrying...
[2015-11-23 10:13:04.614] boot - 8100 ERROR [MQTT Call: test] --- MqttPahoMessageDrivenChannelAdapter: Failed to schedule reconnect
java.lang.NullPointerException
    at org.springframework.integration.mqtt.inbound.MqttPahoMessageDrivenChannelAdapter.scheduleReconnect(MqttPahoMessageDrivenChannelAdapter.java:228)
    at org.springframework.integration.mqtt.inbound.MqttPahoMessageDrivenChannelAdapter.connectionLost(MqttPahoMessageDrivenChannelAdapter.java:255)
    at org.eclipse.paho.client.mqttv3.internal.CommsCallback.connectionLost(CommsCallback.java:229)
    at org.eclipse.paho.client.mqttv3.internal.ClientComms.shutdownConnection(ClientComms.java:339)
    at org.eclipse.paho.client.mqttv3.internal.CommsCallback.run(CommsCallback.java:171)
    at java.lang.Thread.run(Thread.java:722)
[2015-11-23 10:13:04.617] boot - 8100  INFO [Thread-0] --- AnnotationConfigApplicationContext: Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@2d0e1c: startup date [Mon Nov 23 10:08:19 IST 2015]; root of context hierarchy
[2015-11-23 10:13:04.619] boot - 8100  INFO [Thread-0] --- DefaultLifecycleProcessor: Stopping beans in phase 1073741823
[2015-11-23 10:13:04.622] boot - 8100 ERROR [Thread-0] --- MqttPahoMessageDrivenChannelAdapter: Exception while unsubscribing
Client is not connected (32104)
    at org.eclipse.paho.client.mqttv3.internal.ExceptionHelper.createMqttException(ExceptionHelper.java:27)
    at org.eclipse.paho.client.mqttv3.internal.ClientComms.sendNoWait(ClientComms.java:132)
    at org.eclipse.paho.client.mqttv3.MqttAsyncClient.unsubscribe(MqttAsyncClient.java:707)
    at org.eclipse.paho.client.mqttv3.MqttAsyncClient.unsubscribe(MqttAsyncClient.java:682)
    at org.springframework.integration.mqtt.inbound.MqttPahoMessageDrivenChannelAdapter.doStop(MqttPahoMessageDrivenChannelAdapter.java:124)
    at org.springframework.integration.endpoint.AbstractEndpoint.doStop(AbstractEndpoint.java:145)
    at org.springframework.integration.endpoint.AbstractEndpoint.stop(AbstractEndpoint.java:128)
    at org.springframework.context.support.DefaultLifecycleProcessor.doStop(DefaultLifecycleProcessor.java:229)
    at org.springframework.context.support.DefaultLifecycleProcessor.access$300(DefaultLifecycleProcessor.java:51)
    at org.springframework.context.support.DefaultLifecycleProcessor$LifecycleGroup.stop(DefaultLifecycleProcessor.java:363)
    at org.springframework.context.support.DefaultLifecycleProcessor.stopBeans(DefaultLifecycleProcessor.java:202)
    at org.springframework.context.support.DefaultLifecycleProcessor.onClose(DefaultLifecycleProcessor.java:118)
    at org.springframework.context.support.AbstractApplicationContext.doClose(AbstractApplicationContext.java:966)
    at org.springframework.context.support.AbstractApplicationContext$1.run(AbstractApplicationContext.java:893)
[2015-11-23 10:13:04.623] boot - 8100 ERROR [Thread-0] --- MqttPahoMessageDrivenChannelAdapter: Exception while disconnecting
Client is disconnected (32101)
    at org.eclipse.paho.client.mqttv3.internal.ExceptionHelper.createMqttException(ExceptionHelper.java:27)
    at org.eclipse.paho.client.mqttv3.internal.ClientComms.disconnect(ClientComms.java:405)
    at org.eclipse.paho.client.mqttv3.MqttAsyncClient.disconnect(MqttAsyncClient.java:524)
    at org.eclipse.paho.client.mqttv3.MqttAsyncClient.disconnect(MqttAsyncClient.java:493)
    at org.eclipse.paho.client.mqttv3.MqttAsyncClient.disconnect(MqttAsyncClient.java:500)
    at org.springframework.integration.mqtt.inbound.MqttPahoMessageDrivenChannelAdapter.doStop(MqttPahoMessageDrivenChannelAdapter.java:131)
    at org.springframework.integration.endpoint.AbstractEndpoint.doStop(AbstractEndpoint.java:145)
    at org.springframework.integration.endpoint.AbstractEndpoint.stop(AbstractEndpoint.java:128)
    at org.springframework.context.support.DefaultLifecycleProcessor.doStop(DefaultLifecycleProcessor.java:229)
    at org.springframework.context.support.DefaultLifecycleProcessor.access$300(DefaultLifecycleProcessor.java:51)
    at org.springframework.context.support.DefaultLifecycleProcessor$LifecycleGroup.stop(DefaultLifecycleProcessor.java:363)
    at org.springframework.context.support.DefaultLifecycleProcessor.stopBeans(DefaultLifecycleProcessor.java:202)
    at org.springframework.context.support.DefaultLifecycleProcessor.onClose(DefaultLifecycleProcessor.java:118)
    at org.springframework.context.support.AbstractApplicationContext.doClose(AbstractApplicationContext.java:966)
    at org.springframework.context.support.AbstractApplicationContext$1.run(AbstractApplicationContext.java:893)
[2015-11-23 10:13:04.624] boot - 8100  INFO [Thread-0] --- MqttPahoMessageDrivenChannelAdapter: stopped inbound

添加@SpringBootApplication Annotation后,请在下面找到堆栈跟踪

[2015-11-26 10:55:46.571] boot - 5524  INFO [main] --- AnnotationConfigApplicationContext: Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@562d4b: startup date [Thu Nov 26 10:55:46 IST 2015]; root of context hierarchy

[2015-11-26 10:55:46.572] boot - 5524 WARN [main] --- AnnotationConfigApplicationContext:从ApplicationListener处理ContextClosedEvent引发的异常 java.lang.IllegalStateException:ApplicationEventMulticaster未初始化 - 调用&#39;刷新&#39;在通过上下文组播事件之前:org.springframework.context.annotation.AnnotationConfigApplicationContext@562d4b:启动日期[Thu Nov 26 10:55:46 IST 2015];上下文层次结构的根     在org.springframework.context.support.AbstractApplicationContext.getApplicationEventMulticaster(AbstractApplicationContext.java:337)     在org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:324)     在org.springframework.context.support.AbstractApplicationContext.doClose(AbstractApplicationContext.java:1025)     在org.springframework.context.support.AbstractApplicationContext.close(AbstractApplicationContext.java:988)     在org.springframework.boot.SpringApplication.run(SpringApplication.java:329)     在org.springframework.boot.builder.SpringApplicationBuilder.run(SpringApplicationBuilder.java:130)     在com.igate.MqttJavaApplication.main(MqttJavaApplication.java:32) [2015-11-26 10:55:46.594] boot - 5524 WARN [main] --- AnnotationConfigApplicationContext:在上下文关闭时从LifecycleProcessor抛出的异常 java.lang.IllegalStateException:LifecycleProcessor未初始化 - 调用&#39;刷新&#39;在通过上下文调用生命周期方法之前:org.springframework.context.annotation.AnnotationConfigApplicationContext@562d4b:启动日期[Thu Nov 26 10:55:46 IST 2015];上下文层次结构的根     在org.springframework.context.support.AbstractApplicationContext.getLifecycleProcessor(AbstractApplicationContext.java:350)     在org.springframework.context.support.AbstractApplicationContext.doClose(AbstractApplicationContext.java:1033)     在org.springframework.context.support.AbstractApplicationContext.close(AbstractApplicationContext.java:988)     在org.springframework.boot.SpringApplication.run(SpringApplication.java:329)     在org.springframework.boot.builder.SpringApplicationBuilder.run(SpringApplicationBuilder.java:130)     在com.igate.MqttJavaApplication.main(MqttJavaApplication.java:32) 线程&#34; main&#34;中的例外情况java.lang.IllegalStateException:必须至少指定一个基本包     在org.springframework.context.annotation.ComponentScanAnnotationParser.parse(ComponentScanAnnotationParser.java:121)     在org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigurationClass(ConfigurationClassParser.java:214)     在org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:149)     在org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:135)     在org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:260)     在org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:203)     在org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:617)     at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:446)     在org.springframework.boot.SpringApplication.refresh(SpringApplication.java:648)     在org.springframework.boot.SpringApplication.run(SpringApplication.java:311)     在org.springframework.boot.builder.SpringApplicationBuilder.run(SpringApplicationBuilder.java:130)     在com.igate.MqttJavaApplication.main(MqttJavaApplication.java:32)

1 个答案:

答案 0 :(得分:0)

回答可能为时已晚,但希望它可以帮助一些初学者。

几件事:-

  • 在主类中声明 @SpringBootApplication,让项目知道它是一个 Spring Boot 应用程序
  • 对于第二个错误,定义项目的基础包,spring boot 将在其中引用配置文件等。为此,在类级别添加 @ComponentScan({"package-name"})

示例:-

@SpringBootApplication
@ComponentScan({"package-name"})
public class Main {
...
}