Why does AMQDestination throws NPE when using CachingConnectionFactory?

Solution Verified - Updated -

Environment

  • Red Hat Enterprise MRG Messaging 2.*
  • qpid Java client 0.18-* or older

Issue

When using CachingConnectionFactory together with qpid Java client, NullPointerException (see Diagnostic Steps) is raised whenever a Destination object is used.

Resolution

Diagnostic Steps

Backtrace of uncaught NullPointerException:

java.lang.NullPointerException
    at org.apache.qpid.client.AMQDestination.equals(AMQDestination.java:603)
    at org.springframework.jms.connection.CachingConnectionFactory$DestinationCacheKey.destinationEquals(CachingConnectionFactory.java:475)
    at org.springframework.jms.connection.CachingConnectionFactory$DestinationCacheKey.equals(CachingConnectionFactory.java:482)
    at java.util.HashMap.findNonNullKeyEntry(HashMap.java:525)
    at java.util.HashMap.getEntry(HashMap.java:511)
    at java.util.HashMap.get(HashMap.java:497)
    at org.springframework.jms.connection.CachingConnectionFactory$CachedSessionInvocationHandler.getCachedProducer(CachingConnectionFactory.java:357)
    at org.springframework.jms.connection.CachingConnectionFactory$CachedSessionInvocationHandler.invoke(CachingConnectionFactory.java:322)
    at $Proxy2.createProducer(Unknown Source)
    at org.springframework.jms.remoting.JmsInvokerServiceExporter.writeRemoteInvocationResult(JmsInvokerServiceExporter.java:132)
        ...

Trivial reproducer:

import javax.jms.Destination;
import javax.naming.*;
import java.util.Properties;

public class DestinationTest {
    public static void main(String[] args) throws Exception {
        Properties props = new Properties();
        props.setProperty("java.naming.factory.initial", "org.apache.qpid.jndi.PropertiesFileInitialContextFactory");
        props.setProperty("destination.address1", "address1");
        props.setProperty("destination.address2", "address2");

        Context ctx = new InitialContext(props);
        Destination a1 = (Destination) ctx.lookup("address1");
        Destination a2 = (Destination) ctx.lookup("address2");
        if (a1.equals(a2))
            throw new Exception("No Exception and addresses are equal");
        else
            System.out.println("No exceptions");
    }
}

This solution is part of Red Hat’s fast-track publication program, providing a huge library of solutions that Red Hat engineers have created while supporting our customers. To give you the knowledge you need the instant it becomes available, these articles may be presented in a raw and unedited form.