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.
GetMessage.java
/*
* @author Rajat Gupta
* @version 1.0
*
* Name of the Application : GetMessage.java
* Development Environment : Oracle9i JDeveloper
* Creation/Modification History :
*
*
* Rajat Gupta 28-May-2003 Created
*/
package oracle.otnsamples.oc4jjms;
// IO Imports
import java.io.IOException;
import java.io.PrintWriter;
// Util Imports
import java.util.HashMap;
// Naming Imports
import javax.naming.InitialContext;
// Servlet Imports
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
// HTTP Servlet Imports
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* This Servlet reads the message from the queue. If any message is available
* for the client, then it sends the message or else returns null.
*
*/
public class GetMessage extends HttpServlet {
/**
* Content type of the response
*/
private static final String CONTENT_TYPE = "text/xml; charset=ISO-8859-1";
/**
* This HashMap stores the objects of the users currently logged into the
* application. A registered user gets the message through this object.
*/
private HashMap currentSubscribers = new HashMap(5);
/**
* Init method of the Servlet
*
* @param config
*
* @exception ServletException
*/
public void init(ServletConfig config) throws ServletException {
try{
super.init(config);
}catch(Exception e){
// Print the exception on the console
e.printStackTrace();
}
}
/**
* This method is invoked when a client sends a message to this servlet. This
* method performs various operations of which the important ones are
* described as follows :
* 1) The user can login to the Application.
* 2) The user receives the message through this method.
*
* While registering a user, this method creates a subscriber object on
* behalf of the user. The subscriber object is then stored in the HashMap
* from where it is retrieved for this users subsequent requests.
*
* @param request ServletRequest Object
* @param response ServletResponse Object
*
* @exception ServletException
* @exception IOException
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// This variable stores the message that is to be returned to the
// client.
String message;
// Set the Content type
response.setContentType(CONTENT_TYPE);
try{
if (request.getParameter("doRegister") != null){
// This loop will be accessed for the first request of the user. This
// loop registers the user into the application.
//This variable stores the ID of the currently connected client.
String clientID = request.getParameter("id");
if (currentSubscribers.containsKey(clientID)){
// This loop checks if the user is already registered
message = "AlreadyCreated";
}else{
// Its a new user. This loop will create the subscriber object.
// Create the subscriber object for this client
ChatSubscriber subscriber = new ChatSubscriber();
subscriber.createSubscriber(new InitialContext(), clientID);
// Store the client object in a HashMap for future reference.
currentSubscribers.put(clientID, subscriber);
message = "Created";
}
}else if (request.getParameter("checkIfCreated") != null){
// This loop will be accessed if the client selects that he/she is
// already registered with the application. This loop will check
// if he/she is really registered.
// Get the Client ID.
String clientID = request.getParameter("id");
// Get the Subscriber object
ChatSubscriber subscriber =
(ChatSubscriber)currentSubscribers.get(clientID);
if (subscriber == null){
// The user is not already registered
message = "NotCreated";
}else{
// The user is already registered
message = "Created";
}
}else if (request.getParameter("id") != null){
// This loop connects to the topic and checks if any message is
// present for the user.
// Get the client ID
String clientID = request.getParameter("id");
// get the Subscriber object
ChatSubscriber subscriber =
(ChatSubscriber)currentSubscribers.get(clientID);
// Get the message
message = subscriber.getMessage();
}else{
// This loop will be accessed if some problem occurs and the server
// could not access the Client ID.
message = "Unexpected Problem. Please Login again.";
}
// Initialize the PrintWriter object
PrintWriter out = response.getWriter();
// Send the message to the User.
out.print(message);
}catch(Exception e){
// Print the exception on the console
e.printStackTrace();
}
}
/** This method redirects the client request to the doGet() method.
*
* @param request ServletRequest Object
* @param response ServletResponse Object
*
* @exception ServletException
* @exception IOException
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}