activemq-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Christian Posta <christian.po...@gmail.com>
Subject Re: trying to consume (remove) a message
Date Tue, 16 Jul 2013 19:51:11 GMT
This unit test works:


    @Test
    public void remove() throws JMSException, InterruptedException {

        boolean messageConsumed = false;

        Connection connection = null;
        Session session = null;
        try {
            ConnectionFactory connectionFactory = new
ActiveMQConnectionFactory("vm://localhost?broker.persistent=false");
            connection = connectionFactory.createConnection();
            connection.start();

            session = connection.createSession(false,
Session.AUTO_ACKNOWLEDGE);
            Queue queue = session.createQueue("FOO");
            MessageProducer producer = session.createProducer(queue);
            for (int i = 0; i < 5; i++) {
                producer.send(session.createTextMessage("Hi" + i));
            }


            QueueBrowser browser = session.createBrowser(queue);
            Enumeration<?> messagesInQueue = browser.getEnumeration();
            String jmsMessageID = "nan";
            while (messagesInQueue.hasMoreElements()) {
                Message queueMessage = (Message)
messagesInQueue.nextElement();
                System.out.println("Enqueued > " +
queueMessage.getJMSMessageID());
                jmsMessageID = queueMessage.getJMSMessageID();

            }

            System.out.println("Trying to delete > " + jmsMessageID);


            MessageConsumer consumer =
                    session.createConsumer(queue, "JMSMessageID='" +
jmsMessageID + "'");
            Message message = null;
            while ((message = consumer.receive(500)) != null) {
                System.out.println("Consumed > " + message);
            }

        } finally {
            if (session != null)
                session.close();
            if (connection != null)
                connection.close();
        }

    }


On Tue, Jul 16, 2013 at 3:41 PM, Leonardo K. Shikida <shikida@gmail.com>wrote:

> Hi
>
> I've tried
>
> consumer.receive(1000)
>
> without success
>
> []
>
> Leo
>
>
> On Tue, Jul 16, 2013 at 4:25 PM, Christian Posta
> <christian.posta@gmail.com> wrote:
> > Try giving a brief moment between creating your consumers and the call to
> > receiveNoWait():
> >
> >             MessageConsumer consumer =
> >                     session.createConsumer(queue, "JMSMessageID='" +
> > jmsMessageID + "'");
> >             Message message = null;
> >             Thread.sleep(500);
> >             while ((message = consumer.receiveNoWait()) != null) {
> >                 System.out.println("Consumed > " + message);
> >             }
> >
> > Alternatively, use consumer.receive(500)
> >
> >
> > On Tue, Jul 16, 2013 at 11:10 AM, Leonardo K. Shikida <shikida@gmail.com
> >wrote:
> >
> >> Hi
> >>
> >> I am trying to remove (consume) a message from the queue, without
> success.
> >>
> >> Here's my code and the output
> >>
> >> public boolean remove(String jmsMessageID) throws JMSException{
> >>
> >> boolean messageConsumed = false;
> >>
> >> Connection connection = null;
> >> Session session = null;
> >> try {
> >> ConnectionFactory connectionFactory =
> >> this.baseService.getConnectionFactory();
> >> connection = connectionFactory.createConnection();
> >> connection.start();
> >>
> >> // Create a Session
> >> session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
> >>
> >> QueueBrowser browser =
> session.createBrowser(this.baseService.getQueue());
> >>    Enumeration<?> messagesInQueue = browser.getEnumeration();
> >>
> >>    while (messagesInQueue.hasMoreElements()) {
> >>     Message queueMessage = (Message) messagesInQueue.nextElement();
> >>     System.out.println("Enqueued > "+queueMessage.getJMSMessageID());
> >>    }
> >>
> >>    System.out.println("Trying to delete > "+jmsMessageID);
> >>
> >> Queue queue = this.baseService.getQueue();
> >> MessageConsumer consumer =
> >> session.createConsumer(queue,"JMSMessageID='"+jmsMessageID+"'");
> >> Message message = null;
> >> while((message = consumer.receiveNoWait()) != null){
> >> System.out.println("Consumed > "+message);
> >> messageConsumed = true;
> >> }
> >>
> >> } finally {
> >> if (session != null)
> >> session.close();
> >> if (connection != null)
> >> connection.close();
> >> }
> >>
> >> return messageConsumed;
> >> }
> >>
> >> >>>>>>>
> >>
> >> Enqueued          >
> >> ID:7612866413.mydomain.com-58510-1373986917055-5:1:35:1:1
> >> Enqueued          >
> >> ID:7612866413.mydomain.com-58510-1373986917055-5:1:61:1:1
> >> Enqueued          >
> >> ID:7612866413.mydomain.com-58510-1373986917055-5:1:63:1:1
> >> Trying to delete  >
> >> ID:7612866413.mydomain.com-58510-1373986917055-5:1:63:1:1
> >>
> >> (never enter into System.out.println("Consumed > "+message);)
> >>
> >> Am I missing something here?
> >>
> >> []
> >>
> >> Leo
> >>
> >
> >
> >
> > --
> > *Christian Posta*
> > http://www.christianposta.com/blog
> > twitter: @christianposta
>



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

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message