Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages
Node.js, PHP and APIs

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 show

Fix it fast

Most common cause, 1 of 2

The 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.

Fix
<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;
Did this fix it?
Full steps for this cause: check, fix, verify

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.

  • Applies to Spring Boot 4.1 with mq-jms-spring-boot-starter 4.1.2 (com.ibm.mq.jakarta.client 10.0.1.0), Spring Boot 3.5 with mq-jms-spring-boot-starter 3.5.15, the last Boot 3 release
  • Updated
  • By Maaz Zindani, Senior Software Engineer

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.

Run this first
mvn dependency:tree -Dincludes=com.ibm.mq
grep -rn "import com.ibm.mq\|import com.ibm.msg" src/main/java

Then find the line you see in this list and open its fix:

  1. 1The code still uses the javax MQ classes from com.ibm.mq.allclientImports name com.ibm.mq.jms.MQConnectionFactory or com.ibm.msg.client.wmq.WMQConstants, or the tree lists com.ibm.mq.allclient
  2. 2The injected bean is Spring's caching or pooling wrapperImports 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.MQConnectionFactory
  • java.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.

Check
grep -rn "com.ibm.mq.jms.MQConnectionFactory" src/main/java

Fix

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.

Fix
<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.

Verify
mvn dependency:tree -Dincludes=com.ibm.mq

Confirmed by raw.githubusercontent.com

Did this fix it?

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.

Check
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.

Fix
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.

Verify
mvn spring-boot:run

Confirmed by raw.githubusercontent.com

Did this fix it?

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.

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.