【发布时间】:2021-01-08 08:33:44
【问题描述】:
我对服务激活器的使用及其定义方式有疑问。
我有 3 个服务激活器,它们从不同的输入通道获取消息并将它们发送到单个输出通道。这是在“开发”环境中设计的......
@ServiceActivator(inputChannel = "irregularMessageChannel_1", outputChannel = "combinedChannel")
public String handlerSite1(String data, @Header(IpHeaders.IP_ADDRESS) String connectionId) {
if (log.isDebugEnabled())
log.debug("content received from : {} data : {} ", connectionId, data);
return data;
}
@ServiceActivator(inputChannel = "irregularMessageChannel_2", outputChannel = "combinedChannel")
public String handlerSite2(String data, @Header(IpHeaders.IP_ADDRESS) String connectionId) {
if (log.isDebugEnabled())
log.debug("content received from : {} data : {} ", connectionId, data);
return data;
}
@ServiceActivator(inputChannel = "irregularMessageChannel_3", outputChannel = "combinedChannel")
public String handlerSite3(String data, @Header(IpHeaders.IP_ADDRESS) String connectionId) {
if (log.isDebugEnabled())
log.debug("content received from : {} data : {} ", connectionId, data);
return data;
}
但在 prod 或 preprod 环境中,我需要再添加一个...所以我检查了使用 @Profile 注释和 @ServiceActivator 如下所示
@ServiceActivator(inputChannel = "irregularMessageChannel_X", outputChannel = "combinedChannel")
@Profile("prod")
public String handlerSiteX(String data, @Header(IpHeaders.IP_ADDRESS) String connectionId) {
if (log.isDebugEnabled())
log.debug("content received from : {} data : {} ", connectionId, data);
return data;
}
但据我了解,@Profile 不适用于 @ServiceActivator,它也需要 @Bean 定义。
但是
当我用谷歌搜索它时,它写道,如果我使用@Bean 定义,我应该返回 MessageHandler... 我只是创建 MessageHandler 并返回它...
@Bean
@ServiceActivator(inputChannel = "irregularMessageChannel_X",outputChannel = "combinedChannel")
@Profile("prod")
public MessageHandler handlerSiteX() {
MessageHandler handler = new MessageHandler() {
@Override
public void handleMessage(Message<?> message) throws MessagingException {
if (log.isDebugEnabled())
log.debug("content received from : {} data : {} ", message.getHeaders().get(IpHeaders.IP_ADDRESS), message);
}
};
return handler ;
}
问题部分 - 1
现在我有一个问题,我怎样才能像在@ServiceActivator 中使用的那样将消息发送到输出通道?
与@Bean 注解一起,@ServiceActivator 中不允许使用 outputChannel 属性。或者有什么方法可以在没有 @Bean 但有 @ServiceActivator 的情况下使用 @Profile 注释?
编辑
问题部分 - 2
我也应该自己创建输入通道吗?如果我使用@Bean 定义还是像@ServiceActivator 一样自动创建?
感谢您的帮助。
【问题讨论】:
标签: java spring spring-boot spring-integration