0

I am using a Java code in Eclipse which is supposed to read JMS messages continuously from JMS queue on Jboss EAP 6.4. However, I am getting exception when I am running this program. I tried to do some troubleshooting but I am stuck now

The code I am using is as below (Actual IP of Jboss is replaced with #. "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);
    }
}

I am getting the exception:

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)
Justin Bertram
  • 29,372
  • 4
  • 21
  • 43
Sourabh
  • 65
  • 1
  • 4
  • 11
  • I have added jboss-client.jar and jms-1.1.jar as external jar in build path – Sourabh Feb 03 '19 at 14:01
  • Why did you disable your check of the args array? Is this the complete source file? Your exception points to line 56 and I assume that there is no argument given. In that case your assignment of args[0] will fail with ArrayIndexOutOfBoundsException. – Jens Dibbern Feb 03 '19 at 14:21
  • Hi Jens, Yes this is complete source file. I was using this code in in different environment without passing any argument (I was executing this from Eclipse). Should I pass anything here? Or I can change line 56 and 88 to pass any value directly ? Thanks – Sourabh Feb 03 '19 at 14:52
  • If you do not pass an argument, the lookup call will fail because of the empty argument array. You must pass an argument there. – Jens Dibbern Feb 03 '19 at 14:54

1 Answers1

0

The problem is that you're passing in an empty array into the setup() method of GetMessageTriggerResponses and then attempting to use a value from that array. This is the problematic line:

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

Either when you invoke setup() or access the array you should check to make sure it has the expected number of values. This is basic input validation. You actually have some code to validate the input in main(), but you've commented it out for some reason. I recommend you restore that code to avoid this ArrayIndexOutOfBoundsException.

Justin Bertram
  • 29,372
  • 4
  • 21
  • 43