View Javadoc
1   /*
2    * JBoss, Home of Professional Open Source
3    * Copyright 2014, Red Hat, Inc. and/or its affiliates, and individual
4    * contributors by the @authors tag. See the copyright.txt in the
5    * distribution for a full listing of individual contributors.
6    *
7    * Licensed under the Apache License, Version 2.0 (the "License");
8    * you may not use this file except in compliance with the License.
9    * You may obtain a copy of the License at
10   * http://www.apache.org/licenses/LICENSE-2.0
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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   * <p>
40   * A simple servlet 3 as client that sends several messages to a queue or a topic.
41   * </p>
42   * 
43   * <p>
44   * The servlet is registered and mapped to /HelloWorldMDBServletClient using the {@linkplain WebServlet
45   * @HttpServlet}.
46   * </p>
47   * 
48   * @author Serge Pagop (spagop@redhat.com)
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 }