JBoss Community

AS 7.1.1 send/receive a JMS message in Servlet question

created by Michael Justin in Beginner's Corner - View the full discussion

Hello,

 

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 {

        produce();

        consume();

    }

   

    private void produce() {

        try {

            Connection conn = factory.createConnection(username, password);

            Session session = conn.createSession(false,

                    Session.AUTO_ACKNOWLEDGE);

            MessageProducer prod = session.createProducer(session

                    .createTopic(destination));

            prod.setDeliveryMode(DeliveryMode.PERSISTENT);

            conn.start();

            Message msg = session.createTextMessage("hello");

            prod.send(msg);

            conn.close();

        } catch (JMSException e) {

            log.info(e.getMessage());

        }

    }

 

    private void consume() {

        try {

            Connection conn = factory.createConnection(username, password);

            Session session = conn.createSession(false,

                    Session.AUTO_ACKNOWLEDGE);

            MessageConsumer cons = session.createConsumer(session

                    .createTopic(destination));

            conn.start();

            Message msg = cons.receive(1000);

            if (msg == null) {

                log.info("received no message");

            } else {

                TextMessage t = (TextMessage) msg;

                log.info(t.getText());

            }

            conn.close();

        } catch (JMSException e) {

            e.printStackTrace();

        }

    }

 

}

Reply to this message by going to Community

Start a new discussion in Beginner's Corner at Community