I have problemw with JMS Message listener, and its not consuming message from queue, once I restart server then its sending message form queue, no exception or error thrown.
onMessage() in message listener is not firing always..how to resolve the issue.
Even no Exception showed in the server logs. I am using sun java server8.2
Then I tried to implement Exception listner on Connection but its throwing some other error
com.sun.messaging.jms.JMSException: MQRA:CA:Unsupported-setClientID() Exceptiion
here two problems 1 how to resolve Onmessage() issue to consume messages
second how to implement Exception listner. here iam creating Queue connection and Session one time at GatewayServlet init() method
flow is GatewayServlet init()--> calls -->GatewayMessageReceiver init() method when GatewayServlet loads into sun java applicaiton server or deployed into sun java app server.
Then init() method in GatewayMessageReceiver class creates jms session and queue connection.
Here GatewayMessageReceiver implements Message listner class...
Here problem is onMessage() is not calling for some times, when I do restart server its calling onMessage(). but it should call when ever message arrives in Queue, its not happning and no Error or Exception thrown.
I want to implement Exception listner but its thrwoing Errors
could you please help me in this case Ciaran McHale
please find below code
import java.util.*;
import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class GatewayServlet extends HttpServlet {
private GatewayMessageReceiver receiver = null;
/** Initializes the servlet.
*/
public void init(ServletConfig config) throws ServletException {
super.init(config);
receiver = new GatewayMessageReceiver(); //here iam calling my GatewayMessageReceiver for JMS connection creations
info(""+receiver);
}
/** Destroys the servlet
*/
public void destroy() {
if (receiver != null) {
receiver.destroy();
}
}
protected void processGatewayRequest(ServletRequest request, ServletResponse response)
throws ServletException, java.io.IOException {
//doing some business logic
}
protected void processRequest(ServletRequest request, ServletResponse response)
throws ServletException, java.io.IOException {
CCMLogger.getGatewayLogger(GeneralConfigurator.getInstance().getUtility()).debug("Host sending request is:"+request.getRemoteHost());
//check whether it's a push request
processGatewayRequest(request, response);
}
/** Handles the HTTP <code>POST</code> method.
* @param request servlet request
* @param response servlet response
*/
public void service(ServletRequest request, ServletResponse response)
throws ServletException, java.io.IOException {
processRequest(request, response);
}
public void doGet(ServletRequest request, ServletResponse response)
throws ServletException, java.io.IOException {
service(request, response);
}
public void doPost(ServletRequest request, ServletResponse response)
throws ServletException, java.io.IOException {
service(request, response);
}
}
and my JMS MESSAGE LISTNER IS
import javax.jms.*;
import java.util.logging.*;
import com.carrier.ccm.business.*;
import com.carrier.ccm.gateway.service.*;
import com.carrier.ccm.gateway.config.*;
import com.carrier.ccm.service.*;
import com.carrier.ccm.logging.*;
import com.carrier.ccm.util.*;
import com.carrier.ccm.exception.*;
/**
*
* @author Administrator
*/
public class GatewayMessageReceiver implements MessageListener {
private QueueConnection connection = null;
/** Creates a new instance of GatewayMessageReceiver */
public GatewayMessageReceiver() {
super();
init();
}
private void init() {
QueueSession session = null;
QueueReceiver queueReceiver = null;
try{
String queueName = "infoQueue";//its sun java app sever queue name
String qcfName = "infoQueueCF";//connectionfactory created in sun java app sever
Logger.log.log(Level.INFO, "Queue name: "+queueName);
Logger.log.log(Level.INFO, "Queue CF name: "+qcfName);
QueueConnectionFactory qcf =
(QueueConnectionFactory)JndiUtilities.get(qcfName);
Logger.log.log(Level.INFO, "Queue CF: "+qcf);
Queue queue =
(Queue)JndiUtilities.get(queueName);
Logger.log.log(Level.INFO, "Queue: "+queue);
// Creating a QueueConnection to the Message service");
connection = qcf.createQueueConnection();
// Creating a session within the connection
session = connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
// Creating a QueueReceiver
queueReceiver = session.createReceiver(queue);
// setting up a message listener
queueReceiver.setMessageListener(this);
//Starting the Connection
connection.start();
} catch (Throwable t) {
Logger.log(Level.SEVERE, "Failed to start queue listener for business messages", t);
}
}
public void destroy() {
try {
if (connection != null) {
connection.close();
}
} catch (Throwable t) {
Logger.log(Level.SEVERE, "Failed to close queue connection", t);
}
}
public void onMessage(javax.jms.Message message) {
String ut = null;
try {
String utm = message.getStringProperty(IConstants.UTILITY_TAG);
int bcDelay = message.getIntProperty(IConstants.BC_DELAY);
//it must be an ObjectMessage!
ObjectMessage omsg = (ObjectMessage)message;
//Here iam doing business logic
} catch (Throwable t) {
Logger.log(Level.SEVERE, "Failed to process business message", t);
}
}
}
THE JNDI UTILITIES CLASS
import javax.naming.*;
import javax.sql.*;
/**
*
* @author Administrator
*/
public class JndiUtilities {
private static Context context = null;
static {
setJndiContext();
}
/** Creates a new instance of JndiUtilities */
private JndiUtilities() {
super();
}
private static void setJndiContext() {
try {
context = new InitialContext();
} catch (Exception e) {
System.err.println("ERROR getting JNDI context: "+e);
}
}
public static Object get(String name) {
if (context == null) {
setJndiContext();
if (context == null) return null;
}
Object obj;
try {
obj = context.lookup(name);
} catch (Exception e) {
obj = null;
System.err.println("ERROR getting JNDI resource named \""+name+"\": "+e);
}
return obj;
}
}