Java MessageDriven Beans Issue
我需要帮助解决此类问题:
Exception attempting to inject Unresolved Message-Destination-Ref web.News/[email protected]@null into class web.News: Lookup failed for 'java:comp/env/web.News/queue' in SerialContext[myEnv={java.naming.factory.initial=com.sun.enterprise.naming.impl.SerialInitContextFactory, java.naming.factory.state=com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl, java.naming.factory.url.pkgs=com.sun.enterprise.naming}
这是我的实体类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 | package ejb; import java.io.Serializable; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; /** * * @author Maciej1 */ @Entity public class NewsItem implements Serializable { private String heading; private String body; private static final long serialVersionUID = 1L; @Id @GeneratedValue(strategy = GenerationType.AUTO) private Long id; public Long getId() { return id; } public void setId(Long id) { this.id = id; } @Override public int hashCode() { int hash = 0; hash += (id != null ? id.hashCode() : 0); return hash; } @Override public boolean equals(Object object) { // TODO: Warning - this method won't work in the case the id fields are not set if (!(object instanceof NewsItem)) { return false; } NewsItem other = (NewsItem) object; if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) { return false; } return true; } @Override public String toString() { return"ejb.NewsItem[ id=" + id +" ]"; } /** * @return the heading */ public String getHeading() { return heading; } /** * @param heading the heading to set */ public void setHeading(String heading) { this.heading = heading; } /** * @return the body */ public String getBody() { return body; } /** * @param body the body to set */ public void setBody(String body) { this.body = body; } } |
这是我的消息驱动 Beans 类:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | package ejb; import javax.ejb.ActivationConfigProperty; import javax.ejb.MessageDriven; import javax.jms.JMSException; import javax.jms.Message; import javax.jms.MessageListener; import javax.jms.ObjectMessage; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; /** * * @author Maciej1 */ @MessageDriven(mappedName ="jms/NewsQueue", activationConfig = { @ActivationConfigProperty(propertyName ="destinationType", propertyValue ="javax.jms.Queue") }) public class NewsMDB implements MessageListener { @PersistenceContext(unitName ="MDBLabPU") private EntityManager em; public NewsMDB() { } @Override public void onMessage(Message message) { ObjectMessage msg = null; try { if (message instanceof ObjectMessage) { msg = (ObjectMessage) message; NewsItem e = (NewsItem) msg.getObject(); saveObject(e); } } catch (JMSException e) { e.printStackTrace(); } } public void saveObject(Object object) { em.persist(object); } } |
最后我的班级与JavaServer Faces进行通信:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | package web; import ejb.NewsItem; import ejb.NewsItemFacadeLocal; import java.util.List; import javax.annotation.Resource; import javax.ejb.EJB; import javax.enterprise.context.RequestScoped; import javax.inject.Named; import javax.jms.Connection; import javax.jms.ConnectionFactory; import javax.jms.JMSException; import javax.jms.MessageProducer; import javax.jms.ObjectMessage; import javax.jms.Session; /** * * @author Maciej1 */ @Named @RequestScoped public class News { @EJB private NewsItemFacadeLocal newsItemFacade; @Resource(lookup="java:comp/DefaultJMSConnectionFactory") private ConnectionFactory connectionFactory; @Resource(lookup="jms/NewsQueue") private javax.jms.Queue queue; private String headingText ="headingText"; private String bodyText ="bodyText"; void sendNewsItem(String heading, String body) { try { Connection connection = connectionFactory.createConnection(); Session session; session = connection.createSession(true, 0); MessageProducer messageProducer = session.createProducer(queue); ObjectMessage message = session.createObjectMessage(); NewsItem e = new NewsItem(); e.setHeading(heading); e.setBody(body); message.setObject(e); messageProducer.send(message); messageProducer.close(); connection.close(); } catch (JMSException ex) { ex.printStackTrace(); } } public List<NewsItem> getNewsItems() { return newsItemFacade.findAll(); } public String submitNews(){ sendNewsItem(getHeadingText(), getBodyText()); return null; } |
你能帮我解决这个问题吗?
我不得不改变这个声明:
1 2 | @Resource(lookup="jms/NewsQueue") private javax.jms.Queue queue; |
到这个:
1 2 | @Resource(lookup="java:app/jms/NewsQueue") private javax.jms.Queue queue; |
它从 NetBeans 8.1 生成的可能不同。现在这段代码完美运行
我发现这个问题很可能在 Faces 文件中:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | <?xml version='1.0' encoding='UTF-8' ?> <!DOCTYPE html PUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://xmlns.jcp.org/jsf/html"> <h:head> Facelet Title </h:head> <h:body> <h:form> <h:outputText></h:outputText> <h:inputText value="#{news.headingText}" id="headingInputText"></h:inputText> <h:outputText></h:outputText> <h:inputText value="#{news.bodyText}" id="bodyInputText"></h:inputText> <h:commandButton value="Submit" id="submitButton"></h:commandButton> </h:form> </h:body> </html> |