java - 使用 Spring Integration 命令总线启动/停止通过注释定义的 AbstractEndpoints

标签 java spring spring-integration spring-annotations

我使用注释来定义inboundChannelAdapter。 MyConfig.java

      @Bean
      @InboundChannelAdapter(value = "inputChannel", poller = @Poller(fixedRate = "5000"),
          autoStartup = "true")
      public MessageSource<File> input() {
        final FileReadingMessageSource result = new FileReadingMessageSource();
        result.setDirectory(new File("src/main/java/test"));
        return result;
      }

我尝试用以下方式停止它:

 MessageChannel controlChannel = ctx.getBean("controlBusChannel", MessageChannel.class);
      AbstractPollingEndpoint endpoint = ctx.getBean("myConfig.input.inboundChannelAdapter", AbstractPollingEndpoint.class);
      if(endpoint.isRunning()){
        controlChannel.send(new GenericMessage<String>("@"+endpoint.getComponentName()+".stop()"));
      }

但是我得到了这个异常:

Exception in thread "main" org.springframework.messaging.MessageHandlingException: error occurred in message handler [ServiceActivator for [org.springframework.integration.handler.ExpressionCommandMessageProcessor@11b2519] (controlBus)]; nested exception is org.springframework.expression.spel.SpelEvaluationException: EL1008E:(pos 10): Property or field 'input' cannot be found on object of type 'test.commandbus.MyConfig$$EnhancerBySpringCGLIB$$11f6b4ee' - maybe not public?
    at org.springframework.integration.handler.AbstractMessageHandler.handleMessage(AbstractMessageHandler.java:139)
    at org.springframework.integration.dispatcher.AbstractDispatcher.tryOptimizedDispatch(AbstractDispatcher.java:116)
    at org.springframework.integration.dispatcher.UnicastingDispatcher.doDispatch(UnicastingDispatcher.java:147)
    at org.springframework.integration.dispatcher.UnicastingDispatcher.dispatch(UnicastingDispatcher.java:120)
    at org.springframework.integration.channel.AbstractSubscribableChannel.doSend(AbstractSubscribableChannel.java:77)
    at org.springframework.integration.channel.AbstractMessageChannel.send(AbstractMessageChannel.java:442)
    at org.springframework.integration.channel.AbstractMessageChannel.send(AbstractMessageChannel.java:392)
    at test.commandbus.Main.main(Main.java:32)
Caused by: org.springframework.expression.spel.SpelEvaluationException: EL1008E:(pos 10): Property or field 'input' cannot be found on object of type 'test.commandbus.MyConfig$$EnhancerBySpringCGLIB$$11f6b4ee' - maybe not public?
    at org.springframework.expression.spel.ast.PropertyOrFieldReference.readProperty(PropertyOrFieldReference.java:224)
    at org.springframework.expression.spel.ast.PropertyOrFieldReference.getValueInternal(PropertyOrFieldReference.java:94)
    at org.springframework.expression.spel.ast.PropertyOrFieldReference.getValueInternal(PropertyOrFieldReference.java:81)
    at org.springframework.expression.spel.ast.CompoundExpression.getValueRef(CompoundExpression.java:57)
    at org.springframework.expression.spel.ast.CompoundExpression.getValueInternal(CompoundExpression.java:87)
    at org.springframework.expression.spel.ast.SpelNodeImpl.getTypedValue(SpelNodeImpl.java:131)
    at org.springframework.expression.spel.standard.SpelExpression.getValue(SpelExpression.java:330)
    at org.springframework.integration.util.AbstractExpressionEvaluator.evaluateExpression(AbstractExpressionEvaluator.java:150)
    at org.springframework.integration.util.AbstractExpressionEvaluator.evaluateExpression(AbstractExpressionEvaluator.java:145)
    at org.springframework.integration.handler.ExpressionCommandMessageProcessor.processMessage(ExpressionCommandMessageProcessor.java:73)
    at org.springframework.integration.handler.ServiceActivatingHandler.handleRequestMessage(ServiceActivatingHandler.java:89)
    at org.springframework.integration.handler.AbstractReplyProducingMessageHandler.handleMessageInternal(AbstractReplyProducingMessageHandler.java:99)
    at org.springframework.integration.handler.AbstractMessageHandler.handleMessage(AbstractMessageHandler.java:127)
    ... 7 more

有没有一种方法可以停止适配器而不必为此定义特定的@Bean?

最佳答案

您的解决方案如下:

"@'"+endpoint.getComponentName()+"'.stop()"

让它看起来像最后:

@'myConfig.input.inboundChannelAdapter'.stop()

否则我们有这个 SpEL:

@myConfig.input.inboundChannelAdapter.stop()

因此,它评估 myConfig bean 并尝试访问其 input 属性。

关于java - 使用 Spring Integration 命令总线启动/停止通过注释定义的 AbstractEndpoints,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41107121/

相关文章:

java - Java 中的抽象类。

java - 将 clojure/scala repl 附加到正在运行的 JVM

java - Spring中的策略模式和依赖注入(inject)

java - spring-kafka中多个KafkaListener实现共享逻辑

java - Spring Kafka 以 TCP 端口作为生产者

spring-integration - Spring集成中的串行处理

java - Spring Integration Kafka 适配器不产生消息

java - 二叉树的数组表示(打印方法)

java - 为协作 Android 应用程序创建全局 Realm 文件

java - Spring 集成结束流程