2016-05-06 17 views
0

i mit dieser Art von Problem Hilfe benötigen:Java Beans MessageDriven Ausgabe

Ausnahme versucht Ungelöste Nachricht-Destination-Ref web.News/[email protected]@null in Klasse web.News zu injizieren : Suche fehlgeschlagen für '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}

Das ist meine Entity Klasse

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

hier ist meine Message-Driven Beans Klasse:

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

    } 

Am Ende meiner Klasse auf die Kommunikation mit Javaserver Faces:

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

Können Sie mir helfen mit diesem Problem?

+0

Neue Art von Exception: Exception versucht, nicht aufgelöst Nachricht-Destination-Ref web.News/[email protected]@null in Klasse Web.News: Suche fehlgeschlagen für '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} – Mac

Antwort

0

ich aus dieser Erklärung zu ändern, hatte NetBeans 8.1 wahrscheinlich anders. Jetzt funktioniert dieser Code einwandfrei

0

ich feststellen, dass Problem in Faces Datei wahrscheinlich sein kann:

@Resource(lookup="jms/NewsQueue") 
private javax.jms.Queue queue; 

dazu:

@Resource(lookup="java:app/jms/NewsQueue") 
private javax.jms.Queue queue; 

Es zu generieren ist von

<?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> 
     <title>Facelet Title</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>