This Servlet is used by the Chat Client to receive a message.
ChatSubscriber.java
/*
* @author Rajat Gupta
* @version 1.0
*
* Name of the Application : ChatSubscriber.java
* Development Environment : Oracle9i JDeveloper
* Creation/Modification History :
*
*
* Rajat Gupta 28-May-2003 Created
*/
package oracle.otnsamples.oc4jjms;
// JMS Imports
import javax.jms.Message;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.jms.Topic;
import javax.jms.TopicConnection;
import javax.jms.TopicConnectionFactory;
import javax.jms.TopicSession;
import javax.jms.TopicSubscriber;
// Naming Imports
import javax.naming.Context;
/**
* This class is the object representation of a Chat client. Whenever a new
* user logs into the application, then this object is first created and
* assigned to the client.
*
*/
public class ChatSubscriber {
/**
* TopicConnectionFactory Object
*/
// private TopicConnectionFactory topicCF;
/**
* TopicConnection Object
*/
private TopicConnection topicConnection;
/**
* Topic Session Object
*/
private TopicSession topicSession;
/**
* Topic Object
*/
private Topic topic;
/**
* Receiver Object. The application receives the message from the topic through
* this object.
*/
private TopicSubscriber receiver;
/**
* Empty constructor
*/
public ChatSubscriber(){
}
/**
* This method actually registers the chat client with the topic. The
* method first creates a connection to the topic and then creates a
* Receiver object to the topic.
*
* @param jndiContext JNDI Context form the application
* @param clientID ID of the Client currently connected
*
* @exception Exception In Case the application throws some exception
*/
public void createSubscriber(Context jndiContext, String clientID)
throws Exception{
// Lookup the Topic Connection factory and create the
// TopicConnectionFactory object
TopicConnectionFactory topicCF = (TopicConnectionFactory)jndiContext.lookup
("jms/ChatTopicConnectionFactory");
// Create a connection to the topic
topicConnection = topicCF.createTopicConnection();
// Set the Client ID (subscriber) for this object
topicConnection.setClientID(clientID);
// Create a Session to this topic
topicSession = topicConnection.createTopicSession
(true, Session.AUTO_ACKNOWLEDGE);
// Lookup the topic
topic = (Topic)jndiContext.lookup
("jms/ChatTopic");
// Create a durable subscriber to the specified topic.
receiver = topicSession.createDurableSubscriber(topic, "OTN");
// Close the context
jndiContext.close();
// Start the connection to the topic
topicConnection.start();
return;
}
/**
* This method connects to the topic and checks if a new message has
* arrived for the client. If a message is present, then the method
* dequeues and returns it. If no new message is present for the client
* then the method returns a null object.
*
* @exception Exception In Case the application throws some exception
*
* @return Message for the connected client
*/
public String getMessage() throws Exception{
String message = null;
// Receive the message; if any
Message msg = receiver.receiveNoWait();
if (msg != null){
// Extract the Message
message = ((TextMessage)msg).getText();
}
// Return the message
return message;
}
}