0
votes

i need help with this kind of 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}

This is my Entity Class

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

here is my Message-Driven Beans class:

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

    }

In the end my class to communication with 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;
    }

Can you help me with this issue?

2
New kind of excetption: 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}Mac

2 Answers

0
votes

I discover that problem can be probable in Faces file:

<?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>
0
votes

I had to change from this declaration:

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

to this:

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

It's generate from NetBeans 8.1 probably differently. Now this code work perfectly