<!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;"> </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;"> </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;"> </p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>package servlet;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </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;"> </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;"> </p><p>@WebServlet(name = "My Servlet", urlPatterns = { "/ms" })</p><p>public class MyServlet extends HttpServlet {</p><p>    private static final long serialVersionUID = 1L;</p><p>    </p><p>    @Resource(mappedName = "java:/ConnectionFactory")</p><p>    ConnectionFactory factory;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>    private static final Logger log = Logger.getLogger("");</p><p>    private static final String username = "guest";</p><p>    private static final String password = "secret";</p><p>    private static final String destination = "ExampleTopic";</p><p>       </p><p>    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {</p><p>        produce();</p><p>        consume();</p><p>    }</p><p>    </p><p>    private void produce() {</p><p>        try {</p><p>            Connection conn = factory.createConnection(username, password);</p><p>            Session session = conn.createSession(false,</p><p>                    Session.AUTO_ACKNOWLEDGE);</p><p>            MessageProducer prod = session.createProducer(session</p><p>                    .createTopic(destination));</p><p>            prod.setDeliveryMode(DeliveryMode.PERSISTENT);</p><p>            conn.start();</p><p>            Message msg = session.createTextMessage("hello");</p><p>            prod.send(msg);</p><p>            conn.close();</p><p>        } catch (JMSException e) {</p><p>            log.info(e.getMessage());</p><p>        }</p><p>    }</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>    private void consume() {</p><p>        try {</p><p>            Connection conn = factory.createConnection(username, password);</p><p>            Session session = conn.createSession(false,</p><p>                    Session.AUTO_ACKNOWLEDGE);</p><p>            MessageConsumer cons = session.createConsumer(session</p><p>                    .createTopic(destination));</p><p>            conn.start();</p><p>            Message msg = cons.receive(1000);</p><p>            if (msg == null) {</p><p>                log.info("received no message");</p><p>            } else {</p><p>                TextMessage t = (TextMessage) msg;</p><p>                log.info(t.getText());</p><p>            }</p><p>            conn.close();</p><p>        } catch (JMSException e) {</p><p>            e.printStackTrace();</p><p>        }</p><p>    }</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </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>