JMS接入websphere MQ 出现异常,高手赐教
JMS 接口接入Websphere MQ 出现以下异常:
Exception in thread "main" javax.naming.NoInitialContextException: Cannot instantiate class: com.sun.jndi.fscontext.RefFSContextFactory [Root exception is java.lang.ClassNotFoundException: com.sun.jndi.fscontext.RefFSContextFactory]
at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:657)
at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:288)
at javax.naming.InitialContext.init(InitialContext.java:223)
at javax.naming.InitialContext.<init>(InitialContext.java:197)
at devx.articles.mqjms.JNDIUtil.<init>(JNDIUtil.java:32)
at devx.articles.mqjms.Tester.main(Tester.java:47)
Caused by: java.lang.ClassNotFoundException: com.sun.jndi.fscontext.RefFSContextFactory
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:247)
at com.sun.naming.internal.VersionHelper12.loadClass(VersionHelper12.java:46)
at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:654)
... 5 more
以下为代码:
package devx.articles.mqjms;
// JMS classes
import javax.jms.JMSException;
import javax.jms.Queue;
import javax.jms.QueueConnectionFactory;
// JNDI classes
import javax.naming.InitialContext;
import javax.naming.Context;
import javax.naming.NamingException;
// Standard Java classes
import java.util.Hashtable;
/**
*
* 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 );
}
/**
* A convenience method that returns QueueConnectionFactory objects (no casting required)
* @param factoryName QueueConnectionFactory JNDI name
* @return QueueConnectionFactory object
* @throws NamingException
*/
public QueueConnectionFactory getQueueConnectionFactory(String factoryName) throws NamingException
{
return (QueueConnectionFactory) getObjectByName(factoryName);
}
/**
* A convenience method that returns Queue objects (no casting required)
* @param queueName
* @return
* @throws NamingException
*/
public Queue getQueue(String queueName) throws NamingException
{
return (Queue) getObjectByName(queueName);
}
}
--------------------------
package devx.articles.mqjms;
//JMS classes
import javax.jms.Queue;
import javax.jms.QueueSession;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.JMSException;
import javax.jms.Session;
import javax.jms.QueueSender;
import javax.jms.QueueReceiver;
import javax.jms.TextMessage;
import javax.jms.Message;
//JNDI classes
import javax.naming.NamingException;
//Standard Java classes
/**
*
* A class to test JMS with IBM MQ
*
*/
public class Tester
{
public static String icf = "com.sun.jndi.fscontext.RefFSContextFactory";
public static String url = "file:/E:/vgc2/test/JNDI-Directory";
public static void main(String[] vars) throws JMSException, NamingException
{
QueueSession session = null;
QueueConnection connection = null;
QueueConnectionFactory factory = null;
QueueSender queueSender = null;
QueueReceiver queueReceiver= null;
Queue oQueue = null; // A queue to send messages to
Queue iQueue = null; // A queue to receive messages from
try
{
JNDIUtil jndiUtil= new JNDIUtil(icf,url);
factory= jndiUtil.getQueueConnectionFactory("TestQM_QCF");
connection = factory.createQueueConnection();
// Starts (or restarts) a connection's delivery of incoming messages. Messages will not be
// received without this call.
connection.start();
// Indicate a non-transactional session
boolean transacted = false;
session = connection.createQueueSession( transacted, Session.AUTO_ACKNOWLEDGE);
oQueue= jndiUtil.getQueue("OutputTestQueue");
queueSender = session.createSender(oQueue);
TextMessage oMsg = session.createTextMessage();
oMsg.setText("www.devx.com");
// You can set other message properties as well
queueSender.send(oMsg);
iQueue= jndiUtil.getQueue("InputTestQueue");
queueReceiver = session.createReceiver(iQueue);
Message iMsg = queueReceiver.receive(1000);
if ( iMsg != null )
System.out.println( ((TextMessage)iMsg).getText() );
else
System.out.println( "No messages in queue " );
}
finally
{
// Always release resources
if ( queueReceiver!= null )
queueReceiver.close();
if ( queueSender!= null )
queueSender.close();
if ( session!= null )
session.close();
if ( connection!= null )
{
connection.close();
}
}
}
}
请高手不吝赐教,谢谢。
[解决办法]
com.sun.jndi.fscontext.RefFSContextFactory 这个类在 MQ 安装后的目录下某个地方有的,你用 WinRAR 打开一个 zip/jar/rar 文件,然后 .. 退出一个目录下,再搜索 RefFSContextFactory.class,看它在哪个 jar 文件中。
用 WinRAR 搜索比 Windows 搜索的好处就在它能深入探测压缩包里面的文件,查找 .class 文件非常适用。
你用 MQ 自带的 docs 下面的 java 样例里面的测试 JMS 的代码就能看到它用的是哪些 classpath 条目,那个 JMS 测试样例也是用 FS Context 来做 JNDI 的。
[解决办法]
很明显找不到com.sun.jndi.fscontext.RefFSContextFactory这个类,看看你的LIB目录配对了没有
[解决办法]
找不到类com.sun.jndi.fscontext.RefFSContextFactory
fscontext.jar的位置加到classpath里就OK了