You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@activemq.apache.org by rrazey <ro...@dynacrongroup.com> on 2014/01/07 01:52:01 UTC

Can't consume messages from queue

I'm having an issue with consuming messages from a queue.  When I go to
consume it (consumeMessageFromQueue()), there are no messages found.  But if
I read the messages (readMessageFromQueue()), I see the message in the
queue.  What is going on here?

Test.java:
    @Test
    public void receiveMessage () {
        ActiveMQModule module = new ActiveMQModule(URL, QUEUE);
        String messageText = module.consumeMessageFromQueue();
        module.closeConnections();
        assertEquals(MESSAGE, messageText);
    }


ActiveMQModule.java:

package backup;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQQueue;

import javax.jms.*;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

public class ActiveMQModule {

    private static ActiveMQConnectionFactory connectionFactory;
    private Connection connection;
    private Session session;
    private Destination destination;
    private static final int TIMEOUT_MILLISECONDS = 5000;

    public ActiveMQModule(String url, String queueName) {
        try {
            connectionFactory = new ActiveMQConnectionFactory(url);
            connection = connectionFactory.createConnection();
            connection.start();
            session = connection.createSession(false,
Session.AUTO_ACKNOWLEDGE);
            destination = session.createQueue(queueName);
        } catch (Exception e) {
            handleException(e);
        }
    }


    public String consumeMessageFromQueue() {
        String messageText = null;
        try {
            MessageConsumer consumer = session.createConsumer(destination);
            TextMessage message =
(TextMessage)consumer.receive(TIMEOUT_MILLISECONDS);
            messageText = message.getText();
            consumer.close();
        } catch (Exception e) {
            handleException(e);
        }
        return messageText;
    }

    public String readMessageFromQueue() {
        String messageText = null;
        try {
            QueueBrowser browser =
session.createBrowser((Queue)destination);
            TextMessage message =
(TextMessage)browser.getEnumeration().nextElement();
            messageText = message.getText();
            browser.close();
        } catch (Exception e) {
            handleException(e);
        }
        return messageText;
    }

    private void handleException(Exception e) {
        System.out.println("Caught: " + e);
        e.printStackTrace();
    }
}






--
View this message in context: http://activemq.2283324.n4.nabble.com/Can-t-consume-messages-from-queue-tp4676065.html
Sent from the ActiveMQ - User mailing list archive at Nabble.com.

Re: Can't consume messages from queue

Posted by Christian Posta <ch...@gmail.com>.
nothing stands out immediately.. maybe you can complete the test case
by showing where the producer sends to the queue so that we know there
is indeed a message in the queue...

On Mon, Jan 6, 2014 at 4:52 PM, rrazey <ro...@dynacrongroup.com> wrote:
> I'm having an issue with consuming messages from a queue.  When I go to
> consume it (consumeMessageFromQueue()), there are no messages found.  But if
> I read the messages (readMessageFromQueue()), I see the message in the
> queue.  What is going on here?
>
> Test.java:
>     @Test
>     public void receiveMessage () {
>         ActiveMQModule module = new ActiveMQModule(URL, QUEUE);
>         String messageText = module.consumeMessageFromQueue();
>         module.closeConnections();
>         assertEquals(MESSAGE, messageText);
>     }
>
>
> ActiveMQModule.java:
>
> package backup;
>
> import org.apache.activemq.ActiveMQConnectionFactory;
> import org.apache.activemq.command.ActiveMQQueue;
>
> import javax.jms.*;
> import java.util.ArrayList;
> import java.util.Enumeration;
> import java.util.List;
>
> public class ActiveMQModule {
>
>     private static ActiveMQConnectionFactory connectionFactory;
>     private Connection connection;
>     private Session session;
>     private Destination destination;
>     private static final int TIMEOUT_MILLISECONDS = 5000;
>
>     public ActiveMQModule(String url, String queueName) {
>         try {
>             connectionFactory = new ActiveMQConnectionFactory(url);
>             connection = connectionFactory.createConnection();
>             connection.start();
>             session = connection.createSession(false,
> Session.AUTO_ACKNOWLEDGE);
>             destination = session.createQueue(queueName);
>         } catch (Exception e) {
>             handleException(e);
>         }
>     }
>
>
>     public String consumeMessageFromQueue() {
>         String messageText = null;
>         try {
>             MessageConsumer consumer = session.createConsumer(destination);
>             TextMessage message =
> (TextMessage)consumer.receive(TIMEOUT_MILLISECONDS);
>             messageText = message.getText();
>             consumer.close();
>         } catch (Exception e) {
>             handleException(e);
>         }
>         return messageText;
>     }
>
>     public String readMessageFromQueue() {
>         String messageText = null;
>         try {
>             QueueBrowser browser =
> session.createBrowser((Queue)destination);
>             TextMessage message =
> (TextMessage)browser.getEnumeration().nextElement();
>             messageText = message.getText();
>             browser.close();
>         } catch (Exception e) {
>             handleException(e);
>         }
>         return messageText;
>     }
>
>     private void handleException(Exception e) {
>         System.out.println("Caught: " + e);
>         e.printStackTrace();
>     }
> }
>
>
>
>
>
>
> --
> View this message in context: http://activemq.2283324.n4.nabble.com/Can-t-consume-messages-from-queue-tp4676065.html
> Sent from the ActiveMQ - User mailing list archive at Nabble.com.



-- 
Christian Posta
http://www.christianposta.com/blog
twitter: @christianposta