10

I wrote a Java program to connect to Websphere MQ to publish messages. I created a JNDI namespace, connection factory, destinations, and queue manager in Websphere MQ Explore. When I am running my program it is showing ClassCastException for type casting from string to ConnectionFactory.

Here is my code. Can anyone help resolve this problem.

JNDIUtil.java

package com.tradefinance.jms.util;

//JMS classes
import javax.jms.JMSException;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;

//JNDI classes
import javax.naming.InitialContext;
import javax.naming.Context;
import javax.naming.NamingException;

//Standard Java classes
import java.util.Hashtable;
import java.util.Properties;

/**
* 
* A wrapper class for JNDI calls
*
*/
public class JNDIUtil
{
    private Context context;

    public JNDIUtil(String icf, String url) throws JMSException, NamingException
    {
        Hashtable environment = new Hashtable();

        environment.put(Context.INITIAL_CONTEXT_FACTORY, icf ); 
        environment.put(Context.PROVIDER_URL, url);

     context= new InitialContext( environment );

    }

    /**
     * @param ObjName Object Name to be retrieved
     * @return Retrieved Object
     * @throws NamingException
     */
    private Object getObjectByName(String ObjName) throws NamingException
    {

        return context.lookup(ObjName);
    }

    /**
     * @param factoryName Factory Name
     * @return ConnectionFactory object
     * @throws NamingException
     */
    public ConnectionFactory getConnectionFactory(String factoryName) throws NamingException
    {
        return (ConnectionFactory) getObjectByName(factoryName);
    }

    /**
     * @param destinationName Destination Name
     * @return ConnectionFactory object
     * @throws NamingException
     */
    public Destination getDestination(String destinationName) throws NamingException
    {
        return (Destination) getObjectByName(destinationName);

    }
}

NewPublisher.java

package com.tradefinance.jms.topics;

//JMS classes
import javax.jms.JMSException;
import javax.jms.ConnectionFactory;
import javax.jms.Connection;
import javax.jms.Session;
import javax.jms.Destination;
import javax.jms.MessageProducer;
import javax.jms.TextMessage;

//JNDI classes
import javax.naming.NamingException;

import com.tradefinance.jms.util.JNDIUtil;

/**
 * A class to demonstrate how to a publish to a topic.
 */
public class NewsPublisher
{

    public static String icf = "com.sun.jndi.fscontext.RefFSContextFactory";
    public static String url = "file:/C:/JNDI-Directory/";

    public static void main(String[] vars) throws JMSException, NamingException
    {

        ConnectionFactory factory = null;
        Connection connection = null;
        Session session = null;
        Destination destination= null; // a destination can be a topic or a queue
        MessageProducer producer= null; 


        try
        {   

            JNDIUtil jndiUtil= new JNDIUtil(icf,url);

            factory= jndiUtil.getConnectionFactory("TestQM1ConnectionFactory");

            connection = factory.createConnection();            
            connection.start();

            // Indicate a non-transactional session 
            boolean transacted = false;
            session = connection.createSession( transacted, Session.AUTO_ACKNOWLEDGE);          

            destination = jndiUtil.getDestination("NewsTopic");

            producer = session.createProducer(destination);

            TextMessage message = session.createTextMessage("No News is Good News!");
            producer.send(message); 

            System.out.println("NewsPublisher: Message Publication Completed");

        }
        finally
        {
            // Always release resources

            if ( producer!= null )
                producer.close();   

            if ( session!= null )
                session.close();

            if ( connection!= null )
                connection.close();

        }    
    }           
}

Getting the error on these lines:

return (ConnectionFactory) getObjectByName(factoryName); 
in JNDIUtil.java

factory= jndiUtil.getConnectionFactory("TestQM1ConnectionFactory");
in NewPublisher.java
Glenn Slayden
  • 17,543
  • 3
  • 114
  • 108
seshank
  • 101
  • 1
  • 1
  • 4

4 Answers4

3

You are missing some JARs of MQ Client to get this working. I had the same error, and after some further investigation, I ended up with this list of Jars in order to get this working:

  • com.ibm.mq.jmqi.jar
  • com.ibm.mqjms.jar
  • dhbcore.jar
  • fscontext.jar
  • providerutil.jar
  • com.ibm.mq.headers.jar
Ualter Jr.
  • 2,320
  • 1
  • 25
  • 28
  • Note to future visitors to this answer, in IBM MQ 8.0 and later you can use a single IBM jar called `com.ibm.allclient.jar`. You will also need `jms.jar` for JMS applications. If utilizing JNDI then `fscontext.jar` and `providerutil.jar` are also required. The last 3 are distributed by IBM but are actually from Oracle. – JoshMc Jun 08 '20 at 07:03
1

What you received back from the jndi context was a reference. This is a recipe to build the connection factory and I suspect that the class responsible for this cannot be found because the MQ jars required are not in the classpath. The error message is not intuitive.

Failing that, I find a good way to debug jndi lookup issues is to acquire the context and execute a list() on it, printing out the details of each object returned, just so you're clear on what exactly resides in the directory.

Nicholas
  • 15,916
  • 4
  • 42
  • 66
0

Add the Below jars to the classpath :

sibc.jms
sibc.jndi
Nazik
  • 8,696
  • 27
  • 77
  • 123
0

In my case, we use TIBCO JMS adding the following dependency is resolved casting issue

ClassCastException: javax.naming.Reference cannot be cast to javax.jms.ConnectionFactory

implementation 'com.tibco.tibjms:tibjms:5.1.4'
27P
  • 1,183
  • 16
  • 22