IBM MQ ConnectionFactory cast fails after moving to Spring Boot 3 or 4
Your error
java.lang.ClassCastException: class org.springframework.jms.connection.CachingConnectionFactory cannot be cast to class com.ibm.mq.jms.MQConnectionFactory1 more line it can showFix it fast
Most common cause, 1 of 2The code still uses the javax MQ classes from com.ibm.mq.allclient
Pick the starter release built for your Boot generation and delete any direct com.ibm.mq.allclient dependency.
<dependency>
<groupId>com.ibm.mq</groupId>
<artifactId>mq-jms-spring-boot-starter</artifactId>
<version>4.1.2</version>
</dependency>
// Java imports
import com.ibm.mq.jakarta.jms.MQConnectionFactory;
import com.ibm.msg.client.jakarta.wmq.WMQConstants;Short answer
After a move to Spring Boot 3 or 4, casting the injected jakarta.jms.ConnectionFactory to com.ibm.mq.jms.MQConnectionFactory throws a ClassCastException. The MQ starter now builds com.ibm.mq.jakarta.jms.MQConnectionFactory from com.ibm.mq.jakarta.client and, by default, wraps it in a CachingConnectionFactory. Import the jakarta classes, drop com.ibm.mq.allclient, and tune the factory with an MQConnectionFactoryCustomizer bean.
Which cause do you have?
List the IBM MQ jars on the classpath and the MQ classes the code imports. A javax-era jar or import points to cause 1; clean jakarta imports point to cause 2.
mvn dependency:tree -Dincludes=com.ibm.mq
grep -rn "import com.ibm.mq\|import com.ibm.msg" src/main/javaThen find the line you see in this list and open its fix:
- 1The code still uses the javax MQ classes from com.ibm.mq.allclient
Imports name com.ibm.mq.jms.MQConnectionFactory or com.ibm.msg.client.wmq.WMQConstants, or the tree lists com.ibm.mq.allclient - 2The injected bean is Spring's caching or pooling wrapper
Imports are already jakarta, yet printing connectionFactory.getClass() shows CachingConnectionFactory or JmsPoolConnectionFactory
What does the error look like?
java.lang.ClassCastException: class org.springframework.jms.connection.CachingConnectionFactory cannot be cast to class com.ibm.mq.jms.MQConnectionFactoryjava.lang.ClassCastException: class com.ibm.mq.jakarta.jms.MQConnectionFactory cannot be cast to class com.ibm.mq.jms.MQConnectionFactory
Cause 1 of 2
The code still uses the javax MQ classes from com.ibm.mq.allclient
You seeImports name com.ibm.mq.jms.MQConnectionFactory or com.ibm.msg.client.wmq.WMQConstants, or the tree lists com.ibm.mq.allclient
Check
The IBM starter for Spring Boot 2 was built on com.ibm.mq.allclient, the javax.jms client. The Boot 3 and Boot 4 builds switch to com.ibm.mq.jakarta.client, and their factory code creates com.ibm.mq.jakarta.jms.MQConnectionFactory. Both classes share a simple name but live in different packages, so the old one can never hold the new object.
grep -rn "com.ibm.mq.jms.MQConnectionFactory" src/main/javaFix
Pick the starter release built for your Boot generation and delete any direct com.ibm.mq.allclient dependency. Then move imports to the jakarta packages, including WMQConstants.
<dependency>
<groupId>com.ibm.mq</groupId>
<artifactId>mq-jms-spring-boot-starter</artifactId>
<version>4.1.2</version>
</dependency>
// Java imports
import com.ibm.mq.jakarta.jms.MQConnectionFactory;
import com.ibm.msg.client.jakarta.wmq.WMQConstants;Verify
The dependency tree shows only com.ibm.mq.jakarta.client, the build compiles, and the app connects to the queue manager at start-up.
mvn dependency:tree -Dincludes=com.ibm.mqConfirmed by raw.githubusercontent.com
Cause 2 of 2
The injected bean is Spring's caching or pooling wrapper
You seeImports are already jakarta, yet printing connectionFactory.getClass() shows CachingConnectionFactory or JmsPoolConnectionFactory
Check
The MQ starter's auto-configuration hands out Spring's CachingConnectionFactory around the MQ factory unless spring.jms.cache.enabled is false, and a pooled-jms JmsPoolConnectionFactory when ibm.mq.pool.enabled is true. In both setups the bean you inject is the wrapper, so casting it to any MQConnectionFactory class fails at runtime.
System.out.println(connectionFactory.getClass().getName());Fix
Stop casting. Declare an MQConnectionFactoryCustomizer bean, which the starter calls with the MQ factory it creates, or set the value as a property: this one exists as ibm.mq.clientId, and other constants go under ibm.mq.additionalProperties.
import com.ibm.mq.spring.boot.MQConnectionFactoryCustomizer;
import com.ibm.msg.client.jakarta.wmq.WMQConstants;
@Bean
MQConnectionFactoryCustomizer mqCustomizer() {
return factory -> {
try {
factory.setStringProperty(WMQConstants.CLIENT_ID, "orders-service");
} catch (jakarta.jms.JMSException e) {
throw new IllegalStateException(e);
}
};
}Verify
The application starts without a ClassCastException and connects to the queue manager.
mvn spring-boot:runConfirmed by raw.githubusercontent.com
How do I stop it coming back?
- Ban com.ibm.mq.allclient and javax.jms imports in Boot 3 or 4 modules with a dependency rule or a grep step in CI.
- Upgrade mq-jms-spring-boot-starter together with Spring Boot, since each starter line is built for one Boot generation.
- Keep MQ tuning in ibm.mq properties or one customizer bean, never in code that casts the injected factory.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine on the same stack: Dispatch Pro.