|   | 
	 
  
    | 
RSS Feed - WebSphere MQ Support
 | 
RSS Feed - Message Broker Support
 |   
 
  
	     | 
	 | 
   
 
  
	|  How to send and retrieve WebSphere MQ message from servlet | 
	« View previous topic :: View next topic »  | 
   
  
  	
	  
		
		
		  | Author | 
		  Message
		 |  
		
		  | krishnanj | 
		  
		    
			  
				 Posted: Sat May 05, 2007 9:05 am    Post subject: How to send and retrieve WebSphere MQ message from servlet | 
				     | 
			   
			 
		   | 
		 
		
		   Newbie
 
 Joined: 05 May 2007 Posts: 1
  
  | 
		  
		    
			  
				Hello
 
 
 
 I am new to java and WebSphere MQ. i have to develop
 
 an application.
 
 i have 1 html application running in 1 system.when i
 
 entered the details in the form,it should go the
 
 servlet,then the servlet should make a connection to
 
 the queue manager in MQ and should store the message
 
 in the queue.
 
 
 
 then the another servlet should read the message from
 
 the queue and should display the message in output
 
 html.
 
 
 
i have downloaded the samples from the net. but that servlet shows some errors
 
"Error 500: java.lang.Error: Unresolved compilation problems: The import com.ibm.mqbind cannot be resolved MQQueueManager cannot be resolved (or is not a valid type) for the field BillingAddressServlet.qMgr qMgr cannot be resolved MQException cannot be resolved or is not a type qMgr cannot be resolved MQQueueManager cannot be resolved or is not a type MQException cannot be resolved or is not a type MQC cannot be resolved MQC cannot be resolved MQQueue cannot be resolved or is not a type qMgr cannot be resolved MQMessage cannot be resolved or is not a type MQMessage cannot be resolved or is not a type MQC cannot be resolved MQGetMessageOptions cannot be resolved or is not a type MQGetMessageOptions cannot be resolved or is not a type MQException cannot be resolved or is not a type com.sun.server cannot be resolved com.sun.server cannot be resolved MQC cannot be resolved MQC cannot be resolved MQQueue cannot be resolved or is not a type qMgr cannot be resolved MQMessage cannot be resolved or is not a type MQMessage cannot be resolved or is not a type MQPutMessageOptions cannot be resolved or is not a type MQPutMessageOptions cannot be resolved or is not a type MQException cannot be resolved or is not a type "
 
 
this was the error. i also attached the servlet along with this. can anyone tellme what are the things i have to change in that servlet.
 
 
 
 Can anyone helpme on this.
 
 its very urgent.
 
 
 
regards,
 
krishnan.J
 
   
	| Code: | 
   
  
	
 
package com.ibm.toolbelt;
 
 
import java.io.*;
 
import java.util.*;
 
import javax.servlet.*;
 
import javax.servlet.http.*;
 
import com.ibm.mqbind.*;         //include the MQ bindings package
 
 
 
/**
 
 * This type was created in VisualAge.
 
 */
 
public class BillingAddressServlet extends javax.servlet.http.HttpServlet {
 
   
 
   private String qManager = "NC.QManager";   //name of queue manager to connect to
 
 
   private MQQueueManager qMgr;            //define a queue manager object
 
/**
 
 * BillingAddressServlet constructor comment.
 
 */
 
public BillingAddressServlet() {
 
   super();
 
}
 
/**
 
 * This method was created in VisualAge.
 
 */
 
public void destroy() {
 
   System.out.println("In destroy()");
 
   try
 
   {
 
      //Disconnect from the queue manager
 
      qMgr.disconnect();
 
   }
 
   catch (MQException ex)
 
   {
 
      System.out.println("An MQ error occourred: Completion code " + ex.completionCode +
 
                                         " Reason code" + ex.reasonCode);
 
   }
 
   
 
   System.out.println("Out of destroy()");            
 
}
 
/**
 
 * This method was created in VisualAge.
 
 * @param req javax.servlet.http.HttpServletRequest
 
 * @param res javax.servlet.http.HttpServletResponse
 
 * @exception javax.servlet.ServletException The exception description.
 
 * @exception java.io.IOException The exception description.
 
 */
 
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())) {
 
      System.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;
 
         }
 
         
 
      }
 
      System.out.println(tempAddress);
 
            
 
      try {
 
            //calling to put the order
 
            System.out.println("Calling putOrder()");
 
               putOrder(tempAddress);
 
 
               //calling orderUpdateStatus to get the conformation back
 
            System.out.println("Calling orderUpdateStatus()");
 
            String msgText = orderUpdateStatus();
 
            
 
            //Instantiate the bean and calling the JSP
 
            System.out.println("Instantiate the bean and calling the JSP");
 
            performTask(req, res, msgText);
 
            
 
            
 
                  
 
         }
 
         catch (Exception ex)
 
         {
 
            System.out.println("Exception caught in doPost()");
 
            ex.printStackTrace();
 
         }   
 
      
 
   }
 
      
 
   System.out.println("Out of doPost()");            
 
   
 
}
 
