[jboss-user] [EJB 3.0] - Re: Help need to solve the jms not bound error
houston_dragon
do-not-reply at jboss.com
Fri Nov 7 10:49:07 EST 2008
PeterJ:
I was following the tutorial
http://www.netbeans.org/kb/60/javaee/ejb30.html
Thanks
| /*
| * To change this template, choose Tools | Templates
| * and open the template in the editor.
| */
|
| package ejb;
|
| import javax.annotation.Resource;
| import javax.ejb.ActivationConfigProperty;
| import javax.ejb.MessageDriven;
| import javax.ejb.MessageDrivenContext;
| 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 swu3
| */
| @MessageDriven(mappedName = "jms/NewMessage", activationConfig = {
| @ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge"),
| @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue")
|
| })
|
| public class NewMessageBean implements MessageListener {
|
| @Resource
| private MessageDrivenContext mdc;
|
| @PersistenceContext
| private EntityManager em;
|
| public NewMessageBean() {
| }
|
| public void onMessage(Message message) {
| ObjectMessage msg = null;
| try {
| if (message instanceof ObjectMessage) {
| msg = (ObjectMessage) message;
| NewsEntity e = (NewsEntity) msg.getObject();
| save(e);
| }
| } catch (JMSException e) {
| e.printStackTrace();
| mdc.setRollbackOnly();
| } catch (Throwable te) {
| te.printStackTrace();
| }
| }
|
| public void save(Object object) {
| em.persist(object);
| }
|
| }
|
| /*
| * To change this template, choose Tools | Templates
| * and open the template in the editor.
| */
|
| package ejb;
|
| import java.io.Serializable;
| import javax.persistence.Entity;
| import javax.persistence.GeneratedValue;
| import javax.persistence.GenerationType;
| import javax.persistence.Id;
|
| /**
| *
| * @author swu3
| */
| @Entity
| public class NewsEntity implements Serializable {
| private static final long serialVersionUID = 1L;
| @Id
| @GeneratedValue(strategy = GenerationType.AUTO)
| private Long id;
|
| private String title;
| private String body;
|
| public void setBody(String body) {
| this.body = body;
| }
|
| public void setTitle(String title) {
| this.title = title;
| }
|
| public String getBody() {
| return body;
| }
|
| public String getTitle() {
| return title;
| }
|
| 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 NewsEntity)) {
| return false;
| }
| NewsEntity other = (NewsEntity) 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.NewsEntity[id=" + id + "]";
| }
|
| }
|
| /*
| * To change this template, choose Tools | Templates
| * and open the template in the editor.
| */
|
| package ejb;
|
| import java.util.List;
| import javax.ejb.Stateless;
| import javax.persistence.EntityManager;
| import javax.persistence.PersistenceContext;
|
| /**
| *
| * @author swu3
| */
| @Stateless
| public class NewsEntityFacade implements NewsEntityFacadeLocal {
| @PersistenceContext
| private EntityManager em;
|
| public void create(NewsEntity newsEntity) {
| em.persist(newsEntity);
| }
|
| public void edit(NewsEntity newsEntity) {
| em.merge(newsEntity);
| }
|
| public void remove(NewsEntity newsEntity) {
| em.remove(em.merge(newsEntity));
| }
|
| public NewsEntity find(Object id) {
| return em.find(ejb.NewsEntity.class, id);
| }
|
| public List<NewsEntity> findAll() {
| return em.createQuery("select object(o) from NewsEntity as o").getResultList();
| }
|
| }
|
| /*
| * To change this template, choose Tools | Templates
| * and open the template in the editor.
| */
|
| package ejb;
|
| import java.util.List;
| import javax.ejb.Local;
|
| /**
| *
| * @author swu3
| */
| @Local
| public interface NewsEntityFacadeLocal {
|
| void create(NewsEntity newsEntity);
|
| void edit(NewsEntity newsEntity);
|
| void remove(NewsEntity newsEntity);
|
| NewsEntity find(Object id);
|
| List<NewsEntity> findAll();
|
| }
|
| /*
| * To change this template, choose Tools | Templates
| * and open the template in the editor.
| */
|
| package web;
|
| import ejb.NewsEntity;
| import ejb.NewsEntityFacadeLocal;
| import java.io.IOException;
| import java.io.PrintWriter;
| import java.util.Iterator;
| import java.util.List;
| import javax.ejb.EJB;
| import javax.servlet.ServletException;
| import javax.servlet.http.HttpServlet;
| import javax.servlet.http.HttpServletRequest;
| import javax.servlet.http.HttpServletResponse;
|
| /**
| *
| * @author swu3
| */
| public class ListNews extends HttpServlet {
| @EJB
| private NewsEntityFacadeLocal newsEntityFacade;
|
| /**
| * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
| * @param request servlet request
| * @param response servlet response
| */
| protected void processRequest(HttpServletRequest request, HttpServletResponse response)
| throws ServletException, IOException {
| response.setContentType("text/html;charset=UTF-8");
| PrintWriter out = response.getWriter();
| try {
|
| out.println("<html>");
| out.println("<head>");
| out.println("<title>Servlet ListNews</title>");
| out.println("</head>");
| out.println("<body>");
| out.println("<h1>Servlet ListNews at " + request.getContextPath () + "</h1>");
| List news = newsEntityFacade.findAll();
| for (Iterator it = news.iterator(); it.hasNext();) {
| NewsEntity elem = (NewsEntity) it.next();
| out.println(" <b>" + elem.getTitle() + " </b><br />");
| out.println(elem.getBody() + "<br /> ");
| }
| out.println("<a href='PostMessage'>Add new message</a>");
|
| out.println("</body>");
| out.println("</html>");
|
| } finally {
| out.close();
| }
| }
|
| // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
| /**
| * Handles the HTTP <code>GET</code> method.
| * @param request servlet request
| * @param response servlet response
| */
| protected void doGet(HttpServletRequest request, HttpServletResponse response)
| throws ServletException, IOException {
| processRequest(request, response);
| }
|
| /**
| * Handles the HTTP <code>POST</code> method.
| * @param request servlet request
| * @param response servlet response
| */
| protected void doPost(HttpServletRequest request, HttpServletResponse response)
| throws ServletException, IOException {
| processRequest(request, response);
| }
|
| /**
| * Returns a short description of the servlet.
| */
| public String getServletInfo() {
| return "Short description";
| }// </editor-fold>
|
| }
|
| /*
| * To change this template, choose Tools | Templates
| * and open the template in the editor.
| */
|
| package web;
|
| import ejb.NewsEntity;
| import java.io.IOException;
| import java.io.PrintWriter;
| import javax.annotation.Resource;
| import javax.jms.Connection;
| import javax.jms.ConnectionFactory;
| import javax.jms.JMSException;
| import javax.jms.MessageProducer;
| import javax.jms.ObjectMessage;
| import javax.jms.Session;
| import javax.jms.Queue;
| import javax.servlet.ServletException;
| import javax.servlet.http.HttpServlet;
| import javax.servlet.http.HttpServletRequest;
| import javax.servlet.http.HttpServletResponse;
|
| /**
| *
| * @author swu3
| */
| public class PostMessage extends HttpServlet {
|
| @Resource(mappedName="jms/NewMessageFactory")
| private ConnectionFactory connectionFactory;
|
| @Resource(mappedName="jms/NewMessage")
| private Queue queue;
|
| /**
| * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
| * @param request servlet request
| * @param response servlet response
| */
| protected void processRequest(HttpServletRequest request, HttpServletResponse response)
| throws ServletException, IOException {
| response.setContentType("text/html;charset=UTF-8");
|
| // Add the following code to send the JMS message
| String title = request.getParameter("title");
| String body = request.getParameter("body");
| if ((title != null) && (body != 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
| NewsEntity e = new NewsEntity();
| e.setTitle(title);
| e.setBody(body);
|
| message.setObject(e);
| messageProducer.send(message);
| messageProducer.close();
| connection.close();
| response.sendRedirect("ListNews");
|
| } catch (JMSException ex) {
| ex.printStackTrace();
| }
| }
|
|
|
|
| PrintWriter out = response.getWriter();
| try {
|
| out.println("<html>");
| out.println("<head>");
| out.println("<title>Servlet PostMessage</title>");
| out.println("</head>");
| out.println("<body>");
| out.println("<h1>Servlet PostMessage at " + request.getContextPath () + "</h1>");
|
| // The following code adds the form to the web page
| out.println("<form>");
| out.println("Title: <input type='text' name='title'><br/>");
| out.println("Message: <textarea name='body'></textarea><br/>");
| out.println("<input type='submit'><br/>");
| out.println("</form>");
|
|
| out.println("</body>");
| out.println("</html>");
|
| } finally {
| out.close();
| }
| }
|
| // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
| /**
| * Handles the HTTP <code>GET</code> method.
| * @param request servlet request
| * @param response servlet response
| */
| protected void doGet(HttpServletRequest request, HttpServletResponse response)
| throws ServletException, IOException {
| processRequest(request, response);
| }
|
| /**
| * Handles the HTTP <code>POST</code> method.
| * @param request servlet request
| * @param response servlet response
| */
| protected void doPost(HttpServletRequest request, HttpServletResponse response)
| throws ServletException, IOException {
| processRequest(request, response);
| }
|
| /**
| * Returns a short description of the servlet.
| */
| public String getServletInfo() {
| return "Short description";
| }// </editor-fold>
|
| }
|
View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4187759#4187759
Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4187759
More information about the jboss-user
mailing list