I am new to Java Messaging Service and not really completely sure how it all works. I am using JMS with a Java EE project in NetBeans. The server type I am using is glassfish 3. Here is my situation:
I have a program that creates two threads, A and B. These two threads are sending messages back and fourth between eachother through two Queue server resources I created. Below is a series of messages that are suppose to be passed back and fourth between A and B.
What is suppose to happen:
1. A is started
2. B is started
3. A sends B a message, M1 ( producer.send(m1) )
4. B receives M1 and does some stuff ( consumer.receive() )
5. B sends A a message, M2
6. A receives M2 and does some stuff
7. A sends B a message, M3
8. B receives M3 and does some stuff
What actually happens:
1. A is started
2. B is started
3. B receives M3 and does some stuff ( consumer.receive() )
This scenario started popping up ever since I did a session.commit() somewhere inbetween all the messages being sent. Its almost like I committed the state of the queue to some file or the server and now everytime I run my program, it initializes the queue from that saved state.
The reason I had added the commit in the first place was because one of my messages, M2, was not getting received by thread A. Thread B was reaching the part of its code where it sent M2 to thread A through producer.send(M2) (where producer is a MessageProducer object). Thread A would just hang on consumer.receive() (where consumer is a MessageConsumer object). I double checked to see if I was sending M2 to the correct Queue resource which it was.
I finally figured it out. Turns out I need the session.commit() everytime Thread B sends a message. Also, my code had an error in it and caused the program to end prematurely, meaning there were still messages that had not been received and those messages carry over to the next run time. What I ended up doing to fix that is just looping around consumer.receive(100) until it returned a null value before starting my threads.