ClassCastException: javax.de nommage.De référence ne peut pas être lancé à javax.jms.ConnectionFactory

J'ai écrit un programme en Java pour se connecter à Websphere MQ pour publier des messages. J'ai créé un espace de nom JNDI, de connexion de l'usine, de destinations, et le gestionnaire de file d'attente dans Websphere MQ Explorer. Lorsque j'exécute mon programme, c'est en montrant ClassCastException pour le type coulée de string à ConnectionFactory.

Voici mon code. Quelqu'un peut-il aider à résoudre ce problème.

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();

        }    
    }           
}

L'obtention de l'erreur sur ces lignes:

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

factory= jndiUtil.getConnectionFactory("TestQM1ConnectionFactory");
in NewPublisher.java

OriginalL'auteur seshank | 2013-07-24