SalutationMessageBean.java
View Code
1
/*
2 * To change this template, choose Tools | Templates
3 * and open the template in the editor.
4 */
5 package beans;
6
7 import java.util.logging.Level;
8 import java.util.logging.Logger;
9 import javax.ejb.ActivationConfigProperty;
10 import javax.ejb.MessageDriven;
11 import javax.jms.JMSException;
12 import javax.jms.Message;
13 import javax.jms.MessageListener;
14 import javax.jms.TextMessage;
15
16 /**
17 *
18 * @author teemu
19 */
20 @MessageDriven(mappedName = "jms/SalutationQueue", activationConfig = {
21 @ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge"),
22 @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue")
23 })
24 public class SalutationMessageBean implements MessageListener {
25
26 // public default constructor is mandatory
27 public SalutationMessageBean() {
28 }
29
30 @Override
31 public void onMessage(Message message) {
32 String id;
33 try {
34 // simply create add a row to logfile "SalutationLog"
35 // to show that a message has been processed. Log is shown in
36 // GlassFish Console in NetBeans.
37 id = message.getStringProperty("ID");
38 TextMessage tm = (TextMessage)message;
39 Logger.getLogger("SalutationLog").log(Level.INFO,
40 "Salutation processed with ID: "+id+". Message was: "+tm.getText(), "");
41 } catch (JMSException ex) {
42 Logger.getLogger("SalutationLog").log(Level.SEVERE, null, ex);
43 }
44 }
45 }
2 * To change this template, choose Tools | Templates
3 * and open the template in the editor.
4 */
5 package beans;
6
7 import java.util.logging.Level;
8 import java.util.logging.Logger;
9 import javax.ejb.ActivationConfigProperty;
10 import javax.ejb.MessageDriven;
11 import javax.jms.JMSException;
12 import javax.jms.Message;
13 import javax.jms.MessageListener;
14 import javax.jms.TextMessage;
15
16 /**
17 *
18 * @author teemu
19 */
20 @MessageDriven(mappedName = "jms/SalutationQueue", activationConfig = {
21 @ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge"),
22 @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue")
23 })
24 public class SalutationMessageBean implements MessageListener {
25
26 // public default constructor is mandatory
27 public SalutationMessageBean() {
28 }
29
30 @Override
31 public void onMessage(Message message) {
32 String id;
33 try {
34 // simply create add a row to logfile "SalutationLog"
35 // to show that a message has been processed. Log is shown in
36 // GlassFish Console in NetBeans.
37 id = message.getStringProperty("ID");
38 TextMessage tm = (TextMessage)message;
39 Logger.getLogger("SalutationLog").log(Level.INFO,
40 "Salutation processed with ID: "+id+". Message was: "+tm.getText(), "");
41 } catch (JMSException ex) {
42 Logger.getLogger("SalutationLog").log(Level.SEVERE, null, ex);
43 }
44 }
45 }
SalutationQueueServlet.java
View Code
1
/*
2 * To change this template, choose Tools | Templates
3 * and open the template in the editor.
4 */
5 package servlets;
6
7 import java.io.IOException;
8 import java.io.PrintWriter;
9 import javax.jms.Queue;
10 import java.util.logging.Level;
11 import java.util.logging.Logger;
12 import javax.annotation.Resource;
13 import javax.jms.*;
14 import javax.servlet.ServletException;
15 import javax.servlet.annotation.WebServlet;
16 import javax.servlet.http.HttpServlet;
17 import javax.servlet.http.HttpServletRequest;
18 import javax.servlet.http.HttpServletResponse;
19
20 /**
21 *
22 * @author teemu
23 */
24 @WebServlet(name = "SalutationQueueServlet", urlPatterns = {"/SalutationQueueServlet"})
25 public class SalutationQueueServlet extends HttpServlet {
26
27 // use dependency injection to get connection factory and queue resources
28 // You can find these names in the GlassFish Admin Console after
29 // deploying the message-driven salutation bean.
30 @Resource(mappedName = "jms/SalutationQueueFactory")
31 private QueueConnectionFactory connFactory;
32 @Resource(mappedName = "jms/SalutationQueue")
33 private Queue queue;
34
35 /**
36 * Processes requests for both HTTP
37 * <code>GET</code> and
38 * <code>POST</code> methods.
39 *
40 * @param request servlet request
41 * @param response servlet response
42 * @throws ServletException if a servlet-specific error occurs
43 * @throws IOException if an I/O error occurs
44 */
45 protected void processRequest(HttpServletRequest request, HttpServletResponse response)
46 throws ServletException, IOException {
47 response.setContentType("text/html;charset=UTF-8");
48 PrintWriter out = response.getWriter();
49 try {
50
51 String msg = "A message to the bean";
52
53 // get connection from the connection factory
54 Connection connection = connFactory.createConnection();
55
56 // create new session which will be used to facilitate communication
57 Session session = connection.createSession( false, Session.AUTO_ACKNOWLEDGE);
58
59 // create MessageProducer with session. MessageProducer is used
60 // to create and send JMS messages.
61 MessageProducer messageProducer = (MessageProducer) session.createProducer(queue);
62
63 // Finally, create a new TextMessage and send it to the queue.
64 TextMessage textMessage = session.createTextMessage();
65 textMessage.setText(msg);
66 textMessage.setStringProperty("ID", "1");
67 messageProducer.send(textMessage);
68
69 Logger.getLogger("SalutationLog").log(Level.INFO,
70 "Message sent successfully", "Message was sent successfully!");
71
72 out.println("<html>");
73 out.println("<head>");
74 out.println("<title>Servlet SalutationQueueServlet</title>");
75 out.println("</head>");
76 out.println("<body>");
77 out.println("<h1>Servlet SalutationQueueServlet at " + request.getContextPath() + "</h1>");
78 out.println("</body>");
79 out.println("</html>");
80 } catch (JMSException ex) {
81 Logger.getLogger("SalutationLog").log(Level.WARNING,
82 "JMSException in SalutationServlet",
83 "JMSException in SalutationServlet");
84 } finally {
85 out.close();
86 }
87 }
88
89 // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
90 /**
91 * Handles the HTTP
92 * <code>GET</code> method.
93 *
94 * @param request servlet request
95 * @param response servlet response
96 * @throws ServletException if a servlet-specific error occurs
97 * @throws IOException if an I/O error occurs
98 */
99 @Override
100 protected void doGet(HttpServletRequest request, HttpServletResponse response)
101 throws ServletException, IOException {
102 processRequest(request, response);
103 }
104
105 /**
106 * Handles the HTTP
107 * <code>POST</code> method.
108 *
109 * @param request servlet request
110 * @param response servlet response
111 * @throws ServletException if a servlet-specific error occurs
112 * @throws IOException if an I/O error occurs
113 */
114 @Override
115 protected void doPost(HttpServletRequest request, HttpServletResponse response)
116 throws ServletException, IOException {
117 processRequest(request, response);
118 }
119
120 /**
121 * Returns a short description of the servlet.
122 *
123 * @return a String containing servlet description
124 */
125 @Override
126 public String getServletInfo() {
127 return "Short description";
128 } // </editor-fold>
129 }
2 * To change this template, choose Tools | Templates
3 * and open the template in the editor.
4 */
5 package servlets;
6
7 import java.io.IOException;
8 import java.io.PrintWriter;
9 import javax.jms.Queue;
10 import java.util.logging.Level;
11 import java.util.logging.Logger;
12 import javax.annotation.Resource;
13 import javax.jms.*;
14 import javax.servlet.ServletException;
15 import javax.servlet.annotation.WebServlet;
16 import javax.servlet.http.HttpServlet;
17 import javax.servlet.http.HttpServletRequest;
18 import javax.servlet.http.HttpServletResponse;
19
20 /**
21 *
22 * @author teemu
23 */
24 @WebServlet(name = "SalutationQueueServlet", urlPatterns = {"/SalutationQueueServlet"})
25 public class SalutationQueueServlet extends HttpServlet {
26
27 // use dependency injection to get connection factory and queue resources
28 // You can find these names in the GlassFish Admin Console after
29 // deploying the message-driven salutation bean.
30 @Resource(mappedName = "jms/SalutationQueueFactory")
31 private QueueConnectionFactory connFactory;
32 @Resource(mappedName = "jms/SalutationQueue")
33 private Queue queue;
34
35 /**
36 * Processes requests for both HTTP
37 * <code>GET</code> and
38 * <code>POST</code> methods.
39 *
40 * @param request servlet request
41 * @param response servlet response
42 * @throws ServletException if a servlet-specific error occurs
43 * @throws IOException if an I/O error occurs
44 */
45 protected void processRequest(HttpServletRequest request, HttpServletResponse response)
46 throws ServletException, IOException {
47 response.setContentType("text/html;charset=UTF-8");
48 PrintWriter out = response.getWriter();
49 try {
50
51 String msg = "A message to the bean";
52
53 // get connection from the connection factory
54 Connection connection = connFactory.createConnection();
55
56 // create new session which will be used to facilitate communication
57 Session session = connection.createSession( false, Session.AUTO_ACKNOWLEDGE);
58
59 // create MessageProducer with session. MessageProducer is used
60 // to create and send JMS messages.
61 MessageProducer messageProducer = (MessageProducer) session.createProducer(queue);
62
63 // Finally, create a new TextMessage and send it to the queue.
64 TextMessage textMessage = session.createTextMessage();
65 textMessage.setText(msg);
66 textMessage.setStringProperty("ID", "1");
67 messageProducer.send(textMessage);
68
69 Logger.getLogger("SalutationLog").log(Level.INFO,
70 "Message sent successfully", "Message was sent successfully!");
71
72 out.println("<html>");
73 out.println("<head>");
74 out.println("<title>Servlet SalutationQueueServlet</title>");
75 out.println("</head>");
76 out.println("<body>");
77 out.println("<h1>Servlet SalutationQueueServlet at " + request.getContextPath() + "</h1>");
78 out.println("</body>");
79 out.println("</html>");
80 } catch (JMSException ex) {
81 Logger.getLogger("SalutationLog").log(Level.WARNING,
82 "JMSException in SalutationServlet",
83 "JMSException in SalutationServlet");
84 } finally {
85 out.close();
86 }
87 }
88
89 // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
90 /**
91 * Handles the HTTP
92 * <code>GET</code> method.
93 *
94 * @param request servlet request
95 * @param response servlet response
96 * @throws ServletException if a servlet-specific error occurs
97 * @throws IOException if an I/O error occurs
98 */
99 @Override
100 protected void doGet(HttpServletRequest request, HttpServletResponse response)
101 throws ServletException, IOException {
102 processRequest(request, response);
103 }
104
105 /**
106 * Handles the HTTP
107 * <code>POST</code> method.
108 *
109 * @param request servlet request
110 * @param response servlet response
111 * @throws ServletException if a servlet-specific error occurs
112 * @throws IOException if an I/O error occurs
113 */
114 @Override
115 protected void doPost(HttpServletRequest request, HttpServletResponse response)
116 throws ServletException, IOException {
117 processRequest(request, response);
118 }
119
120 /**
121 * Returns a short description of the servlet.
122 *
123 * @return a String containing servlet description
124 */
125 @Override
126 public String getServletInfo() {
127 return "Short description";
128 } // </editor-fold>
129 }
index.jsp
View Code
1 <%--
2 Document : index
3 Created on : Mar 15, 2012, 10:55:19 AM
4 Author : teemu
5 --%>
6
7 <%@page contentType="text/html" pageEncoding="UTF-8"%>
8 <!DOCTYPE html>
9 <html>
10 <head>
11 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
12 <title>SalutationQueueApplication</title>
13 </head>
14 <body>
15 <h1><a href="SalutationQueueServlet">Click here to access the servlet</a></h1>
16 </body>
17 </html>
2 Document : index
3 Created on : Mar 15, 2012, 10:55:19 AM
4 Author : teemu
5 --%>
6
7 <%@page contentType="text/html" pageEncoding="UTF-8"%>
8 <!DOCTYPE html>
9 <html>
10 <head>
11 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
12 <title>SalutationQueueApplication</title>
13 </head>
14 <body>
15 <h1><a href="SalutationQueueServlet">Click here to access the servlet</a></h1>
16 </body>
17 </html>