/**
 
 * This method was created in VisualAge.
 
 * @param config javax.servlet.ServletConfig
 
 * @exception javax.servlet.ServletException The exception description.
 
 */
 
public void init(ServletConfig config) throws ServletException {
 
      super.init(config);
 
      try {
 
            //Create a connection to the queue manager
 
            System.out.println("Create a connection to the queue manager");
 
            qMgr = new MQQueueManager("NC.QManager");
 
            System.out.println("Connection created in init()");
 
            
 
         }
 
         catch (MQException ex)
 
         {
 
            System.out.println("An MQ error occourred in init(): Completion code " + ex.completionCode +
 
                                               " Reason code" + ex.reasonCode);
 
            try {
 
               if (qMgr != null)
 
               //Disconnect from the queue manager
 
               qMgr.disconnect();
 
            }
 
            catch (MQException e)
 
            {
 
               System.out.println("An MQ error occourred in init() while disconnecting: Completion code " + e.completionCode +
 
                                               " Reason code" + e.reasonCode);
 
            }
 
         }
 
         
 
}
 
/**
 
 * This method was created in VisualAge.
 
 */
 
public String orderUpdateStatus() {
 
   String msgText = null;
 
   try {
 
      System.out.println("In orderUpdateStatus()");
 
      
 
      int openOptions = MQC.MQOO_INPUT_AS_Q_DEF | MQC.MQOO_OUTPUT;
 
   
 
      //Specify the queue that we wish to open, and the open options.
 
      System.out.println("Specifying the queue that we wish to open, and the open options.");
 
         
 
            
 
      MQQueue ncOrderUpdateQ = qMgr.accessQueue("NC.OrderCreateQ",
 
                                           openOptions,
 
                                           qManager,      
 
                                           null,             // no dynamic q name
 
                                           null);            // no alternate user id
 
 
      //create a new get the message
 
      MQMessage retrievedMessage = new MQMessage();
 
      
 
      retrievedMessage.messageId = MQC.MQMI_NONE;
 
            
 
      //set the get message options
 
      MQGetMessageOptions gmo = new MQGetMessageOptions();
 
      
 
      //get the message off the queue
 
      ncOrderUpdateQ.get(retrievedMessage, gmo);
 
                  
 
      //Display the message
 
      //msgText = retrievedMessage.readString(retrievedMessage.getMessageLength());  //for NC.UpdateQ
 
      msgText = retrievedMessage.readUTF();      // for NC.OrderCreateQ
 
      System.out.println("The address is: " + msgText);
 
 
      //Close the queue
 
      ncOrderUpdateQ.close();
 
 
      
 
                  
 
   }
 
   catch (MQException ex)
 
   {
 
      System.out.println("An MQ error occourred: Completion code " + ex.completionCode +
 
                                         " Reason code" + ex.reasonCode);
 
      System.out.println("disconnecting in orderUpdateStatus()");
 
      try {
 
         if (qMgr != null)
 
         //Disconnect from the queue manager
 
         qMgr.disconnect();
 
      }
 
      catch (MQException e)
 
      {
 
         System.out.println("An MQ error occourred while disconnecting in orderUpdateStatus(): Completion code " + e.completionCode +
 
                                            " Reason code" + e.reasonCode);
 
      }
 
            
 
            
 
   }
 
   catch (java.io.IOException ex)
 
   {
 
      System.out.println("An error occourred while writing to the message buffer: " + ex);
 
      System.out.println("disconnecting in orderUpdateStatus()");
 
      try {
 
         if (qMgr != null)
 
         //Disconnect from the queue manager
 
         qMgr.disconnect();
 
      }
 
      catch (MQException ep)
 
      {
 
         System.out.println("An MQ error occourred while disconnecting in orderUpdateStatus(): Completion code " + ep.completionCode +
 
                                               " Reason code" + ep.reasonCode);
 
      }
 
   }
 
   return(msgText);
 
}
 
