JMS point to point
Hi,
I'm trying to access a JMS point to point message queue. It is located on a remote machine. I know the queue name, and the ip address of the machine it is located on. I type the following code:
import javax.jms.*;
public class listener {
public static void main(String[] args) {
while (true) {
try {
QueueConnectionFactory connFactory = new com.sun.messaging.QueueConnectionFactory();
QueueConnection conn = connFactory.createQueueConnection();
QueueSession session = conn.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
Queue q = new com.sun.messaging.Queue("QueueName");
QueueReceiver receiver = session.createReceiver(q);
conn.start();
Message m = receiver.receive();
if (m instanceof TextMessage) {
TextMessage txt = (TextMessage) m;
System.out.println("Message Received: " + txt.getText());
}
session.close();
conn.close();
} catch (Exception ex) {
System.out.println(ex.toString());
}
}
}
}
This code runs just fine. However, when it goes to create the message nothing happens. I am guessing that I need to add the ip address of the server where the JMS queue is located. Also, J2EE is not in use, so jndi is not of help in this situation. Any help would be greatly appreciated. Thanks.
Do you get any exceptions?Kaj
kajbja at 2007-7-12 14:46:58 >

Yes and no.
When I run the program in debug mode and step through the program, it terminates at the Message m step. However, when I run the debugger immediatly after it gets past the message m step and just continully runs through the loop. After about 3-4 iterations of the loop it throws:
javax.jms.ResourceAllocationException: [C4073]: A JMS destination limit was reached. Too many Subscribers/Receivers for Queue.
I assume this exception is thrown because I am running 2 applications of the program, and after multiple iterations of the loop, I am logging into the Message queue to many times.
> Yes and no.
>
> When I run the program in debug mode and step through
> the program, it terminates at the Message m step.
> However, when I run the debugger immediatly after it
> gets past the message m step and just continully runs
> through the loop. After about 3-4 iterations of the
> loop it throws:
>
> javax.jms.ResourceAllocationException: [C4073]: A JMS
> destination limit was reached. Too many
> Subscribers/Receivers for Queue.
>
> I assume this exception is thrown because I am
> running 2 applications of the program, and after
> multiple iterations of the loop, I am logging into
> the Message queue to many times.
Why are you recreating all resources in the loop?
Kaj
kajbja at 2007-7-12 14:46:58 >

For some reason, when I put any part of the code outside of the while(true) loop, I get compiler errors. For example, I put everything above the if(message) statement outside the while loop, it gives me an error saying "m cannot be resolved". Also the session.close() says "session cannot be resolved".
I'm also not sure which parts of the set-up should only be created once, and which I need to have running constantly. This program is meant to be a listener, listening to a message queue all the time 24/7. Any suggestions on what to keep inside the while loop?
Thanks for your help so far by the way =D.
> I'm also not sure which parts of the set-up should
> only be created once, and which I need to have
> running constantly. This program is meant to be a
> listener, listening to a message queue all the time
> 24/7. Any suggestions on what to keep inside the
> while loop?
This is what I would keep inside the loop: receiver.receive();
>
> Thanks for your help so far by the way =D.
Np
kajbja at 2007-7-12 14:46:58 >

I still get compiler errors. I moved everything except
receiver.receive();
// Is this message a TextMessage?
if (m instanceof TextMessage) {
// Set the message as a TextMessage
TextMessage txt = (TextMessage) m;
// Acknowledge reception of message
System.out.println("Message Received: " + txt.getText());
}
// Close the session
session.close();
// Close the connection
conn.close();
That is wrapped in a try/catch block. I get the following compiler errors:
reciever cannot be resolved. m cannot be resolved. session cannot be resolved. conn cannot be resolved. I have no idea why this is the case. They are all in the same method (main). However, for some reason if they are outside the while loop the inner loops can not see their variables.
I don't see anyplace where you're getting the JNDI context of the remote machine.PS.
jndi is not in use in this application. There is no jndi.properties on the host server to access. This program has to access the queue without using jndi, which is why I am stuck =). All the tutorials I have found online use jndi.
The company who wrote this system have only said to use JBoss. J2EE / jndi is not in use at all, so using context cts = new InitialContext(); Then doing a jndi lookup only results in an Exception being thrown. This exception states that there is no jndi.properities located on the machine.
> reciever cannot be resolved. m cannot be resolved.
> session cannot be resolved. conn cannot be resolved.
> I have no idea why this is the case. They are all in
> the same method (main). However, for some reason if
> they are outside the while loop the inner loops can
> not see their variables.
Post the full code for the method.
Kaj
kajbja at 2007-7-12 14:46:58 >

The code I posted in the original post is the entire code. All this program is supposed to be is a simple class that listens to a message queue. This message queue uses JBoss, which has lead me to the assumption that I can not use jndi.
So, I guess my question is really how do I link up with the JBoss message queue. That code is all I have written for the listener class. I have been unsuccessful in finding a useful JBoss tutorial.