activemq-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Guangnuan.Liu" <guangnuan....@gmail.com>
Subject A bug found in souce code of activemq web demo
Date Thu, 07 Sep 2006 03:48:45 GMT

The web demo is deployed together with Tomcat 5.0. The Send a messager
function in the page http://localhost:8080/activemq-web-demo-4.0.2/  is
well, but the Receive a message function consumes two messages a time from a
queue. I had been troubled by this strange behavior of
activemq-web-demo-4.0.2 for several days. As I am new to Java, xml, and
activemq, my colleague helped me in finding the bug in source code in

trunk\activemq-web\src\main\java\org\apache\activemq\web\MessageServlet.java.

Let's have a look at doMessages method in MessageServlet.java. There is an
excessive message = consumer.receiveNoWait(); operation in the while cycle.
It just consumes a message and does nothing when invoked by doGet. I comment
out this line then the code works fine.


    /**
     * Reads a message from a destination up to some specific timeout period
     * 
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    protected void doMessages(HttpServletRequest request,
HttpServletResponse response, int maxMessages) throws ServletException,
IOException {

        int messages = 0;
        try {
            WebClient client = WebClient.getWebClient(request);
            Destination destination = getDestination(client, request);
            if (destination==null)
                throw new NoDestinationSuppliedException();
            long timeout = getReadTimeout(request);
            boolean ajax = isRicoAjax(request);
            if (!ajax)
                maxMessages = 1;

            if (log.isDebugEnabled()) {
                log.debug("Receiving message(s) from: " + destination + "
with timeout: " + timeout);
            }

            MessageAvailableConsumer consumer = (MessageAvailableConsumer)
client.getConsumer(destination);
            Continuation continuation = null;
            Listener listener = null;
            Message message = null;

            synchronized (consumer) {
                // Fetch the listeners
                listener = (Listener) consumer.getAvailableListener();
                if (listener == null) {
                    listener = new Listener(consumer);
                    consumer.setAvailableListener(listener);
                }
                // Look for any available messages
                message = consumer.receiveNoWait();

                // Get an existing Continuation or create a new one if there
are
                // no events.
                if (message == null) {
                    continuation =
ContinuationSupport.getContinuation(request, consumer);

                    // register this continuation with our listener.
                    listener.setContinuation(continuation);

                    // Get the continuation object (may wait and/or retry
                    // request here).
                    continuation.suspend(timeout);
                }

                // Try again now
                if (message == null)
                    message = consumer.receiveNoWait();

                // write a responds
                response.setContentType("text/xml");
                PrintWriter writer = response.getWriter();

                if (ajax)
                    writer.println("<ajax-response>");

                // handle any message(s)
                if (message == null) {
                    // No messages so OK response of for ajax else no
content.
                    response.setStatus(ajax ? HttpServletResponse.SC_OK :
HttpServletResponse.SC_NO_CONTENT);
                }
                else {
                    // We have at least one message so set up the response
                    response.setStatus(HttpServletResponse.SC_OK);
                    String type = getContentType(request);
                    if (type != null)
                        response.setContentType(type);

                    // send a response for each available message (up to max
                    // messages)
                    while ((maxMessages < 0 || messages < maxMessages) &&
message != null) {
                        //
System.err.println("message["+messages+"]="+message);
                        if (ajax) {
                            writer.print("<response type='object' id='");
                            writer.print(request.getParameter("id"));
                            writer.println("'>");
                        }
                        else
                            // only ever 1 message for non ajax!
                            setResponseHeaders(response, message);

                        writeMessageResponse(writer, message);

                        if (ajax)
                            writer.println("</response>");

                        // look for next message
                        message = consumer.receiveNoWait();   //This line
should be commented out.
                        messages++;
                    }
                }

                if (ajax) {
                    writer.println("<response type='object'
id='poll'><ok/></response>");
                    writer.println("</ajax-response>");
                }
            }
        }
        catch (JMSException e) {
            throw new ServletException("Could not post JMS message: " + e,
e);
        }
        finally {
            if (log.isDebugEnabled()) {
                log.debug("Received " + messages + " message(s)");
            }
        }
    }
-- 
View this message in context: http://www.nabble.com/A-bug-found-in-souce-code-of-activemq-web-demo-tf2230955.html#a6183760
Sent from the ActiveMQ - User forum at Nabble.com.


Mime
View raw message