【问题标题】:My code receives the message from the queue multiple time, I want to receive it only one time. How can I do it?我的代码多次从队列中接收消息,我只想接收一次。我该怎么做?
【发布时间】:2022-10-07 20:40:19
【问题描述】:

我是新手,所以我想我的问题相对简单。

我正在使用 Websphere Application Server 平台和默认 JMS 提供程序从队列发送和接收消息。这是我的应用程序的样子:

My App

Saytime 是我的主要 servlet,它将我的代码重新路由到 .jsp 文件。 Produce 按钮向应用程序发送以下代码并生成写入框中的消息:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String queueName = "jms/helloqueue";
    Context jndiContext = null;
    QueueConnectionFactory queueConnectionFcatory = null;
    QueueConnection queueConnection = null;
    QueueSession queueSession = null;
    QueueSender queueSender = null;
    Queue queue = null;
    TextMessage textMessage = null;
    response.setContentType("text/html");
    request.setCharacterEncoding("UTF-8"); // To information the that you may use Unicode characters
    response.setCharacterEncoding("UTF-8");
    String txt = request.getParameter("text");
   
    try {
        Properties initialProperties = new Properties();
        initialProperties.put(InitialContext.INITIAL_CONTEXT_FACTORY, "com.ibm.websphere.naming.WsnInitialContextFactory");
        initialProperties.put(InitialContext.PROVIDER_URL, "iiop://localhost:2810");
        jndiContext = new InitialContext(initialProperties);
   } catch (NamingException e) {
        e.printStackTrace();
        System.exit(1);
    }
    try {
        queueConnectionFcatory = (QueueConnectionFactory) jndiContext.lookup("jms/helloqcf");
        queue = (Queue) jndiContext.lookup(queueName);
    } catch (NamingException e) {
        e.printStackTrace();
        System.exit(1);
    }
    try {
        queueConnection = queueConnectionFcatory.createQueueConnection();
        queueSession = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
        queueSender = queueSession.createSender(queue);
        textMessage = queueSession.createTextMessage();
        
        textMessage.setText(txt);
        queueSender.send(textMessage);
        
    } catch (JMSException e) {
        System.out.println("JMS Exception occured: "+ e.getMessage());
    }finally{
        if(queueConnection != null){
            try{
                Thread.sleep(6000);
                queueConnection.close();
            } catch(Exception e){}
        }
    }
    RequestDispatcher rd = request.getRequestDispatcher("saytime");
    rd.forward(request,response);
}

接收按钮将我的应用程序发送到以下 servlet 代码并从队列接收消息:

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String queueName = "jms/helloqueue";
    Context jndiContext = null;
    QueueConnectionFactory queueConnectionfactory = null;
    QueueConnection queueConnection = null;
    QueueSession queueSession = null;
    QueueReceiver queueReceiver = null;
    Queue queue = null;
    String text = null;

    try {
        Properties initialProperties = new Properties();
        initialProperties.put(InitialContext.INITIAL_CONTEXT_FACTORY, "com.ibm.websphere.naming.WsnInitialContextFactory");
        initialProperties.put(InitialContext.PROVIDER_URL,"iiop://localhost:2810");
        jndiContext = new InitialContext(initialProperties);
    } catch (NamingException e) {
        System.out.println("JNDI exception occured: " + e.getMessage());
        System.exit(1);
    }

    try {
        queueConnectionfactory = (QueueConnectionFactory) jndiContext.lookup("jms/helloqcf");
        queue = (Queue) jndiContext.lookup(queueName);
    } catch (NamingException e) {
        System.exit(1);
    }
    try{
        queueConnection = queueConnectionfactory.createQueueConnection();
        queueSession = queueConnection.createQueueSession(true,Session.AUTO_ACKNOWLEDGE);
        queueReceiver = queueSession.createReceiver(queue);
        //queueReceiver.setMessageListener(listener);
        queueConnection.start();
        text = queueReceiver.receive().toString();
        
    } catch(JMSException e){
        System.out.println("Exception occured: "+ e.getMessage());
    }
    finally{
        if (queueConnection != null) {
            try {
                queueConnection.close();
            } catch (JMSException e) {
            }
        }
    }

    if(text != null) {
        request.setAttribute("message", text.toString());
    }
    RequestDispatcher rd = request.getRequestDispatcher("saytime");
    rd.forward(request,response);
}

之后,我在 .jsp 文件中使用这个小代码打印消息:

        <%
    String getValues = (String) request.getAttribute("message");
    %>
    <%
    if(getValues != null){
        out.println("<p>" + getValues + "</p>");
    }else{
        out.println("<p> There is no message </p>");
    }
    %>

问题是这样的:我可以接收我生成的消息,但是按钮会继续接收消息,直到 JMSXDeliveryCount 的计数达到 5。大多数 JMSXDeliveryCount 从 1 开始,我总共可以收到 5 次消息。我只想收到一次,然后消息消失。

此外,我想知道如何只打印我的消息。我打印了额外的细节,就像你在图片中看到的那样。如果可能的话,我不想那样。

我试图限制重新投递数量,但我想我无法想出正确的代码。另外,我尝试使用不同的确认模式,但它也不起作用。

我对此感到非常困惑,一些帮助将是完美的。谢谢。

【问题讨论】:

    标签: queue jms websphere message-queue


    【解决方案1】:

    问题是您将消费者的会话创建为成交.看到这一行:

    queueSession = queueConnection.createQueueSession(true,Session.AUTO_ACKNOWLEDGE);
    

    由于会话被处理,确认模式将被忽略。因此,您应该确认消息并手动提交会话,或者使用非事务会话并允许根据确认模式自动确认消息。

    【讨论】:

      猜你喜欢
      • 2014-05-06
      • 2011-08-01
      • 2022-08-12
      • 1970-01-01
      • 2023-03-31
      • 1970-01-01
      • 2020-05-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多