gpt4 book ai didi

java - 消息驱动 Bean - 连续循环

转载 作者:太空宇宙 更新时间:2023-11-04 12:09:30 25 4
gpt4 key购买 nike

请参阅下面的代码:

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);
}

}

我添加此类,然后在 Netbeans 7.4 中重新启动 Glassfish 服务器。然后,Glassfish 日志似乎表明 OnMessage 正在连续循环中运行,并继续运行直到 Netbeans 崩溃。 Student 对象包含我从此 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>

}

为什么会不断循环?我已经删除了 servlet,但似乎仍然存在连续循环。

最佳答案

我在使用 MDB 时遇到了类似的问题。我通过查找队列解决了这个问题。实际中尝试换行

@Resource(mappedName="jms/StudentMessage")

@Resource(lookup="jms/StudentMessage")

关于java - 消息驱动 Bean - 连续循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39981797/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com