Using Servlets for MQSeries
chanchal11 Mar 18, 2004 10:54 AMHey, I am passing some form parameters to a Servlet which is creating a MQseries message from these parameters and putting it in a queue.When Servlet is invoked it gives following error in MQseries code:
--------------------------------------------------------------
In doPost() Input information is; price_zone: 10$; point_of_sale: w; event: aa; entry_timestamp: 12; perf: aa; no_seats: 2; order_id: 1 Creating a QueueConnectionFactory Creating a Connection
HTTP ERROR: 500 javax/resource/ResourceException
RequestURI=/HelloWorldContext/Simple
-----------------------------------------------------------------------------
I putted all jar files in my application folder(Ear) and added into application.xml file.Its very simple example.Please take a look at this and let me know where I am wrong.Here is my Servlet code:
------------------------------------------------------------------------
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.jms.*;
import com.ibm.mq.jms.MQQueueConnectionFactory;
// Import required for program tracing
import com.ibm.mq.jms.services.ConfigEnvironment;
public class Simple extends javax.servlet.http.HttpServlet
{
public static String QMGR = "QM1";
public static final String QUEUE = "SYSTEM.DEFAULT.LOCAL.QUEUE" ;
Queue ioQueue= null;
QueueSession session = null;
QueueConnection connection = null;
QueueConnectionFactory factory = null;
//define a queue manager object
public Simple()
{
super();
System.out.println("Inside constructor");
}
public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException
{
String tempAddress = "Input information is";
res.setContentType("text/plain");
PrintWriter out = res.getWriter();
if ("application/x-www-form-urlencoded".equals(req.getContentType()))
{
out.println("In doPost()");
Enumeration enum = req.getParameterNames();
while (enum.hasMoreElements())
{
String name = (String) enum.nextElement();
String values = req.getParameter(name);
if(values != null)
{
tempAddress = tempAddress + "; " + name + ": " + values;
}
}
out.println(tempAddress);
try
{
out.println("Creating a QueueConnectionFactory");
factory = new MQQueueConnectionFactory();
((MQQueueConnectionFactory)factory).setQueueManager(QMGR);
// Create a QueueConnection from the QueueConnectionFactory
out.println("Creating a Connection");
connection = factory.createQueueConnection();
// IMPORTANT: Receive calls will be blocked if the connection is
// not explicitly started, so make sure that we do so!
out.println("Starting the Connection");
connection.start();
// We now create a QueueSession from the connection. Here we
// specify that it shouldn't be transacted, and that it should
// automatically acknowledge received messages
out.println("Creating a Session");
boolean transacted = false;
session = connection.createQueueSession( transacted,Session.AUTO_ACKNOWLEDGE);
// Use the session to create the queue object, supplying
// the required MQ-specific parameter
ioQueue = session.createQueue( QUEUE );
// We now use the session to create a QueueSender, passing in the
// destination (the Queue object) as a parameter
out.println("Creating a QueueSender");
QueueSender queueSender = session.createSender(ioQueue);
// Create a QueueReceiver in the same way
out.println( "Creating a QueueReceiver");
QueueReceiver queueReceiver = session.createReceiver(ioQueue);
// The session is used to create messages, so create an empty
// TextMessage and fill it with some data
out.println( "Creating a TextMessage" );
TextMessage outMessage = session.createTextMessage();
out.println("Adding Text");
outMessage.setText(tempAddress);
// Ask the QueueSender to send the message we have created
out.println( "Sending the message to " + ioQueue.getQueueName() );
queueSender.send(outMessage);
// Now use the QueueReceiver to retrieve the message, blocking
// for a maximum of 1000ms. The receive call returns when the
// message arrives, or after 1000ms, whichever is sooner
out.println( "Reading the message back again" );
Message inMessage = queueReceiver.receive(1000);
// Check to see if the receive call has actually returned a
// message. If it hasn't, report this and throw an exception...
if( inMessage == null )
{
out.println( "The attempt to read the message back again " +
"failed, apparently because it wasn't there");
throw new JMSException("Failed to get message back again");
}
out.println( "\n" + "Got message"+": "+inMessage);
// Check that the message received (a) is of the correct type,
// and (b) contains the same text as the one sent, reporting the
// result of these two checks
if( inMessage instanceof TextMessage )
{
// Extract the message content with getText()
String replyString = ((TextMessage) inMessage).getText();
// Test its equality with the message text sent
if( replyString.equals(tempAddress) )
{
out.println("Reply string equals original string");
}
else
{
// If they differ, print them both out
out.println("Error! Reply string differs from " + "original string");
out.println("Original string = '" + tempAddress + "'");
out.println("Reply string = '" + replyString + "'");
}
}
else
{
// Report that the incoming message was not of the expected
// type, and throw an exception
out.println("Reply message was not a TextMessage");
throw new JMSException("Retrieved the wrong type of message");
}
// Remember to call the close() method on all of the objects
// used, to ensure proper clean-up of resources
// Closing QueueReceiver
out.println("Closing QueueReceiver");
queueReceiver.close();
// Closing QueueSender
out.println("Closing QueueSender");
queueSender.close();
// Closing QueueSesssion.
out.println("Closing Session");
session.close();
session = null;
// Closing QueueConnection.
out.println("Closing Connection");
connection.close();
connection = null;
}
catch( JMSException je )
{
out.println("caught JMSException: " + je);
// check for a linked exception that provides more detail
Exception le = je.getLinkedException();
if (le != null) out.println("linked exception: "+le);
}
catch( Exception e )
{
// This catches any exception thrown in the main body of
// the program, displaying it on screen
out.println("Caught exception: " + e );
}
finally
{
// A finally block is a good place to ensure that we don't forget
// to close the most important JMS objects
try
{
if (session != null)
{
out.println("closing session");
session.close();
}
if (connection != null)
{
out.println("closing connection");
connection.close();
}
}
catch (JMSException je)
{
out.println("failed with "+je);
}
}
out.println("finished");
}
}
}
-------------------------------------------------------------------------------