2 years ago
#12839
Rayyan
Reference of IBM MQ CachingConnectionFactory when Sleuth is enabled
While trying to autowire IBM MQ CachingConnectionFactory or JMS ConnectionFactory, application is always injected by the LazyConnectionFactory wrapper of Spring Cloud Sleuth, if Sleuth is enabled for JMS.
How do we get the reference or qualifier to access the actual IBM MQ CachingConnectionFactory or JMS ConnectionFactory. This is required to programmatically configure the SSL, Backoff, Timeout, additional JmsTemplate, etc
spring.sleuth.messaging.jms.enabled: true
@Bean("mqJmsFactory")
public JmsListenerContainerFactory<?> mqJmsFactory(ConnectionFactory connectionFactory, DefaultJmsListenerContainerFactoryConfigurer configurer)
throws Exception {
@Bean("mqCachingConnectionFactory")
public CachingConnectionFactory mqCachingConnectionFactory(
@Qualifier("mqQueueConnectionFactory") MQQueueConnectionFactory mqQueueConnectionFactory) {
@Primary
@Bean("jmsTemplate")
public JmsTemplate jmsTemplate(ConnectionFactory connectionFactory) {
Caused by: org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'xxxxxxxxxxxxFactory' is expected to be of type 'com.ibm.mq.jms.MQQueueConnectionFactory' but was actually of type 'org.springframework.cloud.sleuth.brave.instrument.messaging.LazyConnectionFactory'
at org.springframework.beans.factory.support.DefaultListableBeanFactory.checkBeanNotOfRequiredType(DefaultListableBeanFactory.java:1818)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1795)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1355)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1309)
at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:887)
at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:791)
... 89 common frames omitted
If javax.jms.ConnectionFactory is injected, still behind the scenes the implementation class is LazyConnectionFactory and it is failing when we want to actually make changes on the underlying Implementation class.
// session cache
var cachingConnectionFactory = (CachingConnectionFactory) connectionFactory;
cachingConnectionFactory.setSessionCacheSize(sessionCacheSize);
Now the exception thrown is,
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mqJmsFactory' defined in com.fab.integration.config.MQJMSFactoryConfig: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.jms.config.JmsListenerContainerFactory]: Factory method 'mqJmsFactory' threw exception; nested exception is java.lang.ClassCastException: class org.springframework.cloud.sleuth.brave.instrument.messaging.LazyConnectionFactory cannot be cast to class org.springframework.jms.connection.CachingConnectionFactory (org.springframework.cloud.sleuth.brave.instrument.messaging.LazyConnectionFactory and org.springframework.jms.connection.CachingConnectionFactory are in unnamed module of loader 'app')
at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:658)
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:638)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1352)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1195)
at
UPDATE:
We use the JMS Spring Boot starter for creating our JMS resources and once the resources are created by Spring Boot we do enhancements on top of those beans. For ex, additional JmsTemplate bean to handle different timeToLive, receiveTimeout, sessionTransacted or Not, etc
<dependency>
<groupId>com.ibm.mq</groupId>
<artifactId>mq-jms-spring-boot-starter</artifactId>
<version>2.6.2</version>
</dependency>
spring-jms
spring-cloud-sleuth
0 Answers
Your Answer