【问题标题】:How to set Context.INITIAL_CONTEXT_FACTORY? NoInitialContextException?如何设置 Context.INITIAL_CONTEXT_FACTORY?没有初始上下文异常?
【发布时间】:2015-01-09 15:28:46
【问题描述】:

我是 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

【问题讨论】:

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


    【解决方案1】:

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

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

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

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

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

    【讨论】:

    • 好的。谢谢。但是我在哪里可以找到 System.out.log?
    • 获得 System.log。问这个会很愚蠢,但我该如何测试呢?我的意思是它应该作为 Java 应用程序运行还是在服务器上运行?
    • @Lizzie 你应该作为 Java 应用程序运行,而不是在服务器上运行,好像我很理解你想从外部 Java 程序而不是部署在 WAS 上的应用程序访问应用程序服务器中的队列
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-14
    • 1970-01-01
    相关资源
    最近更新 更多