Implementing JMS queue receiver class

Let's create the CourseQueueReceiver class in the packt.jee.eclipse.jms package with the following content:

public class CourseQueueReceiver { 
 
  private QueueConnection connection; 
  private QueueSession session; 
  private Queue queue; 
 
  private String receiverName; 
 
  public CourseQueueReceiver(String name) throws Exception{ 
 
    //save receiver name 
    this.receiverName = name; 
 
    //look up JMS connection factory 
    InitialContext initCtx = new InitialContext(); 
    QueueConnectionFactory connectionFactory = 
(QueueConnectionFactory)initCtx.lookup("jms/CourseManagemenCF"); //create JMS connection connection = connectionFactory.createQueueConnection(); connection.start(); //create JMS session session = connection.createQueueSession(false,
Session.AUTO_ACKNOWLEDGE); //look up queue queue = (Queue)initCtx.lookup("jms/courseManagementQueue"); topicPublisher = new CourseTopicPublisher(); QueueReceiver receiver = session.createReceiver(queue); //register message listener receiver.setMessageListener(new MessageListener() { @Override public void onMessage(Message message) { //we expect ObjectMessage here; of type CourseDTO //skipping validation try { CourseDTO course = (CourseDTO)
((ObjectMessage)message).getObject(); //process addCourse action. For example, save it in the
database System.out.println("Received addCourse message for Course name - " +
course.getName() + " in Receiver " + receiverName); } catch (Exception e) { e.printStackTrace(); //TODO: handle and log exception } } }); } public void stop() { if (connection != null) { try { connection.close(); } catch (JMSException e) { e.printStackTrace(); //TODO: log exception } } } }

The code to look up the connection factory and the queue is similar to that in CourseQueueSender. Note that the constructor takes a name argument. We don't really need to use the JMS API, but we will use it as an identifier for instances of the CourseQueueReceiver class. We register a message listener in the constructor, and in the onMessage method of the listener class we get the CourseDTO object from the message and print the message to the console. This message will appear in the GlassFish console in Eclipse when we execute the code. To keep the example simple, we have not implemented the code to save the Course information to the database, but you can do so using the JDBC or JDO APIs we learned about in Chapter 4, Creating JEE Database Applications.

We need to instantiate the CourseQueueReceiver class at application startup, so that it will start listening for the messages. One way to implement this is in a servlet that loads on startup.

Let's create the JMSReceiverInitServlet class in the packt.jee.eclipse.jms.servlet package. We will mark this servlet to load at startup using annotations and instantiate CourseQueueReceiver in the init method:

package packt.jee.eclipse.jms.servlet; 
 
//skipped imports 
 
@WebServlet(urlPatterns="/JMSReceiverInitServlet", loadOnStartup=1) 
public class JMSReceiverInitServlet extends HttpServlet { 
  private static final long serialVersionUID = 1L; 
 
  private CourseQueueReceiver courseQueueReceiver = null; 
 
    public JMSReceiverInitServlet() { 
        super(); 
    } 
 
    @Override 
    public void init(ServletConfig config) throws ServletException 
{ super.init(config); try { courseQueueReceiver = new CourseQueueReceiver("Receiver1"); } catch (Exception e) { log("Error creating CourseQueueReceiver", e); } } @Override public void destroy() { if (courseQueueReceiver != null) courseQueueReceiver.stop(); super.destroy(); } }

Publish the project again in the server and execute addCourse.jsp (see the Executing addCourse.jsp section of this chapter). Switch to the Console view in Eclipse. You should see the message that we printed in the onMessage method in CourseQueueReceiver:

Figure 10.6: Example of a console message from the JMS receiver class
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.225.234.28