【问题标题】:Spring Integration @ServiceActivator or @Bean definition to support @Profile usageSpring 集成 @ServiceActivator 或 @Bean 定义以支持 @Profile 使用
【发布时间】: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


    【解决方案1】:

    您只能在@Beans 上使用@Profile,而不能在@Bean 中的@ServiceActivator 方法上使用。

    Annotations on @Bean Methods

    您的@Bean 应如下所示:

    @Bean
    @Profile("...")
    @ServiceActivator(inputChannel = "...")
    public MessageHandler profiledHandler() {
        ServiceActivatingHandler handler = new ServiceActivatingHandler(myServiceBean, 
            "handlerSiteX");
        handler.setOutputChannelName("combinedChannel");
        return handler;
    {
    

    输出通道在处理程序上进行;是的,如果需要,将创建输入通道。

    【讨论】:

    • 通常@ServiceActivator 在一些@Componenet 中定义。如果配置文件不活动,您可以在该类上使用@Profile,并且将跳过它的bean(连同@ServiceActivator)。当你不在同一个类中混合测试和生产代码时,以这种方式设计你的项目真的更好。
    • 谢谢 Gary 和 @Artem,我会继续尝试您的建议。
    【解决方案2】:

    您必须赢得客户的信任,这就是为什么您必须利用您的资源来提供出色的定制包装解决方案。定制包装解决方案需要指定,因此,您需要使用具有卓越品质的个性化产品包装。 Custom packaging

    【讨论】:

    • 可能与我的用例无关。谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-16
    • 2012-01-17
    • 1970-01-01
    相关资源
    最近更新 更多