1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.jboss.as.quickstarts.servlet;
18
19 import java.io.IOException;
20 import java.io.PrintWriter;
21
22 import javax.annotation.Resource;
23 import javax.jms.Connection;
24 import javax.jms.ConnectionFactory;
25 import javax.jms.Destination;
26 import javax.jms.JMSException;
27 import javax.jms.MessageProducer;
28 import javax.jms.Queue;
29 import javax.jms.Session;
30 import javax.jms.TextMessage;
31 import javax.jms.Topic;
32 import javax.servlet.ServletException;
33 import javax.servlet.annotation.WebServlet;
34 import javax.servlet.http.HttpServlet;
35 import javax.servlet.http.HttpServletRequest;
36 import javax.servlet.http.HttpServletResponse;
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51 @WebServlet("/HelloWorldMDBServletClient")
52 public class HelloWorldMDBServletClient extends HttpServlet {
53
54 private static final long serialVersionUID = -8314035702649252239L;
55
56 private static final int MSG_COUNT = 5;
57
58 @Resource(mappedName = "${property.connection.factory}")
59 private ConnectionFactory connectionFactory;
60
61 @Resource(mappedName = "${property.helloworldmdb.queue}")
62 private Queue queue;
63
64 @Resource(mappedName = "${property.helloworldmdb.topic}")
65 private Topic topic;
66
67 @Override
68 protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
69 resp.setContentType("text/html");
70 PrintWriter out = resp.getWriter();
71 Connection connection = null;
72 out.write("<h1>Quickstart: This example demonstrates the use of <strong>JMS 1.1</strong> and <strong>EJB 3.1 Message-Driven Bean</strong> in Red Hat JBoss Enterprise Application Platform 6.</h1>");
73 try {
74 Destination destination;
75 if (req.getParameterMap().keySet().contains("topic")) {
76 destination = topic;
77 } else {
78 destination = queue;
79 }
80 out.write("<p>Sending messages to <em>" + destination + "</em></p>");
81 connection = connectionFactory.createConnection();
82 Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
83 MessageProducer messageProducer = session.createProducer(destination);
84 connection.start();
85 out.write("<h2>Following messages will be send to the destination:</h2>");
86 TextMessage message = session.createTextMessage();
87 for (int i = 0; i < MSG_COUNT; i++) {
88 message.setText("This is message " + (i + 1));
89 messageProducer.send(message);
90 out.write("Message (" + i + "): " + message.getText() + "</br>");
91 }
92 out.write("<p><i>Go to your JBoss EAP server console or log to see the result of messages processing</i></p>");
93
94 } catch (JMSException e) {
95 e.printStackTrace();
96 out.write("<h2>A problem occurred during the delivery of this message</h2>");
97 out.write("</br>");
98 out.write("<p><i>Go your the JBoss EAP server console or log to see the error stack trace</i></p>");
99 } finally {
100 if (connection != null) {
101 try {
102 connection.close();
103 } catch (JMSException e) {
104 e.printStackTrace();
105 }
106 }
107 if (out != null) {
108 out.close();
109 }
110 }
111 }
112
113 protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
114 doGet(req, resp);
115 }
116
117 }