Spring Integration Java DSL配置POJO Service Activator及nullChannel问题
问题1解答
你可以直接使用.handle()方法绑定你写的MessageProcessor处理器,有两种常用传参方式:
- 如果
MessageProcessor已经被Spring容器管理(比如加了@Component注解),直接注入实例后传入bean实例+要调用的方法名即可:
// 方式1:保留原有的theOtherChannel中转逻辑,单独定义Service Activator流 @Bean public IntegrationFlow processorFlow(MessageProcessor messageProcessor) { return IntegrationFlows.from(theOtherChannel()) .handle(messageProcessor, "handle") .get(); } // 方式2:直接合并到原有inputFlow,省略中间channel步骤,逻辑和原XML/POJO配置完全等价 public IntegrationFlow inputFlow(MessageProcessor messageProcessor) { return IntegrationFlows.from(inputChannel()) .log(LoggingHandler.Level.DEBUG, "com.dash.messages") .transform(Transformers.fromJson(MessageWrapper.class, customObjectMapper())) .handle(messageProcessor, "handle") .get(); }
注意:POJO上的@ServiceActivator注解在DSL配置场景下可以省略,DSL的.handle()会自动完成方法参数绑定,包括@Headers注解标注的头信息也会自动注入。
问题2解答
Java DSL中存在null channel概念,和XML配置的nullChannel完全对应:
- Spring上下文默认会自动注册一个名为
nullChannel的单例Bean,类型为org.springframework.integration.channel.NullChannel,所有发往这个channel的消息都会被直接丢弃。 - 你可以通过两种方式引用:
- 直接使用名称:
.channel("nullChannel") - 使用DSL内置工具类获取实例:
.channel(MessageChannels.nullChannel())
- 直接使用名称:
额外说明:你当前的handle方法返回值为void,处理完消息后默认不会产生输出消息,不需要额外显式配置输出到nullChannel;如果你的Service Activator方法有返回值,但不需要向下游传递,才需要显式配置输出到nullChannel。
内容的提问来源于stack exchange,提问作者Dhiren Dash
相关产品推荐
相关产品推荐

