问题描述
我有一个JmsConfig
配置类,它通过以下方式处理主题的JMS事件:
I got a JmsConfig
configuration class that handles JMS events from a topic in the following way:
- 它定义了一个
@Bean ConnectionFactory
,其中包含ActiveMQ实现 - 它定义了
@Bean JmsListenerContainerFactory
实例化DefaultJmsListenerContainerFactory
并将其通过Boot的DefaultJmsListenerContainerFactoryConfigurer
- 它定义包含
MappingJackson2MessageConverter
的@Bean MessageConverter
并设置自定义ObjectMapper
- 我在服务的方法上使用指向我的工厂的
@JmsListener
注释.这是我唯一可以使用的主题,仅订阅即可.
- It defines a
@Bean ConnectionFactory
, containing an ActiveMQ implementation - It defines a
@Bean JmsListenerContainerFactory
instantiating aDefaultJmsListenerContainerFactory
and passing it through Boot'sDefaultJmsListenerContainerFactoryConfigurer
- It defines a
@Bean MessageConverter
containing aMappingJackson2MessageConverter
and setting a customObjectMapper
- I use
@JmsListener
annotation pointing to myfactory on a method of my service. This is the only use I have for the topic, subscription alone.
现在,我想转到 Spring Integration .经过大量阅读后,并且假设我不需要双向使用(丢弃 Gateways )或轮询机制(丢弃 @InboundChannelAdapter
),我打算使用message-driven-channel-adapter
,以传统的XML配置写成.我发现Java习惯用法应该通过新的Spring Integration DSL库来完成,因此,我在寻找适当的代码段.
Now I want to move to Spring Integration. After reading a lot, and provided I don't need a bidirectional use (discarding Gateways) neither a polling mechanism (discarding @InboundChannelAdapter
), I am going for a message-driven-channel-adapter
, in traditional XML configuration wording. I found that Java idiom should be accomplished by means of the new Spring Integration DSL library, and thus, I look for the proper snippet.
似乎JmsMessageDrivenChannelAdapter
是适当的等效项,我找到了一种方法:
It seems JmsMessageDrivenChannelAdapter
is the proper equivalent, and I found a way:
IntegrationFlows.from(Jms.messageDriverChannelAdapter(...))
但是问题在于,它仅接受ActiveMQ ConnectionFactory或AbstractMessageListenerContainer
,但没有我的引导预配置的JmsListenerContainerFactory
!
But the problem is that this only accepts the ActiveMQ ConnectionFactory or an AbstractMessageListenerContainer
, but no my boot pre-configured JmsListenerContainerFactory
!
这应该如何最终实现?
推荐答案
JmsListenerContainerFactory
专用于@JmsListener
,它是用于配置DefaultMessageListenerContainer
的更高级别的抽象.引导程序不为原始DefaultMessageListenerContainer
提供自动配置选项;您必须自己连接.但是您仍然可以使用Boot属性...
JmsListenerContainerFactory
is specific for the @JmsListener
, it's a higher level abstraction used to configure a DefaultMessageListenerContainer
. Boot does not provide an auto configuration option for a raw DefaultMessageListenerContainer
; you have to wire it up yourself. But you can still use the Boot properties...
@Bean
public IntegrationFlow flow(ConnectionFactory connectionFactory,
JmsProperties properties) {
return IntegrationFlows.from(Jms.messageDrivenChannelAdapter(container(connectionFactory, properties)))
...
.get();
}
private DefaultMessageListenerContainer container(ConnectionFactory connectionFactory,
JmsProperties properties) {
DefaultMessageListenerContainer container = new DefaultMessageListenerContainer();
container.setConcurrentConsumers(properties.getListener().getConcurrency());
container.setMaxConcurrentConsumers(properties.getListener().getMaxConcurrency());
...
return container;
}
这篇关于迁移JMS事件以侦听Spring Boot with Spring Boot的正确最终方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!