【发布时间】: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,但在我的代码中它是正确的 ;)