我遇到了JMS Message侦听器的问题,并且它没有消耗来自队列的消息,一旦我重新启动服务器然后它的发送消息形成队列,没有异常或抛出错误。
消息监听器中的onMessage()不会始终触发..如何解决问题。
即使服务器日志中没有显示异常。我正在使用sun java server8.2
然后我尝试在Connection上实现异常列表器,但是它抛出了一些其他错误
com.sun.messaging.jms.JMSException:MQRA:CA:Unsupported-setClientID()Exceptiion
这里有两个问题1如何解决消耗消息的Onmessage()问题
第二个如何实现Exception listner。 这里我在GatewayServlet init()方法
创建了Queue连接和Session一次flow是GatewayServlet init() - >调用 - > GatewayServlet加载到sun java应用服务器或部署到sun java app服务器时的GatewayMessageReceiver init()方法。
然后,GatewayMessageReceiver类中的init()方法创建jms会话和队列连接。
这里的GatewayMessageReceiver实现Message listner类...
这里的问题是onMessage()有时没有调用,当我重新启动服务器时调用onMessage()。但它应该在消息到达队列时调用,它不是很开心,也没有抛出错误或异常。
我想实现Exception listner但是它的错误
在这种情况下,Ciaran McHale可以帮助我吗请找到以下代码
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);
}
}
和我的JMS MESSAGE LISTNER
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);
}
}
}
JNDI实用班级
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;
}
}
答案 0 :(得分:1)
您没有为此处的人员提供任何代码进行检查,因此您不太可能获得有用的详细答案。
我的猜测是你的JMS客户端在初始化的某个地方失败了,可能在调用setClientID()
时,但你的代码是(mis)使用try-catch
子句来捕获和忽略例外。如果没有与JMS代理的正确连接,您的应用程序将不会收到任何消息。
您正在使用的JMS产品可能包含一些演示应用程序。如果是这样,那么我建议您检查它们以查看可用于初始化应用程序并正确处理可能引发的任何异常的编码步骤。演示应用程序还可能会显示如何实现ExceptionListener
。
顺便说一下,使用ExceptionListener
不意味着会向其报告所有异常。您的应用程序代码仍然需要使用try-catch
子句来确定方法调用何时立即/同步失败。 ExceptionListener
函数用于不同的目的,即在出现问题时通知您的应用程序异步。