Following are the main Source files of this application

Class Description
SendMessage.java This Servlet is used by the Chat Client to send the message. 
GetMessage.java This Servlet is used by the Chat Client to receive a message. 
ChatSubscriber.java This Java class has the important methods for adding a subscriber and dequeing the message. The methods in this class are used by the GetMessage.java servlet.

/*
 * @author Rajat Gupta
 * @version 1.0
 *
 * Name of the Application        : ListenerMDBBean.java

 * Development Environment       : Oracle9i JDeveloper
 * Creation/Modification History  :
 *
 *
 *   Rajat Gupta        28-May-2003        Created
 */

package oracle.otnsamples.oc4jjms.impl;

// EJB Imports

import javax.ejb.MessageDrivenBean;
import javax.ejb.MessageDrivenContext;

// JMS Imports
import javax.jms.DeliveryMode;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.Session;
import javax.jms.Topic;
import javax.jms.TopicConnection;
import javax.jms.TopicConnectionFactory;

import javax.jms.TopicPublisher;
import javax.jms.TopicSession;

// Naming Imports
import javax.naming.Context;
import javax.naming.InitialContext;


/**
 * This Message Driven Bean listens to a queue. If any message is enqueued to
 * the queue, then this bean is invoked. On invocation, the bean first dequeues
 * the message and then enqueues this message to other topic.

 *
 */
public class ListenerMDBBean implements MessageDrivenBean, MessageListener  {

  /**
   * Context for the MDB
   */
  private MessageDrivenContext context;

  /**
   * TopicConnectionFactory Object
   */
//  TopicConnectionFactory topicCF;


  /**
   * TopicConnection Object
   */
  TopicConnection topicConnection;

  /**
   * TopicSession Object
   */
  TopicSession topicSession;

  /**
   * Topic Object
   */

  Topic topic;

  /**
   * Object to publish the message to the topic.
   */
  TopicPublisher sender;

  /**
   * This method will be invoked when this MDB is created. The method
   * initializes some objects needed by the bean.
   */
  public void ejbCreate() {
    try{
      // Get the Initial Context
      Context jndiContext = new InitialContext();


      // Lookup the Topic Connection factory and create the
      // QueueConnectionFactory.
      TopicConnectionFactory topicCF = (TopicConnectionFactory)jndiContext.lookup
            ("jms/ChatTopicConnectionFactory");

      // Create a Connection to the Topic
      topicConnection = topicCF.createTopicConnection();

      // Create a Session to the topic.
      topicSession = topicConnection.createTopicSession
                                           (true, Session.AUTO_ACKNOWLEDGE);

      // Lookup the Topic and create its object.
      topic = (Topic)jndiContext.lookup
                              ("jms/ChatTopic");


      // Creates an object to send messages to the specified topic
      sender = topicSession.createPublisher(topic);

      // Un-comment the following line if you want the message in the topic
      // to expire after 10 minutes. As of now, the message will be present
      // in the topic until all the subscribed users have received the
      // message.
      //sender.setTimeToLive(600000);

      // Set the message to be stored persistently.
      sender.setDeliveryMode(DeliveryMode.PERSISTENT);

      // This application does not need a unique ID for each message. So, we
      // will disable its generation. This will save time and improve
      // performance as the application does not have to go through elaborate
      // algorithms to generate unique ID for each message.

      sender.setDisableMessageID(true);

      // Close the JNDI Context
      jndiContext.close();

      // Start the Connection
      topicConnection.start();
    }catch(Exception e){
      // Print the exception on the console
      e.printStackTrace();
    }
  }

  /**
   * This method will be invoked when a message is enqueued to the queue on
   * which the MDB is listening to.
   *
   * @param message Message enqueued in the queue

   */
  public void onMessage(Message message) {
    try{
      // Enqueue the message in the queue. This method uses the default
      // delivery mode, priority, and time to live. But, you can also
      // specify these parameters explicitly through other overloaded
      // methods within the QueueSender Class.
      sender.publish(message);

      // Commit the transaction
      topicSession.commit();

    }catch (Exception e){
      // Print the exception on the console
      e.printStackTrace();
    }
  }

  /**

   * This method will be called when this MBD is removed from memory. The
   * method closes the objects that are no longer required.
   */
  public void ejbRemove() {
    try{
      // Close the connection
      topicConnection.stop();

      // Close Resources
      sender.close();
      topicSession.close();
      topicConnection.close();

    }catch (Exception e){
      // Print the exception on the console
      e.printStackTrace();
    }
  }

  /**

   * This method sets the MDB Context.
   *
   * @param ctx MBD Context
   */
  public void setMessageDrivenContext(MessageDrivenContext ctx) {
    this.context = ctx;
  }
}
E-mail this page
Printer View Printer View
Oracle Is The Information Company About Oracle | Oracle RSS Feeds | Careers | Contact Us | Site Maps | Legal Notices | Terms of Use | Privacy