Search in sources :

Example 1 with QueueBrowser

use of javax.jms.QueueBrowser in project javaee7-samples by javaee-samples.

the class MessageReceiverSync method receiveAll.

public void receiveAll() {
    System.out.println("--> Receiving redundant messages ...");
    try {
        QueueBrowser browser = context.createBrowser(myQueue);
        while (browser.getEnumeration().hasMoreElements()) {
            System.out.println("--> here is one");
            context.createConsumer(myQueue).receiveBody(String.class, 1000);
        }
    } catch (JMSException ex) {
        Logger.getLogger(MessageReceiverSync.class.getName()).log(Level.SEVERE, null, ex);
    }
}
Also used : JMSException(javax.jms.JMSException) QueueBrowser(javax.jms.QueueBrowser)

Example 2 with QueueBrowser

use of javax.jms.QueueBrowser in project tomee by apache.

the class JMSContextImpl method createBrowser.

@Override
public QueueBrowser createBrowser(final Queue queue, final String messageSelector) {
    try {
        final QueueBrowser browser = session().createBrowser(queue, messageSelector);
        checkAutoStart();
        return browser;
    } catch (final JMSException e) {
        throw toRuntimeException(e);
    }
}
Also used : JMSException(javax.jms.JMSException) QueueBrowser(javax.jms.QueueBrowser)

Example 3 with QueueBrowser

use of javax.jms.QueueBrowser in project tomee by apache.

the class JMSContextImpl method createBrowser.

@Override
public QueueBrowser createBrowser(final Queue queue) {
    try {
        final QueueBrowser browser = session().createBrowser(queue);
        checkAutoStart();
        return browser;
    } catch (final JMSException e) {
        throw toRuntimeException(e);
    }
}
Also used : JMSException(javax.jms.JMSException) QueueBrowser(javax.jms.QueueBrowser)

Example 4 with QueueBrowser

use of javax.jms.QueueBrowser in project sling by apache.

the class JMSQueueManagerTest method emptyQueue.

private void emptyQueue(String name) throws JMSException {
    dumpQueue(name);
    Connection connection = amqConnectionFactoryService.getConnectionFactory().createConnection();
    connection.start();
    Session session = connection.createSession(true, Session.SESSION_TRANSACTED);
    Queue queue = session.createQueue(name);
    MessageConsumer consumer = session.createConsumer(queue);
    for (; ; ) {
        Message m = consumer.receive(100);
        if (m == null) {
            LOGGER.info("No more messages in queue {} ", name);
            break;
        }
        LOGGER.info("Got message  {}", m);
        m.acknowledge();
        session.commit();
    }
    boolean shouldFail = false;
    QueueBrowser browser = session.createBrowser(queue);
    for (Enumeration messages = browser.getEnumeration(); messages.hasMoreElements(); ) {
        Message m = (Message) messages.nextElement();
        LOGGER.info("Queued message {} ", m);
        shouldFail = true;
    }
    browser.close();
    if (shouldFail) {
        fail("Queue was not emptied as expected");
    }
    consumer.close();
    session.close();
    connection.stop();
}
Also used : MessageConsumer(javax.jms.MessageConsumer) Enumeration(java.util.Enumeration) Message(javax.jms.Message) Connection(javax.jms.Connection) Queue(javax.jms.Queue) QueueBrowser(javax.jms.QueueBrowser) Session(javax.jms.Session)

Example 5 with QueueBrowser

use of javax.jms.QueueBrowser in project sling by apache.

the class JMSQueueManagerTest method checkMessagesInQueue.

private void checkMessagesInQueue(String name, int expected) throws JMSException {
    Connection connection = amqConnectionFactoryService.getConnectionFactory().createConnection();
    connection.start();
    Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    Queue queue = session.createQueue(name);
    QueueBrowser browser = session.createBrowser(queue);
    int n = 0;
    for (Enumeration e = browser.getEnumeration(); e.hasMoreElements(); ) {
        Message m = (Message) e.nextElement();
        LOGGER.info("Message at {} is {} ", n, m);
        n++;
    }
    browser.close();
    session.close();
    connection.stop();
    assertEquals(expected, n);
}
Also used : Enumeration(java.util.Enumeration) Message(javax.jms.Message) Connection(javax.jms.Connection) Queue(javax.jms.Queue) QueueBrowser(javax.jms.QueueBrowser) Session(javax.jms.Session)

Aggregations

QueueBrowser (javax.jms.QueueBrowser)10 JMSException (javax.jms.JMSException)6 Enumeration (java.util.Enumeration)4 Connection (javax.jms.Connection)4 Message (javax.jms.Message)4 Session (javax.jms.Session)4 MessageConsumer (javax.jms.MessageConsumer)3 Queue (javax.jms.Queue)3 MessageProducer (javax.jms.MessageProducer)1 TextMessage (javax.jms.TextMessage)1