[jboss-user] [Beginner's Corner] - AS 7.1.1 send/receive a JMS message in Servlet question

Michael Justin do-not-reply at jboss.com
Fri Apr 6 13:06:23 EDT 2012

Michael Justin [https://community.jboss.org/people/mjustin] created the discussion

"AS 7.1.1 send/receive a JMS message in Servlet question"

To view the discussion, visit: https://community.jboss.org/message/728702#728702


I am trying to build a simple JMS produce / consume example in a Servlet. The full source code is below.

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.

package servlet;

import java.io.IOException;
import java.io.Writer;
import java.util.logging.Logger;

import javax.annotation.Resource;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.DeliveryMode;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet(name = "My Servlet", urlPatterns = { "/ms" })
public class MyServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    @Resource(mappedName = "java:/ConnectionFactory")
    ConnectionFactory factory;

    private static final Logger log = Logger.getLogger("");
    private static final String username = "guest";
    private static final String password = "secret";
    private static final String destination = "ExampleTopic";

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    private void produce() {
        try {
            Connection conn = factory.createConnection(username, password);
            Session session = conn.createSession(false,
            MessageProducer prod = session.createProducer(session
            Message msg = session.createTextMessage("hello");
        } catch (JMSException e) {

    private void consume() {
        try {
            Connection conn = factory.createConnection(username, password);
            Session session = conn.createSession(false,
            MessageConsumer cons = session.createConsumer(session
            Message msg = cons.receive(1000);
            if (msg == null) {
                log.info("received no message");
            } else {
                TextMessage t = (TextMessage) msg;
        } catch (JMSException e) {


Reply to this message by going to Community

Start a new discussion in Beginner's Corner at Community

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/jboss-user/attachments/20120406/b1af0449/attachment.html 

More information about the jboss-user mailing list