<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<body link="#355491" alink="#4262a1" vlink="#355491" style="background: #e2e2e2; margin: 0; padding: 20px;">

<div>
        <table cellpadding="0" bgcolor="#FFFFFF" border="0" cellspacing="0" style="border: 1px solid #dadada; margin-bottom: 30px; width: 100%; -moz-border-radius: 6px; -webkit-border-radius: 6px;">
                <tbody>
                        <tr>

                                <td>

                                        <table border="0" cellpadding="0" cellspacing="0" bgcolor="#FFFFFF" style="border: solid 2px #ccc; background: #dadada; width: 100%; -moz-border-radius: 6px; -webkit-border-radius: 6px;">
                                                <tbody>
                                                        <tr>
                                                                <td bgcolor="#000000" valign="middle" height="58px" style="border-bottom: 1px solid #ccc; padding: 20px; -moz-border-radius-topleft: 3px; -moz-border-radius-topright: 3px; -webkit-border-top-right-radius: 5px; -webkit-border-top-left-radius: 5px;">
                                                                        <h1 style="color: #333333; font: bold 22px Arial, Helvetica, sans-serif; margin: 0; display: block !important;">
                                                                        <!-- To have a header image/logo replace the name below with your img tag -->
                                                                        <!-- Email clients will render the images when the message is read so any image -->
                                                                        <!-- must be made available on a public server, so that all recipients can load the image. -->
                                                                        <a href="https://community.jboss.org/index.jspa" style="text-decoration: none; color: #E1E1E1">JBoss Community</a></h1>
                                                                </td>

                                                        </tr>
                                                        <tr>
                                                                <td bgcolor="#FFFFFF" style="font: normal 12px Arial, Helvetica, sans-serif; color:#333333; padding: 20px;  -moz-border-radius-bottomleft: 4px; -moz-border-radius-bottomright: 4px; -webkit-border-bottom-right-radius: 5px; -webkit-border-bottom-left-radius: 5px;"><h3 style="margin: 10px 0 5px; font-size: 17px; font-weight: normal;">
    AS 7.1.1 send/receive a JMS message in Servlet question
</h3>
<span style="margin-bottom: 10px;">
    created by <a href="https://community.jboss.org/people/mjustin">Michael Justin</a> in <i>Beginner's Corner</i> - <a href="https://community.jboss.org/message/728702#728702">View the full discussion</a>
</span>
<hr style="margin: 20px 0; border: none; background-color: #dadada; height: 1px;">

<div class="jive-rendered-content"><p>Hello,</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>I am trying to build a simple JMS produce / consume example in a Servlet. The full source code is below.</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>The servlet uses a resource annotation to inject the JMS destination which is configured in standalone-full.xml, and I also can verify that the destination exists (in the admin interface) and user and password are valid. The produce / consume methods complete without any errors, but the web admin interface does not show any messages. I tried to use persistent messages but it did not help. I will also check out the examples in the source distribution, and come back if I found the reason.</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>package servlet;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>import java.io.IOException;</p><p>import java.io.Writer;</p><p>import java.util.logging.Logger;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>import javax.annotation.Resource;</p><p>import javax.jms.Connection;</p><p>import javax.jms.ConnectionFactory;</p><p>import javax.jms.DeliveryMode;</p><p>import javax.jms.JMSException;</p><p>import javax.jms.Message;</p><p>import javax.jms.MessageConsumer;</p><p>import javax.jms.MessageProducer;</p><p>import javax.jms.Session;</p><p>import javax.jms.TextMessage;</p><p>import javax.servlet.ServletException;</p><p>import javax.servlet.annotation.WebServlet;</p><p>import javax.servlet.http.HttpServlet;</p><p>import javax.servlet.http.HttpServletRequest;</p><p>import javax.servlet.http.HttpServletResponse;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>@WebServlet(name = "My Servlet", urlPatterns = { "/ms" })</p><p>public class MyServlet extends HttpServlet {</p><p>&#160;&#160;&#160; private static final long serialVersionUID = 1L;</p><p>&#160;&#160;&#160; </p><p>&#160;&#160;&#160; @Resource(mappedName = "java:/ConnectionFactory")</p><p>&#160;&#160;&#160; ConnectionFactory factory;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>&#160;&#160;&#160; private static final Logger log = Logger.getLogger("");</p><p>&#160;&#160;&#160; private static final String username = "guest";</p><p>&#160;&#160;&#160; private static final String password = "secret";</p><p>&#160;&#160;&#160; private static final String destination = "ExampleTopic";</p><p>&#160;&#160;&#160;&#160;&#160;&#160; </p><p>&#160;&#160;&#160; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; produce();</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; consume();</p><p>&#160;&#160;&#160; }</p><p>&#160;&#160;&#160; </p><p>&#160;&#160;&#160; private void produce() {</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; try {</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Connection conn = factory.createConnection(username, password);</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Session session = conn.createSession(false,</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Session.AUTO_ACKNOWLEDGE);</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; MessageProducer prod = session.createProducer(session</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; .createTopic(destination));</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; prod.setDeliveryMode(DeliveryMode.PERSISTENT);</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; conn.start();</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Message msg = session.createTextMessage("hello");</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; prod.send(msg);</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; conn.close();</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; } catch (JMSException e) {</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; log.info(e.getMessage());</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p>&#160;&#160;&#160; }</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>&#160;&#160;&#160; private void consume() {</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; try {</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Connection conn = factory.createConnection(username, password);</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Session session = conn.createSession(false,</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Session.AUTO_ACKNOWLEDGE);</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; MessageConsumer cons = session.createConsumer(session</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; .createTopic(destination));</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; conn.start();</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Message msg = cons.receive(1000);</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; if (msg == null) {</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; log.info("received no message");</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; } else {</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; TextMessage t = (TextMessage) msg;</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; log.info(t.getText());</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; conn.close();</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; } catch (JMSException e) {</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; e.printStackTrace();</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p>&#160;&#160;&#160; }</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>}</p></div>

<div style="background-color: #f4f4f4; padding: 10px; margin-top: 20px;">
    <p style="margin: 0;">Reply to this message by <a href="https://community.jboss.org/message/728702#728702">going to Community</a></p>
        <p style="margin: 0;">Start a new discussion in Beginner's Corner at <a href="https://community.jboss.org/choose-container!input.jspa?contentType=1&containerType=14&container=2075">Community</a></p>
</div></td>
                        </tr>
                    </tbody>
                </table>


                </td>
            </tr>
        </tbody>
    </table>

</div>

</body>
</html>