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.
SendMessage.java
/*
* @author Rajat Gupta
* @version 1.0
*
* Name of the Application : SendMessage.java
* Development Environment : Oracle9i JDeveloper
* Creation/Modification History :
*
*
* Rajat Gupta 28-May-2003 Created
*/
package oracle.otnsamples.oc4jjms;
// IO Imports
import java.io.IOException;
// JMS Imports
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueSender;
import javax.jms.QueueSession;
import javax.jms.Session;
// Naming Imports
import javax.naming.Context;
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 creates a connection to the queue and enqueues the message
* sent by the Chat client to the queue. It uses JMS APIs to do this.
*/
public class SendMessage extends HttpServlet {
/**
* QueueConnectionFactory Object
*/
//QueueConnectionFactory queueCF;
/**
* QueueConnection Object
*/
QueueConnection queueConnection;
/**
* QueueSession Object
*/
QueueSession queueSession;
/**
* Queue object
*/
Queue queue;
/**
* Object of the sender. The application enqueues messages to the queue
* through this object.
*/
QueueSender sender;
/**
* Init method of the Servlet. This method initializes various objects.
*
* @exception ServletException
*
* @param config ServletConfig Object
*/
public void init(ServletConfig config) throws ServletException{
super.init(config);
try{
// Get the Initial Context
Context jndiContext = new InitialContext();
// Lookup the QueueConnection factory and create the
// QueueConnectionFactory object
QueueConnectionFactory queueCF = (QueueConnectionFactory)jndiContext.lookup
("jms/MainQueueConnectionFactory");
// Create a Connection to the QueueTable. The connection is created in
// stopped mode. No messages will be delivered until the
// Connection.start method is explicitly called.
queueConnection = queueCF.createQueueConnection();
// Create a Session to the queue. It takes in two parameters which are
// 1) Transacted - Indicates whether the session is transacted. This
// is a boolean value.
// 2) AcknowledgeMode - Indicates whether the consumer or the client
// will acknowledge any messages it receives; ignored if the
// session is transacted. Valid values are Session.AUTO_ACKNOWLEDGE,
// Session.CLIENT_ACKNOWLEDGE and Session.DUPS_OK_ACKNOWLEDGE.
queueSession = queueConnection.createQueueSession
(true, Session.AUTO_ACKNOWLEDGE);
// Lookup the Queue and create its object.
queue = (Queue)jndiContext.lookup
("jms/MainQueue");
// Creates an object to send messages to the specified queue
sender = queueSession.createSender(queue);
// Close the context
jndiContext.close();
// Start the Connection
queueConnection.start();
}catch(Exception e){
// Print the exception on the console
e.printStackTrace();
}
}
/**
* This method is invoked when a client sends a message to this servlet.
*
* @exception ServletException
* @exception IOException
*
* @param request ServletRequest Object
* @param response ServletResponse Object
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException{
try{
// Get the message to be enqueued
String message = request.getParameter("msg");
// Enqueue the message in the queue. This method uses the default
// delivery mode, priority, and time to live.
sender.send(queueSession.createTextMessage(message));
// Commit the transaction
queueSession.commit();
}catch(Exception e){
// Print the exception on the console
e.printStackTrace();
}
}
/**
* This method redirects the client request to the doGet() method.
*
* @exception ServletException
* @exception IOException
*
* @param request
* @param response
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException{
doGet(request, response);
}
/**
* This method will be called when the Servlet is destroyed. It closes
* various objects that are no longer required by the application.
*/
public void destroy(){
try{
// Close the connection
queueConnection.stop();
// Close the various resources
sender.close();
queueSession.close();
queueConnection.close();
}catch(Exception e){
// Print the exception on the console
e.printStackTrace();
}
}
}