简体   繁体   中英

Message Driven Bean - Continuous Loop

Please see the code below:

package com.w0051977.dao;

import javax.annotation.Resource;
import javax.ejb.ActivationConfigProperty;
import javax.ejb.MessageDriven;
import javax.ejb.MessageDrivenContext;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.ObjectMessage;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import com.w0051977.model.Student;

/**
 *
 * @author 
 */
@MessageDriven(activationConfig = {
    @ActivationConfigProperty(propertyName = "destinationLookup", propertyValue = "jms/StudentMessage"),
    @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue")
})
public class StudentMessage implements MessageListener {

    @Resource
    private MessageDrivenContext mdc;
    @PersistenceContext(unitName = "CRUDWebAppPU")
    private EntityManager em;



    public StudentMessage() {
    }

    @Override
    public void onMessage(Message message) {
        ObjectMessage msg = null;
    try {
        if (message instanceof ObjectMessage) {
            msg = (ObjectMessage) message;
            Student s1 = (Student) msg.getObject();
            save(s1);            
        }
    } catch (JMSException e) {
        e.printStackTrace();
        mdc.setRollbackOnly();
    } catch (Throwable te) {
        te.printStackTrace();
    }
    }

    public void save(Object object) {
        em.persist(object);
    }

}

I add this class and then restart the Glassfish server in Netbeans 7.4. The Glassfish logs then seem to indicate that OnMessage is running in a continuous loop and continues to run until Netbeans crashes. The Student object contains values, which I feed in from this Servlet:

package com.w0051977.controller;

import java.io.IOException;
import java.io.PrintWriter;
import javax.annotation.Resource;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.ObjectMessage;
import javax.jms.Queue;
import javax.jms.Session;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.w0051977.model.Student;

/**
 *
 * @author 3212627
 */
@WebServlet(name = "ServletAddStudentMessageBean", urlPatterns = {"/ServletAddStudentMessageBean"})
public class ServletAddStudentMessageBean extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */

    @Resource(mappedName="jms/StudentMessageFactory")
        private  ConnectionFactory connectionFactory;

        @Resource(mappedName="jms/StudentMessage")
        private  Queue queue;

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        response.setContentType("text/html;charset=UTF-8");
            try {
                Connection connection = connectionFactory.createConnection();
                Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
                MessageProducer messageProducer = session.createProducer(queue);

                ObjectMessage message = session.createObjectMessage();
                // here we create NewsEntity, that will be sent in JMS message
                Student s1 = new Student();
                s1.setFirstName("Fred");
                s1.setLastName("Bloggs");
                s1.setYearLevel(3);

                message.setObject(s1);                
                messageProducer.send(message);
                messageProducer.close();
                connection.close();
                response.sendRedirect("StudentInfo");

            } catch (JMSException ex) {
                ex.printStackTrace();
            }

           PrintWriter out = response.getWriter();

           out.println("Servlet PostMessage at " + request.getContextPath() + "</h1>");

    // The following code adds the form to the web page
    out.println("<form>");
    out.println("Title: <input type='text' name='title'><br/>");
    out.println("Message: <textarea name='body'></textarea><br/>");
    out.println("<input type='submit'><br/>");
    out.println("</form>");

    out.println("</body>");


        }

        // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
        /**
         * Handles the HTTP <code>GET</code> method.
         *
         * @param request servlet request
         * @param response servlet response
         * @throws ServletException if a servlet-specific error occurs
         * @throws IOException if an I/O error occurs
         */
        @Override
        protected void doGet(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            processRequest(request, response);
        }

        /**
         * Handles the HTTP <code>POST</code> method.
         *
         * @param request servlet request
         * @param response servlet response
         * @throws ServletException if a servlet-specific error occurs
         * @throws IOException if an I/O error occurs
         */
        @Override
        protected void doPost(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            processRequest(request, response);
        }

        /**
         * Returns a short description of the servlet.
         *
         * @return a String containing servlet description
         */
        @Override
        public String getServletInfo() {
            return "Short description";
        }// </editor-fold>

    }

Why is there a continuous loop? I have deleted the servlet and there still appears to be a continuous loop.

I had a similar problem with a MDB. I solved that doing the lookup of the Queue. In practice, try to change the line

@Resource(mappedName="jms/StudentMessage")

with

@Resource(lookup="jms/StudentMessage")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM