使用 Spring 与 MQTT 集成时需要 "A Task Scheduler"

"A Task Scheduler" is Required when using Spring Integration with MQTT

我有一个项目,我正在使用 Spring 集成,我在其中连接到代理,然后发布-订阅发送给代理的消息。

当使用@EnableAutoConfiguration 和@ComponentScan 和@Configuration 时,应用程序运行很好,它作为一个整体使用@SpringBootApplication。但是我不想使用@EnableAutoConfiguration,这只是一个要集成到更大的块项目中的POC项目。其中,如果使用 @EnableAutoConfiguration 可能会导致其他不需要的组件出现问题。

那么,我该如何解决这个问题?

这只是项目的摘录,完成后通常应该 运行:


@Configuration
@ComponentScan
public class TestMQTTApp {

    public static void main(String[] args) {
        SpringApplication.run(TestMQTTApp.class,args);
    }

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

    @Bean
    public MqttPahoMessageDrivenChannelAdapter inbound() {
        String clientId = "uuid-" + UUID.randomUUID().toString();
        MqttPahoMessageDrivenChannelAdapter adapter =
                new MqttPahoMessageDrivenChannelAdapter("tcp://localhost:1883", clientId, "topic1"   );
        adapter.setCompletionTimeout(5000);
        adapter.setConverter(new DefaultPahoMessageConverter());
        adapter.setQos(1);
        adapter.setOutputChannel(mqttInputChannel());
        return adapter;
    }
    @Bean
    public EmbeddedServletContainerFactory servletContainer() {
        TomcatEmbeddedServletContainerFactory factory =
                new TomcatEmbeddedServletContainerFactory();
        return factory;
    }
}

错误如下:

org.springframework.context.ApplicationContextException: Failed to start bean 'inbound'; nested exception is java.lang.IllegalStateException: A 'taskScheduler' is required
    at org.springframework.context.support.DefaultLifecycleProcessor.doStart(DefaultLifecycleProcessor.java:176) ~[spring-context-4.3.20.RELEASE.jar:4.3.20.RELEASE]
    at org.springframework.context.support.DefaultLifecycleProcessor.access0(DefaultLifecycleProcessor.java:50) ~[spring-context-4.3.20.RELEASE.jar:4.3.20.RELEASE]
    at org.springframework.context.support.DefaultLifecycleProcessor$LifecycleGroup.start(DefaultLifecycleProcessor.java:350) ~[spring-context-4.3.20.RELEASE.jar:4.3.20.RELEASE]
    at org.springframework.context.support.DefaultLifecycleProcessor.startBeans(DefaultLifecycleProcessor.java:149) ~[spring-context-4.3.20.RELEASE.jar:4.3.20.RELEASE]
    at org.springframework.context.support.DefaultLifecycleProcessor.onRefresh(DefaultLifecycleProcessor.java:112) ~[spring-context-4.3.20.RELEASE.jar:4.3.20.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:880) ~[spring-context-4.3.20.RELEASE.jar:4.3.20.RELEASE]
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.finishRefresh(EmbeddedWebApplicationContext.java:144) ~[spring-boot-1.5.1.RELEASE.jar:1.5.1.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:546) ~[spring-context-4.3.20.RELEASE.jar:4.3.20.RELEASE]
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) ~[spring-boot-1.5.1.RELEASE.jar:1.5.1.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:737) [spring-boot-1.5.1.RELEASE.jar:1.5.1.RELEASE]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:370) [spring-boot-1.5.1.RELEASE.jar:1.5.1.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:314) [spring-boot-1.5.1.RELEASE.jar:1.5.1.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1162) [spring-boot-1.5.1.RELEASE.jar:1.5.1.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1151) [spring-boot-1.5.1.RELEASE.jar:1.5.1.RELEASE]
    at com.harman.TestSpring.main(TestSpring.java:23) [classes/:na]
Caused by: java.lang.IllegalStateException: A 'taskScheduler' is required
    at org.springframework.util.Assert.state(Assert.java:70) ~[spring-core-4.3.20.RELEASE.jar:4.3.20.RELEASE]
    at org.springframework.integration.mqtt.inbound.MqttPahoMessageDrivenChannelAdapter.doStart(MqttPahoMessageDrivenChannelAdapter.java:145) ~[spring-integration-mqtt-4.3.20.RELEASE.jar:4.3.20.RELEASE]
    at org.springframework.integration.endpoint.AbstractEndpoint.start(AbstractEndpoint.java:117) ~[spring-integration-core-4.3.20.RELEASE.jar:4.3.20.RELEASE]
    at org.springframework.context.support.DefaultLifecycleProcessor.doStart(DefaultLifecycleProcessor.java:173) ~[spring-context-4.3.20.RELEASE.jar:4.3.20.RELEASE]
    ... 14 common frames omitted

我在扫描任务计划程序的包时遇到问题,尽管我在代码中没有提到这一点。

POM 的附加信息:

你失踪了@EnableIntegration。这增加了更广泛的 Spring 集成基础设施,包括必需的 TaskScheduler。请参阅文档以获得更多信息:https://docs.spring.io/spring-integration/docs/current/reference/html/overview.html#configuration-enable-integration