程序中的异常以读取JMS消息

时间:2019-02-03 13:59:40

标签: java jms jms-queue

我在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)

1 个答案:

答案 0 :(得分:0)

问题在于您要将 empty 数组传递到setup()的{​​{1}}方法中,然后尝试使用该数组中的值。这是有问题的行:

GetMessageTriggerResponses

无论是调用recvq = (Queue) context.lookup(args[0]); 还是访问数组,都应检查以确保其具有期望的值数。这是基本的输入验证。实际上,您有一些代码可以验证setup()中的输入,但是出于某种原因您已经将其注释掉了。我建议您还原该代码以避免此main()