Spring 集成 - 使用 Java 配置转换服务激活器
Spring Integration - Convert Service Activator with Java Configuration
我尝试将 "Hello World example" 从 Spring 集成示例 (https://github.com/spring-projects/spring-integration-samples/tree/master/basic/helloworld) 从 XML 转换为 Java 配置,(所以 @Configuration
注解).
配置 class 如下所示:
@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());
我在控制台中看到 "Message payload: test",但不幸的是,我没有在输出通道上收到消息(我在 outputChannel.receive(0)
上有一个 NullPointerException
。
您知道为什么 Service Activator 不将消息发送到输出通道吗?
你的MessageHandler
returnsvoid
.
您需要继承 AbstractReplyProducingMessageHandler
。
谢谢 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)。
我尝试将 "Hello World example" 从 Spring 集成示例 (https://github.com/spring-projects/spring-integration-samples/tree/master/basic/helloworld) 从 XML 转换为 Java 配置,(所以 @Configuration
注解).
配置 class 如下所示:
@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());
我在控制台中看到 "Message payload: test",但不幸的是,我没有在输出通道上收到消息(我在 outputChannel.receive(0)
上有一个 NullPointerException
。
您知道为什么 Service Activator 不将消息发送到输出通道吗?
你的MessageHandler
returnsvoid
.
您需要继承 AbstractReplyProducingMessageHandler
。
谢谢 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)。