/**
 
 * This method was created in VisualAge.
 
 * @param request javax.servlet.http.HttpServletRequest
 
 * @param response javax.servlet.http.HttpServletResponse
 
 */
 
public void performTask(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, java.lang.String returnMessage) {
 
   System.out.println("in performTask()");   
 
   try 
 
      {
 
         // instantiate the bean
 
         BillingAddressBean myBillingAddressBean = new BillingAddressBean();
 
         // set the return message in the bean
 
         myBillingAddressBean.setMQReturnMessage(returnMessage);
 
         // store the bean in the request so it can be accessed by pages which are accessed with callPage()
 
         ((com.sun.server.http.HttpServiceRequest)request).setAttribute("BillingAddressBean", myBillingAddressBean);
 
         // Call the output page.
 
         ((com.sun.server.http.HttpServiceResponse)response).callPage("/AddressOutputPage.jsp", request);
 
         
 
      }
 
      catch (Exception ex)
 
      {
 
         System.out.println("Exception caught in performTask()");
 
         ex.printStackTrace();
 
      }
 
}
 
/**
 
 * This method was created in VisualAge.
 
 */
 
public void putOrder(String tempAddress) {
 
   try {
 
      System.out.println("In putOrder()");
 
      int openOptions = MQC.MQOO_INPUT_AS_Q_DEF | MQC.MQOO_OUTPUT;
 
   
 
      //Specify the queue that we wish to open, and the open options.
 
      System.out.println("Specifying the queue that we wish to open, and the open options.");
 
         
 
      MQQueue ncOrderDataQ = qMgr.accessQueue("NC.OrderCreateQ",
 
                                           openOptions,
 
                                           qManager,      
 
                                           null,             // no dynamic q name
 
                                           null);            // no alternate user id
 
 
               
 
      //Define a MQ message
 
      System.out.println("Define a MQ message");
 
      MQMessage customerAddress = new MQMessage();
 
                                    
 
      System.out.println("tempAddress =" + tempAddress);
 
               
 
      customerAddress.writeUTF(tempAddress);
 
      
 
      //specify the message options
 
      MQPutMessageOptions pmo = new MQPutMessageOptions();
 
 
      //put the message on the queue
 
      ncOrderDataQ.put(customerAddress, pmo);
 
 
      System.out.println("Put message done");
 
            
 
      //Close the queue
 
      ncOrderDataQ.close();                     
 
            
 
      }
 
      catch (MQException ex)
 
      {
 
            System.out.println("An MQ error occourred: Completion code " + ex.completionCode +
 
                                               " Reason code" + ex.reasonCode);
 
            System.out.println("disconnecting in doPost()");
 
            try {
 
               if (qMgr != null)
 
               //Disconnect from the queue manager
 
               qMgr.disconnect();
 
            }
 
            catch (MQException e)
 
            {
 
               System.out.println("An MQ error occourred while disconnecting in doPost(): Completion code " + e.completionCode +
 
                                               " Reason code" + e.reasonCode);
 
            }
 
            
 
            
 
      }
 
      catch (java.io.IOException ex)
 
      {
 
            System.out.println("An error occourred while writing to the message buffer: " + ex);
 
            System.out.println("disconnecting in doPost()");
 
            try {
 
               if (qMgr != null)
 
               //Disconnect from the queue manager
 
               qMgr.disconnect();
 
            }
 
            catch (MQException ep)
 
            {
 
               System.out.println("An MQ error occourred while disconnecting in doPost(): Completion code " + ep.completionCode +
 
                                               " Reason code" + ep.reasonCode);
 
            }
 
      }
 
               
 
      
 
      System.out.println("Out of putOrder()");
 
            
 
}
 
} | 
   
 
 | 
			   
			 
		   | 
		 
		
		  | Back to top | 
		  
		  	
		   | 
		 
		
		    | 
		 
		
		  | fjb_saper | 
		  
		    
			  
				 Posted: Sat May 05, 2007 4:20 pm    Post subject:  | 
				     | 
			   
			 
		   | 
		 
		
		    Grand High Poobah
 
 Joined: 18 Nov 2003 Posts: 20768 Location: LI,NY 
  | 
		  
		    
			  
				Apparently this is a classpath error.
 
 
