gpt4 book ai didi

java - 连接到 glassFish 服务器上的 JNDI 时出现 NoInitialContextException

转载 作者:行者123 更新时间:2023-11-29 08:48:31 26 4
gpt4 key购买 nike

我正在尝试编写一个 JMS 应用程序,我使用 glassFish 管理页面构建了一个 ConnectionFactory 和 Qeueu,我想知道如何让我的应用程序知道服务器上构建的 jndi 以便能够发送消息。

import java.util.Hashtable;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueReceiver;
import javax.jms.QueueSender;
import javax.jms.QueueSession;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.swing.JOptionPane;

public class TestJMS {
public static void main(String[] args) {
String input = JOptionPane.showInputDialog("Enter JMS Client Type");

if (input.equals("1")) {
QueueConnection queueConnection = null;

try {


Context context = new InitialContext();
QueueConnectionFactory queueConnectionFactory = (QueueConnectionFactory) context.lookup("jms/ConnectionFactory");

String queueName = "jms/Queue";

Queue queue = (Queue) context.lookup(queueName);

queueConnection = queueConnectionFactory.createQueueConnection();
QueueSession queueSession = queueConnection.createQueueSession(false,Session.AUTO_ACKNOWLEDGE);

QueueSender queueSender = queueSession.createSender(queue);

TextMessage message = queueSession.createTextMessage();
message.setText("This is a TextMessage");
queueSender.send(message);
System.out.println("Message sent.");

} catch (NamingException ex) {
ex.printStackTrace();
System.out.println("Naming Exception");
ex.printStackTrace();
} catch (JMSException ex) {
ex.printStackTrace();
System.out.println("JMS Exception");
} finally {
if (queueConnection != null) {
try {
queueConnection.close();
} catch (JMSException ex) {
}
}
}
}}}

正在处理异常

javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file:  java.naming.factory.initialNaming Exception

at javax.naming.spi.NamingManager.getInitialContext(Unknown Source)
at javax.naming.InitialContext.getDefaultInitCtx(Unknown Source)
at javax.naming.InitialContext.getURLOrDefaultInitCtx(Unknown Source)
at javax.naming.InitialContext.lookup(Unknown Source)
at TestJMS.main(TestJMS.java:37)
javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
at javax.naming.spi.NamingManager.getInitialContext(Unknown Source)
at javax.naming.InitialContext.getDefaultInitCtx(Unknown Source)
at javax.naming.InitialContext.getURLOrDefaultInitCtx(Unknown Source)
at javax.naming.InitialContext.lookup(Unknown Source)
at TestJMS.main(TestJMS.java:37)

最佳答案

您需要在创建 InitialContext 时传递一个属性对象。属性对象指定连接到特定后端服务器所需的工厂类和提供者信息(以及其他设置)。

例如,对于 IBM WebSphere,

Properties p = new Properties();   
String conFact = "com.ibm.websphere.naming.WsnInitialContextFactory"l
p.setProperty(Context.INITIAL_CONTEXT_FACTORY, conFact);
p.setProperty(Context.PROVIDER_URL, <providerInfo>);
ic = new InitialContext(p);

对于 GlassFish,这将是客户端 jar 中的一个连接工厂,它应该是请求 IC 时指定。

这是来自 Oracle 的部分示例 http://docs.oracle.com/cd/E19879-01/821-0029/aeqba/index.html

Hashtable  env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.fscontext.RefFSContextFactory");
env.put(Context.PROVIDER_URL, "file:///C:/imq_admin_objects");
Context ctx = new InitialContext(env);

关于java - 连接到 glassFish 服务器上的 JNDI 时出现 NoInitialContextException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23906034/

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