Implementing JMS topic subscriber

We will now implement a topic subscriber class to receive messages published to the topic we created earlier. Create a CourseTopicSubscriber class in the packt.jee.eclipse.jms package with the following content:

package packt.jee.eclipse.jms; 
//skipping imports 
public class CourseTopicSubscriber { 
 
  private TopicConnection connection; 
  private TopicSession session; 
  private Topic topic; 
 
  private String subscriberName; 
 
  public CourseTopicSubscriber(String name) throws Exception{ 
 
    this.subscriberName = name; 
 
    InitialContext initCtx = new InitialContext(); 
    TopicConnectionFactory connectionFactory = 
(TopicConnectionFactory)initCtx.lookup("jms/CourseManagemenCF"); connection = connectionFactory.createTopicConnection(); connection.start(); session = connection.createTopicSession(false,
Session.AUTO_ACKNOWLEDGE); topic = (Topic)initCtx.lookup("jms/courseManagementTopic"); TopicSubscriber subscriber = session.createSubscriber(topic); subscriber.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
database System.out.println("Received addCourse notification for
Course name - " + course.getName() + " in Subscriber " +
subscriberName); } catch (JMSException 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 } } } }

Again, the JMS APIs to subscribe to a topic are similar to those in CourseQueueReceiver, but with different class names and method names. We also identify subscribers with names so that we know which instance of the class receives the message.

In the preceding example, we created the topic subscriber by calling TopicSession.createSubscriber. In this case, the subscriber will receive messages from the topic as long as the subscriber is active. If the subscriber becomes inactive and then active again, it loses messages published by the topic during that period. If you want to make sure that the subscriber receives all the messages, you need to create a durable subscription using TopicSession.createDurableSubscriber. Along with the topic name, this method takes the subscriber name as the second argument. Refer to https://javaee.github.io/javaee-spec/javadocs/javax/jms/TopicSession.html#createDurableSubscriber-javax.jms.Topic-java.lang.String- for more information.

We will create two instances of the CourseTopicSubscriber class (so there will be two topic subscribers) in JMSReceiverInitServlet. These two instances will start listening for messages on application startup (the servlet is loaded on startup):

@WebServlet(urlPatterns="/JMSReceiverInitServlet", loadOnStartup=1) 
public class JMSReceiverInitServlet extends HttpServlet { 
  private CourseQueueReceiver courseQueueReceiver = null; 
  private CourseTopicSubscriber courseTopicSubscriber = null; 
  private CourseQueueReceiver courseQueueReceiver1 = null; 
  private CourseTopicSubscriber courseTopicSubscriber1 = null; 
 
    @Override 
    public void init(ServletConfig config) throws ServletException 
{ super.init(config); try { courseQueueReceiver = new CourseQueueReceiver("Receiver1"); courseQueueReceiver1 = new CourseQueueReceiver("Receiver2"); courseTopicSubscriber = new
CourseTopicSubscriber("Subscriber1"); courseTopicSubscriber1 = new
CourseTopicSubscriber("Subscriber2"); } catch (Exception e) { log("Error creating CourseQueueReceiver", e); } } //remaining code is unchanged. Skipping it }

We now have two queue listeners and two topic listeners ready when the application starts. Republish the project, execute addCourse.jsp, and add a course. Check the messages in the Console view of Eclipse. You will see that the message published in the topic is received by all subscribers, but the same message published in the queue is received by only one receiver:

Figure 10.8: Console output showing multiple JMS receivers listening to JMS queue and topic
..................Content has been hidden....................

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