Queues are meant for point-to-point communication, but this does not mean that there can't be more than one listener for a queue. However, only one listener gets the message. Furthermore, it is not guaranteed that the same listener will get the message every time. If you want to test this, add one more instance of CourseQueueReceiver in JMSReceiverInitServlet. Let's add the second instance with a different name, say Receiver2:
@WebServlet(urlPatterns="/JMSReceiverInitServlet", loadOnStartup=1) public class JMSReceiverInitServlet extends HttpServlet { private CourseQueueReceiver courseQueueReceiver = null; private CourseQueueReceiver courseQueueReceiver1 = null; @Override public void init(ServletConfig config) throws ServletException { super.init(config); try { //first instance of CourseQueueReceiver courseQueueReceiver = new CourseQueueReceiver("Receiver1"); //create another instance of CourseQueueReceiver with a
different name courseQueueReceiver1 = new CourseQueueReceiver("Receiver2"); } catch (Exception e) { log("Error creating CourseQueueReceiver", e); } } @Override public void destroy() { if (courseQueueReceiver != null) courseQueueReceiver.stop(); if (courseQueueReceiver1 != null) courseQueueReceiver1.stop(); super.destroy(); } //rest of the code remains the same }
Republish the project, execute addCourse.jsp, and add a few courses. Check the Console messages. You may see that some of the messages were received by Receiver1 and the others by Receiver2:
Figure 10.7: Console output showing multiple JMS receivers listening to a JMS queue