为什么java代码正确编译并在使用传递参数调用类时抛出错误?

时间:2014-01-30 16:37:27

标签: java jms

我一直关注这个网址 http://docs.oracle.com/javaee/1.4/tutorial/doc/JMS5.html 我创建了connectionfactory,队列和主题。我一直在使用上面url中给出的源代码来连接JMS。虽然我运行以下代码,但没有任何编译错误,但代码在传递参数时不会运行。

import javax.jms.*;
import javax.naming.*;

public class SimpleProducer {
    public static void main(String[] args) {
        final int NUM_MSGS;

        if ((args.length < 1) || (args.length > 2)) {
            System.out.println("Program takes one or two arguments: " +
                "<dest_name> [<number-of-messages>]");
            System.exit(1);
        }

        String destName = new String(args[0]);
        System.out.println("Destination name is " + destName);

        if (args.length == 2) {
            NUM_MSGS = (new Integer(args[1])).intValue();
        } else {
            NUM_MSGS = 1;
        }

        Context jndiContext = null;

        try {
            jndiContext = new InitialContext();
        } catch (NamingException e) {
            System.out.println("Could not create JNDI API context: " + e.toString());
            System.exit(1);
        }

        /*
         * Look up connection factory and destination.  If either
         * does not exist, exit.  If you look up a
         * TopicConnectionFactory or a QueueConnectionFactory,
         * program behavior is the same.
         */
        ConnectionFactory connectionFactory = null;
        Destination dest = null;

        try {
            connectionFactory = (ConnectionFactory) jndiContext.lookup("jms/ConnectionFactory");
            dest = (Destination) jndiContext.lookup(destName);
        } catch (Exception e) {
            System.out.println("JNDI API lookup failed: " + e.toString());
            e.printStackTrace();
            System.exit(1);
        }

        /*
         * Create connection.
         * Create session from connection; false means session is
         * not transacted.
         * Create producer and text message.
         * Send messages, varying text slightly.
         * Send end-of-messages message.
         * Finally, close connection.
         */
        Connection connection = null;
        MessageProducer producer = null;

        try {
            connection = connectionFactory.createConnection();

            Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            producer = session.createProducer(dest);

            TextMessage message = session.createTextMessage();

            for (int i = 0; i < NUM_MSGS; i++) {
                message.setText("This is message " + (i + 1));
                System.out.println("Sending message: " + message.getText());
                producer.send(message);
            }

            /*
             * Send a non-text control message indicating end of
             * messages.
             */
            producer.send(session.createMessage());
        } catch (JMSException e) {
            System.out.println("Exception occurred: " + e.toString());
        } finally {
            if (connection != null) {
                try {
                    connection.close();
                } catch (JMSException e) {
                }
            }
        }
    }
}

当我使用eclipse运行代码并传递类的参数,即paramater myqueue和3.它返回以下错误。

Destination name is jms/Queue
JNDI API lookup failed: 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
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 SimpleProducer.main(SimpleProducer.java:53)

我是否必须在glassfish服务器的jndi.properties配置文件中配置任何内容。目前jndi.properties final已经

java.naming.factory.initial=com.sun.enterprise.naming.impl.SerialInitContextFactory

1 个答案:

答案 0 :(得分:0)

您提供的URL有“创建JMS管理对象”部分。您应该按照这些步骤正确配置测试环境。

本教程教您如何使用Web界面进行配置,但您应该能够在服务器上找到配置文件,以便稍后将相同的数据应用到Eclipse环境中。

Glassfish有一个类似的Web控制台。您应该能够以与教程相似的方式创建Connection Factory。