JmsListenerContainerFacotory 不可用:NoSuchBeanDefinitionException,即使配置了 DefaultJmsListenerContainerFactory

JmsListenerContainerFacotory Not available: NoSuchBeanDefinitionException, even though have configured a DefaultJmsListenerContainerFactory

我正在编写 unit/integration 测试来检查是否正确调用了使用 IBM MQ 的 JMS 侦听器。我的 IncomingDataListener 中有两个侦听器。它们用于同一队列中的两种类型的数据,由 MyDataProcessorService 和 MyOtherDataProcessorService 处理。

如下所示,我模拟了 MyDataProcessorService 和 MyOtherDataProcessorService 这两个服务。

但我没有使用 JmsTemplate 将消息发送到我的队列,以测试是否调用了正确的方法。

相反,我通过直接调用 recieveMessage() 方法并检查是否调用了正确的数据处理器服务来直接读取 xml 文件。

请告诉我是否应该使用 JmsTemplate 将消息发送到队列,而不是继续使用这种直接调用接收者的方法?

@RunWith(SpringRunner.class)
@Import({IncomingDataListener.class, DefaultJmsListenerContainerFactory.class})
@ContextConfiguration(classes = JmsConfig.class)
@PropertySource("classpath:config/application-test.properties")
public class IncomingDataListenerTest {

    @Autowired
    private IncomingDataListener incomingDataListener;

    @MockBean
    private MyDataProcessorService myDataProcessorService;

    @MockBean
    private MyOtherDataProcessorService myOtherDataProcessorService;


    private String myDataFileString;
    
    @PostConstruct
    public void setup() throws IOException {
        myDataFileString = XmlUtils.readFile("FileHavingXMLData.xml");
    }

    @Test
    public void testProcessMyData() throws StaticDataValidationException {
        incomingDataListener.receiveMessage(myDataFileString);// Directly  I am calling the IncomingDataListener classes recieveMessage method, is it right or wrong??
        verify(myDataProcessorService, times(1)).process(any());
        verify(myOtherDataProcessorService, times(0)).processOther(any());
        
    }
}

这是配置class,最初我想创建一个 JmsTemplate 来将消息发送到队列,然后在侦听器端测试是否调用了正确的处理器!!

@Configuration
@PropertySource("classpath:config/application-test.properties")
@Slf4j
public class JmsConfig {

    @Value("${mq.jms.host}")
    private String host;

    @Value("${mq.jms.port}")
    private Integer port;

    @Value("${mq.jms.queue.manager}")
    private String queueManager;

    @Value("${mq.jms.channel}")
    private String channel;

    @Value("${mq.inbound.queue}")
    private String queue;

    @Value("${mq.timeout}")
    private Long timeout;

    @Bean
    public MQQueueConnectionFactory getConnectionFactory() {
        MQQueueConnectionFactory mqQueueConnectionFactory = new MQQueueConnectionFactory();
        try {
            mqQueueConnectionFactory.setHostName(host);
            mqQueueConnectionFactory.setQueueManager(queueManager);
            mqQueueConnectionFactory.setChannel(channel);
        } catch (JMSException e){
            log.debug("Error creating Connection Factory");
        }
        return mqQueueConnectionFactory;
    }

    @Bean
    public JmsTemplate jmsTemplateFactory(MQQueueConnectionFactory mqQueueConnectionFactory) {
        JmsTemplate jmsTemplate = new JmsTemplate(mqQueueConnectionFactory);
        return jmsTemplate;
    }

    @Bean
    public JmsListenerContainerFactory queueContainer(MQQueueConnectionFactory mqQueueConnectionFactory) {
        DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
        factory.setConnectionFactory(mqQueueConnectionFactory);
        return factory;
    }
}

据我所知,我已经完成了所有要求的事情,但我遇到了如下所列的异常。

java.lang.IllegalStateException: Failed to load ApplicationContext

    at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:132)
    at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:123)
    at org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener.postProcessFields(MockitoTestExecutionListener.java:95)
    at org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener.injectFields(MockitoTestExecutionListener.java:79)
    at org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener.prepareTestInstance(MockitoTestExecutionListener.java:54)
    at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:244)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:227)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runReflectiveCall(SpringJUnit4ClassRunner.java:289)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:291)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:246)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access[=12=]0(ParentRunner.java:58)
    at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:268)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
    at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
    at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:230)
    at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:58)
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'jmsListenerContainerFactory' available
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:814)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1282)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:297)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:207)
    at org.springframework.jms.config.JmsListenerEndpointRegistrar.resolveContainerFactory(JmsListenerEndpointRegistrar.java:159)
    at org.springframework.jms.config.JmsListenerEndpointRegistrar.registerAllEndpoints(JmsListenerEndpointRegistrar.java:143)
    at org.springframework.jms.config.JmsListenerEndpointRegistrar.afterPropertiesSet(JmsListenerEndpointRegistrar.java:135)
    at org.springframework.jms.annotation.JmsListenerAnnotationBeanPostProcessor.afterSingletonsInstantiated(JmsListenerAnnotationBeanPostProcessor.java:210)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:912)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:878)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550)
    at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:128)
    at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:60)
    at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.delegateLoading(AbstractDelegatingSmartContextLoader.java:275)
    at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.loadContext(AbstractDelegatingSmartContextLoader.java:243)
    at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:99)
    at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:124)
    ... 25 more

请告诉我如何走出这个难题。

你的工厂有一个非标准的 bean 名称;默认为 jmsListenerContainerFactory;如果您将其命名为其他名称,则必须在 @JmsListener 注释中指定 bean 名称。

两者都

   @Bean
    public JmsListenerContainerFactory jmsListenerContainerFactory(MQQueueConnectionFactory mqQueueConnectionFactory) {
        DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
        factory.setConnectionFactory(mqQueueConnectionFactory);
        return factory;
    }

   @Bean("jmsListenerContainerFactory")
    public JmsListenerContainerFactory queueContainer(MQQueueConnectionFactory mqQueueConnectionFactory) {
        DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
        factory.setConnectionFactory(mqQueueConnectionFactory);
        return factory;
    }
    /**
     * The bean name of the {@link org.springframework.jms.config.JmsListenerContainerFactory}
     * to use to create the message listener container responsible for serving this endpoint.
     * <p>If not specified, the default container factory is used, if any.
     */
    String containerFactory() default "";

我认为以下其中一项应该适合您

  1. 在配置文件中将方法名称 queueContainer 重命名为 jmsListenerContainerFactory
  2. 在配置文件中使用 @Bean("jmsListenerContainerFactory") 而不是 @Bean
  3. 将要注入工厂的属性的 type 更改为 JmsListenerContainerFactory
  4. 将要注入工厂的属性的 name 更改为 jmsListenerContainerFactory

您可以从以下来源了解更多关于 spring 如何命名 bean 并将它们连接到 类 的信息: