如何设置Context.INITIAL_CONTEXT_FACTORY? NoInitialContextException?

时间:2014-11-12 17:17:52

标签: java java-ee jms jndi websphere-8

我是JMS和Websphere服务器的新手,我正在尝试从我的Java代码访问在Websphere Application Server 8上配置的JMS队列。我无法准确理解应为Context.INITIAL_CONTEXT_FACTORY设置的值。它应该是类的完全限定类名还是特定于应用程序服务器的东西?

Hashtable environment = new Hashtable();
        environment.put(Context.INITIAL_CONTEXT_FACTORY, "com.ibm.websphere.naming.WsnInitialContextFactory");
        environment.put(Context.PROVIDER_URL, "iiop://localhost:9081");

将Context.INITIAL_CONTEXT_FACTORY的值设置为完全限定的类名,即com.ms.test.Demo我收到NoInitialContextException。

PFB我正在使用的代码 -

package com.jms.test;

import java.util.Hashtable;
import javax.jms.Queue;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

public class Demo {
  public static void main(String[] args) throws NamingException {
        System.out.println("Start.....");
        Hashtable environment = new Hashtable();
        environment.put(Context.INITIAL_CONTEXT_FACTORY, "com.jms.test.Demo");
        environment.put(Context.PROVIDER_URL, "iiop://localhost:9081");
       //String pUrl = System.getProperty(Context.PROVIDER_URL);
        //System.out.println("*******"+pUrl+"********");
        InitialContext ctx = new InitialContext(environment);
        Queue queue = (Queue) ctx.lookup("jms/TestQ111200");
        System.out.println("*** Queue is *** "+queue.toString());
  }}

我已使用以下链接中给出的步骤在Websphere Application Server中进行了JMS配置: http://pic.dhe.ibm.com/infocenter/iisinfsv/v8r1/index.jsp?topic=/com.ibm.swg.im.iis.infoservdir.user.doc/topics/t_isd_user_setting_up_jms_in_was.html

1 个答案:

答案 0 :(得分:1)

在连接到WebSphere时,您始终使用以下内容,而不是您自己的类。

environment.put(Context.INITIAL_CONTEXT_FACTORY, "com.ibm.websphere.naming.WsnInitialContextFactory");

需要正确连接到WAS。 对于第二个属性,您需要提供bootstrap端口而不是http。通常是2809,在SystemOut.log中查找以下消息:

00000001 NameServerImp A   NMSV0018I: Name server available on bootstrap port 2809.

您还需要特定的罐子,以便客户端连接到WAS JMS。看到 Installing and configuring the Thin Client for JMS with WebSphere Application Server