0

我在 Eclipse 中使用 Java 代码,它应该从 Jboss EAP 6.4 上的 JMS 队列中连续读取 JMS 消息。但是,当我运行这个程序时,我遇到了异常。我试图做一些故障排除,但我现在卡住了

我使用的代码如下(Jboss的实际IP替换为#.“remote://#.#.#.#:4447”)

import java.util.Hashtable;
import javax.jms.Connection;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueReceiver;
import javax.jms.QueueSession;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import EDU.oswego.cs.dl.util.concurrent.CountDown;

public class GetMessageTriggerResponses
{
    static CountDown done = new CountDown(10);

    QueueConnection conn;
    QueueSession session;
    Queue recvq;

    public static class ExListener implements MessageListener 
    {
        public void onMessage(Message msg) 
        {
            done.release();
            TextMessage tm = (TextMessage) msg;
            try {
                System.out.println("Received message: \n" + tm.getText());
            } catch (Throwable t) {
                t.printStackTrace();
            }
        }
    }

    public void setup(String[] args) throws JMSException, NamingException, InterruptedException
    {
        Hashtable env = new Hashtable();
        env.put(InitialContext.INITIAL_CONTEXT_FACTORY,"org.jboss.naming.remote.client.InitialContextFactory");
        env.put(InitialContext.PROVIDER_URL, "remote://#.#.#.#:4447");  // DEV-ENV

        Context context = new InitialContext(env);
        QueueConnectionFactory connectionFactory = (QueueConnectionFactory) context
                .lookup("jms/RemoteConnectionFactory");

        Connection connection = connectionFactory.createConnection();

        recvq = (Queue) context.lookup(args[0]);

        System.out.println("listening on "+args[0]);

        session = conn.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE);
        conn.start();

        System.out.println("Begin recv message");
        QueueReceiver receiver = session.createReceiver(recvq);
        receiver.setMessageListener(new ExListener());

        done.acquire();
    }

     public void stop()
        throws JMSException
    {
        conn.stop();
        session.close();
        conn.close();
    }

    public static void main(String args[])
        throws Exception
    {
       /*if (args.length < 1) {
            System.err.println("Usage: java prog_name msg-trigger-qname");
            return;
        }*/

        System.out.println("Begin GetMessageTriggerResponses");
        GetMessageTriggerResponses client = new GetMessageTriggerResponses();
        client.setup(args);
        client.stop();
        System.out.println("End GetMessageTriggerResponses");
        System.exit(0);
    }
}

我得到了例外:

Begin GetMessageTriggerResponses
Feb 04, 2019 12:49:42 AM org.xnio.Xnio <clinit> INFO: XNIO Version 3.0.17.GA-redhat-1 Feb 04, 2019 12:49:42 AM org.xnio.nio.NioXnio <clinit> INFO: XNIO NIO Implementation Version 3.0.17.GA-redhat-1 
Feb 04, 2019 12:49:42 AM org.jboss.remoting3.EndpointImpl <clinit> INFO: JBoss Remoting version 3.3.12.Final-redhat-2 
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
    at GetMessageTriggerResponses.setup(GetMessageTriggerResponses.java:56)
    at GetMessageTriggerResponses.main(GetMessageTriggerResponses.java:88)
4

1 回答 1

0

问题是您将一个数组传递给setup()of 方法,GetMessageTriggerResponses然后尝试使用该数组中的一个值。这是有问题的行:

recvq = (Queue) context.lookup(args[0]);

当您调用setup()或访问数组时,您应该检查以确保它具有预期数量的值。这是基本的输入验证。您实际上有一些代码来验证 中的输入main(),但由于某种原因您已将其注释掉。我建议您恢复该代码以避免这种情况ArrayIndexOutOfBoundsException

于 2019-02-04T19:52:01.043 回答