【问题标题】:Spring Integration - Convert Service Activator with Java ConfigurationSpring Integration - 使用 Java 配置转换服务激活器
【发布时间】:2019-11-18 13:17:22
【问题描述】:

我尝试将 Spring Integration 示例 (https://github.com/spring-projects/spring-integration-samples/tree/master/basic/helloworld) 中的“Hello World 示例”从 XML 转换为 Java 配置(因此使用 @Configuration 注释)。

配置类如下所示:

@Configuration
@EnableIntegration
public class BasicIntegrationConfig{

    @Bean
    public DirectChannel inputCHannel() {
        return new DirectChannel();
    }

    @Bean
    public QueueChannel outputChannel() {
        return new QueueChannel();
    }

    @Bean
    @ServiceActivator(inputChannel= "inputChannel", outputChannel= "outputChannel" )
    public MessageHandler fileWritingMessageHandler() {
        MessageHandler mh =  new MessageHandler() {
            @Override
            public void handleMessage(Message<?> message) throws MessagingException {
                System.out.println("Message payload: " + message.getPayload());
            }
        };
        return mh;
    }
}

为了测试它,我使用了示例项目中提供的main()

DirectChannel fileChannel = applicationContext.getBean("inputChannel", DirectChannel.class);
QueueChannel outputChannel = applicationContext.getBean("outputChannel", QueueChannel.class);
System.out.println("********** SENDING MESSAGE");
fileChannel.send(new GenericMessage<>("test"));
System.out.println(outputChannel.receive(0).getPayload());

我在控制台中看到“消息负载:测试”,但不幸的是,我没有在输出通道上收到消息(我在 outputChannel.receive(0) 上有一个 NullPointerException。 您知道为什么服务激活器不将消息发送到输出通道吗?

【问题讨论】:

  • 您有一个错字:inputCHannel(大写 H)在您的班级 BasicIntegrationConfig 中出现两次,但您在 main() 中将其称为 inputChannel(小写 h)。
  • 确实谢谢 Jesper,但在我的代码中它是正确的 ;)

标签: spring spring-integration


【解决方案1】:

您的MessageHandler 返回void

您需要改为子类AbstractReplyProducingMessageHandler

【讨论】:

    【解决方案2】:

    谢谢你,Gary,切换到后效果很好:

        @Bean
        @ServiceActivator(inputChannel= "inputChannel")
        public AbstractReplyProducingMessageHandler fileWritingMessageHandler() {
            AbstractReplyProducingMessageHandler  mh = new AbstractReplyProducingMessageHandler() {
                @Override
                protected Object handleRequestMessage(Message<?> message) {
                    String payload= (String)message.getPayload();
                    return "Message Payload : ".concat(payload);
                }
            };
            mh.setOutputChannelName("outputChannel");
            return mh;
        }
    

    附带说明,我必须删除 @ServiceActivator 注释中的 output channel 属性,并将其放入方法体中(如果没有,则为 Bean Validation Exception)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-07-22
      • 1970-01-01
      • 1970-01-01
      • 2017-06-23
      • 1970-01-01
      • 1970-01-01
      • 2016-03-29
      • 2015-12-12
      相关资源
      最近更新 更多