使用Spring Integration Command Bus启动/停止通过注释定义的AbstractEndPoint



我使用注释来定义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属性。

最新更新