Anyway inside a J2EE server you should be using JMS and not java base...
 
 
Enjoy    _________________ MQ & Broker admin | 
			   
			 
		   | 
		 
		
		  | Back to top | 
		  
		  	
		   | 
		 
		
		    | 
		 
		
		  | marcin.kasinski | 
		  
		    
			  
				 Posted: Sat May 05, 2007 10:47 pm    Post subject:  | 
				     | 
			   
			 
		   | 
		 
		
		   Sentinel
 
 Joined: 21 Dec 2004 Posts: 850 Location: Poland / Warsaw 
  | 
		  
		    
			  
				
   
	| fjb_saper wrote: | 
   
  
	
 
Anyway inside a J2EE server you should be using JMS and not java base...
 
 | 
   
 
 
 
Why ?
 
 
In my opinion it is not obvious.
 
 
I think you can use both JMS and base classes.
 
 
You can not say "always use JMS"
 
 
Sometimes it is better to use JMS.
 
Sometimes it is better to use base classes. _________________ Marcin | 
			   
			 
		   | 
		 
		
		  | Back to top | 
		  
		  	
		   | 
		 
		
		    | 
		 
		
		  | fjb_saper | 
		  
		    
			  
				 Posted: Sun May 06, 2007 12:09 pm    Post subject:  | 
				     | 
			   
			 
		   | 
		 
		
		    Grand High Poobah
 
 Joined: 18 Nov 2003 Posts: 20768 Location: LI,NY 
  | 
		  
		    
			  
				
   
	| marcin.kasinski wrote: | 
   
  
	
   
	| fjb_saper wrote: | 
   
  
	
 
Anyway inside a J2EE server you should be using JMS and not java base...
 
 | 
   
 
 
 
Why ?
 
 
In my opinion it is not obvious.
 
 
I think you can use both JMS and base classes.
 
 
You can not say "always use JMS"
 
 
Sometimes it is better to use JMS.
 
Sometimes it is better to use base classes. | 
   
 
 
But with JMS you get out of the box the connection pools and scalability + transactionality.... and so far I had not to deal with an application where the difference in speed would have mattered (we are into milliseconds here...)
 
 
Enjoy    _________________ MQ & Broker admin | 
			   
			 
		   | 
		 
		
		  | Back to top | 
		  
		  	
		   | 
		 
		
		    | 
		 
		
		  | BenR | 
		  
		    
			  
				 Posted: Sun May 06, 2007 10:51 pm    Post subject:  | 
				     | 
			   
			 
		   | 
		 
		
		   Acolyte
 
 Joined: 31 Jan 2006 Posts: 60 Location: Hursley, UK 
  | 
		  
		    
			  
				| The base Java API does not comply with the J2EE specification, so it's not appropriate to use it in that environment. | 
			   
			 
		   | 
		 
		
		  | Back to top | 
		  
		  	
		   | 
		 
		
		    | 
		 
		
		  | marcin.kasinski | 
		  
		    
			  
				 Posted: Sun May 06, 2007 10:58 pm    Post subject:  | 
				     | 
			   
			 
		   | 
		 
		
		   Sentinel
 
 Joined: 21 Dec 2004 Posts: 850 Location: Poland / Warsaw 
  | 
		  
		    
			  
				I haven't thought about performance only.
 
 
Sometimes you have framework based on MQ base classes which does lots of things for you (MQ and some business logic).
 
 
I think we can not say "newer use...".
 
There are exceptions. _________________ Marcin | 
			   
			 
		   | 
		 
		
		  | Back to top | 
		  
		  	
		   | 
		 
		
		    | 
		 
		
		  | fjb_saper | 
		  
		    
			  
				 Posted: Mon May 07, 2007 4:13 pm    Post subject:  | 
				     | 
			   
			 
		   | 
		 
		
		    Grand High Poobah
 
 Joined: 18 Nov 2003 Posts: 20768 Location: LI,NY 
  | 
		  
		    
			  
				
   
	| marcin.kasinski wrote: | 
   
  
	I haven't thought about performance only.
 
 
Sometimes you have framework based on MQ base classes which does lots of things for you (MQ and some business logic).
 
 
I think we can not say "newer use...".
 
There are exceptions. | 
   
 
 
You may notice that I did not say always use or never use... _________________ MQ & Broker admin | 
			   
			 
		   | 
		 
		
		  | Back to top | 
		  
		  	
		   | 
		 
		
		    | 
		 
		
		  | 
		    
		   | 
		 
	   
	 | 
   
 
  
	     | 
	 | 
	Page 1 of 1 | 
   
 
 
 
  
  	
	  
		
		  
 
  | 
		  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
  | 
  		 
	   
	 | 
   
 
  	 | 
	  |