Java MessageDriven Bean问题

时间:2016-05-06 00:14:05

标签: jms cdi message-driven-bean

我需要这方面的帮助:

  

异常尝试将未解决的Message-Destination-Ref web.News/queue@java.lang.String@null注入到类web.News:SerialContext中的'java:comp / env / web.News / queue'查找失败[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}

这是我的实体类

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

这是我的Message-Driven Beans课程:

   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进行通信:

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

你能帮我解决这个问题吗?

2 个答案:

答案 0 :(得分:0)

我发现Faces文件中可能存在问题:

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

答案 1 :(得分:0)

我不得不改变这个宣言:

displayAd

到此:

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

它可能以不同的方式从NetBeans 8.1生成。现在这段代码完美无缺