英文:
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 = "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;
}
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 = "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;
}
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 = "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 ;
}
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 @Bean
s, not on @ServiceActivator
methods that are in a @Bean
.
See Annotations on @Bean Methods.
Your @Bean
should look like this:
@Bean
@Profile("...")
@ServiceActivator(inputChannel = "...")
public MessageHandler profiledHandler() {
ServiceActivatingHandler handler = new ServiceActivatingHandler(myServiceBean,
"handlerSiteX");
handler.setOutputChannelName("combinedChannel");
return handler;
{
The output channel goes on the handler; yes, the input channel will be created if needed.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论