我创建了一个Java EE 6应用程序并实现了插入,更新和删除。他们有时工作正常,但突然停止工作。我调试了,我发现有时候JMS消息不会从servlet传递到消息驱动的bean。因此,在这种情况下,我的数据不会被插入,更新或删除。这是使用持久性和javaDB的Java EE应用程序的正常行为吗?或者我犯了错误?
在servlet中我有这个:
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
String id=request.getParameter("id");
String name=request.getParameter("name");
String description=request.getParameter("description");
if ((name!=null) && (description!=null)) {
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
AuthorEntity e = new AuthorEntity();
e.setAuthorName(name);
e.setDescription(description);
message.setObject(e);
messageProducer.send(message);
messageProducer.close();
connection.close();
response.sendRedirect("ListAuthors");
} catch (JMSException ex) {
ex.printStackTrace();
}
}
PrintWriter out = response.getWriter();
try {
/*
* TODO output your page here. You may use following sample code.
*/
out.println("<html>");
out.println("<head>");
out.println("<title>New Author</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>New Author</h1>");
int n=authorEntityFacade.count();
out.println("<form>");
out.println("Author ID : <input type='number' name='id' value='"+(n+1)+"'><br/>");
out.println("Author Name : <input type='text' name='name'><br/>");
out.println("Description : <textarea name='description'></textarea><br/>");
out.println("<input type='submit'><br/>");
out.println("</form>");
out.println("</body>");
out.println("</html>");
} finally {
out.close();
}
}
这里我准备了jms消息并发送到消息驱动的bean。
在消息驱动的bean中,我有:
@Resource
private MessageDrivenContext mdc;
@PersistenceContext(unitName = "IT11032748_Book_Catalogue-ejbPU")
private EntityManager em;
public NewAuthor() {
}
@Override
public void onMessage(Message message) {
ObjectMessage objectMessage=null;
try{
if(message instanceof ObjectMessage){
objectMessage=(ObjectMessage)message;
AuthorEntity author=(AuthorEntity)objectMessage.getObject();
save(author);
}
}catch(JMSException e){
e.printStackTrace();
mdc.setRollbackOnly();
}catch(Throwable te){
te.printStackTrace();
}
}
public void save(Object object) {
em.persist(object);
}
在这里,我接受消息并插入或者使用watever。
调试时,所有值都已正确设置,并且还会发送servlet消息。但是,调试点有时不会移动到消息驱动的bean:(