10

我编写了一个 Java 程序来连接到 Websphere MQ 以发布消息。我在 Websphere MQ Explore 中创建了 JNDI 名称空间、连接工厂、目的地和队列管理器。当我运行我的程序时,它显示从toClassCastException的类型转换。stringConnectionFactory

这是我的代码。任何人都可以帮助解决这个问题。

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

    }
}

新发布者.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();

        }    
    }           
}

在这些行上得到错误:

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

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

4 回答 4

3

您缺少一些MQ 客户端的JAR来使其正常工作。我遇到了同样的错误,经过进一步调查,我最终得到了这个罐子列表,以使其正常工作:

  • com.ibm.mq.jmqi.jar
  • com.ibm.mqjms.jar
  • dhbcore.jar
  • fscontext.jar
  • providerutil.jar
  • com.ibm.mq.headers.jar
于 2016-02-18T11:51:18.910 回答
1

您从 jndi 上下文中收到的是参考。这是构建连接工厂的方法,我怀疑无法找到负责此操作的类,因为所需的 MQ jar 不在类路径中。错误信息不直观。

如果做不到这一点,我发现调试 jndi 查找问题的好方法是获取上下文并在其上执行 list(),打印出返回的每个对象的详细信息,这样您就可以清楚地了解目录中的确切内容。

于 2013-07-24T10:05:27.210 回答
0

将下面的罐子添加到classpath

sibc.jms
sibc.jndi
于 2013-10-03T06:23:19.863 回答
0

就我而言,我们使用 TIBCO JMS 添加以下依赖项已解决强制转换问题

ClassCastException:javax.naming.Reference 不能转换为 javax.jms.ConnectionFactory

implementation 'com.tibco.tibjms:tibjms:5.1.4'
于 2020-06-08T06:47:27.023 回答