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

时间:2014-05-28 08:09:19

标签: java glassfish jms

我试图编写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)

2 个答案:

答案 0 :(得分:0)

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

例如,对于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);

答案 1 :(得分:0)

我在 Glassfish 5

上遇到了此错误

我在maven项目中使用了此解决方案,并添加了对pom.xml的依赖性。

Hashtable没有被针刺。

    <!-- https://mvnrepository.com/artifact/org.glassfish.main.appclient/gf-client -->
    <dependency>
        <groupId>org.glassfish.main.appclient</groupId>
        <artifactId>gf-client</artifactId>
        <version>5.0</version>
    </dependency>

以下是我的示例,如您的初始代码:

        Context context = new InitialContext();
        IServicioSumarRemote servicioSumarRemote = (IServicioSumarRemote)context.lookup("java:global/wsserver/ServicioSumarWSImpl!bz.soap.services.IServicioSumarRemote");