Spring Integration @ServiceActivator 或 @Bean 定义以支持 @Profile 用法

huangapple go评论80阅读模式
英文:

Spring Integration @ServiceActivator or @Bean definition to support @Profile usage

问题

我有关于服务激活器Service Activator的用法和定义方式的问题

我有三个服务激活器它们从不同的输入通道接收消息并将其发送到单个输出通道这是在dev环境中设计的

@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;
}

但是在生产或预生产环境中我需要再添加一个 因此我尝试在@ServiceActivator中使用@Profile注解如下所示

@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中那样自动创建

感谢您的帮助
英文:

I have question about usage of service activator and its definition way.

I have 3 service activatiors that are taking messages from different input channels and sends them to single output channel. This is designed in "dev" environment...

@ServiceActivator(inputChannel = &quot;irregularMessageChannel_1&quot;, outputChannel = &quot;combinedChannel&quot;)
public String handlerSite1(String data, @Header(IpHeaders.IP_ADDRESS) String connectionId) {
if (log.isDebugEnabled())
log.debug(&quot;content received from : {} data : {} &quot;, connectionId, data);
return data;
}
@ServiceActivator(inputChannel = &quot;irregularMessageChannel_2&quot;, outputChannel = &quot;combinedChannel&quot;)
public String handlerSite2(String data, @Header(IpHeaders.IP_ADDRESS) String connectionId) {
if (log.isDebugEnabled())
log.debug(&quot;content received from : {} data : {} &quot;, connectionId, data);
return data;
}
@ServiceActivator(inputChannel = &quot;irregularMessageChannel_3&quot;, outputChannel = &quot;combinedChannel&quot;)
public String handlerSite3(String data, @Header(IpHeaders.IP_ADDRESS) String connectionId) {
if (log.isDebugEnabled())
log.debug(&quot;content received from : {} data : {} &quot;, connectionId, data);
return data;
}

But in prod or preprod environment i need to add one more... so i checked to use @Profile annotation with @ServiceActivator like below

@ServiceActivator(inputChannel = &quot;irregularMessageChannel_X&quot;, outputChannel = &quot;combinedChannel&quot;)
@Profile(&quot;prod&quot;)
public String handlerSiteX(String data, @Header(IpHeaders.IP_ADDRESS) String connectionId) {
if (log.isDebugEnabled())
log.debug(&quot;content received from : {} data : {} &quot;, connectionId, data);
return data;
}

But as far as i understood, @Profile is not working with @ServiceActivator and it requires @Bean Definition too.

But

When i googled it, it is written that if i use @Bean definition, i should return MessageHandler... I simply create MessageHandler and return it...

@Bean
@ServiceActivator(inputChannel = &quot;irregularMessageChannel_X&quot;,outputChannel = &quot;combinedChannel&quot;)
@Profile(&quot;prod&quot;)
public MessageHandler handlerSiteX() {
MessageHandler handler = new MessageHandler() {
@Override
public void handleMessage(Message&lt;?&gt; message) throws MessagingException {
if (log.isDebugEnabled())
log.debug(&quot;content received from : {} data : {} &quot;, message.getHeaders().get(IpHeaders.IP_ADDRESS), message);
}
};
return handler ;
}

Question Part - 1

Now i have question that how can i send my message to output channel like i use in @ServiceActivator ?

Together with @Bean annotation it is not allowed to use outputChannel attribute in @ServiceActivator. Or is there any way to use @Profile annotation without @Bean but with @ServiceActivator ?

> EDIT

Question Part - 2

Also should i create input channel by my self ? If ı use @Bean definition or is it created automatically like in @ServiceActivator ?

Thanks for your help.

答案1

得分: 3

你只能在 @Bean 上使用 @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;
{

输出通道添加到处理器上;是的,如果需要的话,输入通道将会被创建。

英文:

You can only use @Profile on @Beans, not on @ServiceActivator methods that are in a @Bean.

See Annotations on @Bean Methods.

Your @Bean should look like this:

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

The output channel goes on the handler; yes, the input channel will be created if needed.

huangapple
  • 本文由 发表于 2020年9月22日 16:35:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/64005964.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定