Reputation: 95
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)
Upvotes: 0
Views: 469
Reputation: 34998
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
.
Upvotes: 0