gpt4 book ai didi

java - 如何设置 Context.INITIAL_CONTEXT_FACTORY?没有初始上下文异常?

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:02:32 25 4
gpt4 key购买 nike

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

最佳答案

当连接到 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.

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

关于java - 如何设置 Context.INITIAL_CONTEXT_FACTORY?没有初始上下文异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26892729/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com