ASG
IBM
Zystems
Cressida
Icon
Netflexity
 
  MQSeries.net
Search  Search       Tech Exchange      Education      Certifications      Library      Info Center      SupportPacs      LinkedIn  Search  Search                                                                   FAQ  FAQ   Usergroups  Usergroups
 
Register  ::  Log in Log in to check your private messages
 
RSS Feed - WebSphere MQ Support RSS Feed - Message Broker Support

MQSeries.net Forum Index » IBM MQ Java / JMS » Using Servlet for MQseries

Post new topic  Reply to topic
 Using Servlet for MQseries « View previous topic :: View next topic » 
Author Message
chanchal
PostPosted: Thu Mar 18, 2004 8:38 am    Post subject: Using Servlet for MQseries Reply with quote

Apprentice

Joined: 30 Jan 2004
Posts: 38

Hey, I am passing some form parameters to a Servlet which is creating a MQseries message from these parameters and putting it in a queue.I am using JBoss application server.When Servlet is invoked it displays the parameters and their value passed fro form but gives error in MQseries code:
--------------------------------------------------------------
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.Do I need to do anything in addition to that??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");
}
}

}
-------------------------------------------------------------------------------
Back to top
View user's profile Send private message
bower5932
PostPosted: Thu Mar 18, 2004 10:55 am    Post subject: Reply with quote

Jedi Knight

Joined: 27 Aug 2001
Posts: 3023
Location: Dallas, TX, USA

My guess would be that you don't have everything configured in your classpath.
Back to top
View user's profile Send private message Send e-mail Visit poster's website AIM Address Yahoo Messenger
Display posts from previous:   
Post new topic  Reply to topic Page 1 of 1

MQSeries.net Forum Index » IBM MQ Java / JMS » Using Servlet for MQseries
Jump to:  



You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Protected by Anti-Spam ACP
 
 


Theme by Dustin Baccetti
Powered by phpBB © 2001, 2002 phpBB Group

Copyright © MQSeries.net. All rights reserved.