Example usage for javax.jms QueueBrowser close

List of usage examples for javax.jms QueueBrowser close

Introduction

In this page you can find the example usage for javax.jms QueueBrowser close.

Prototype


void close() throws JMSException;

Source Link

Document

Closes the QueueBrowser .

Usage

From source file:org.apache.activemq.apollo.JmsQueueBrowserTest.java

public void testBrowseClose() throws Exception {
    Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    ActiveMQQueue destination = new ActiveMQQueue("TEST");

    connection.start();/*  w ww . j a va 2  s .c  om*/

    TextMessage[] outbound = new TextMessage[] { session.createTextMessage("First Message"),
            session.createTextMessage("Second Message"), session.createTextMessage("Third Message") };

    MessageProducer producer = session.createProducer(destination);
    producer.send(outbound[0]);
    producer.send(outbound[1]);
    producer.send(outbound[2]);

    // create browser first
    QueueBrowser browser = session.createBrowser((Queue) destination);
    Enumeration enumeration = browser.getEnumeration();

    // browse some messages
    assertEquals(outbound[0], (Message) enumeration.nextElement());
    assertEquals(outbound[1], (Message) enumeration.nextElement());
    //assertEquals(outbound[2], (Message) enumeration.nextElement());

    browser.close();

    // create consumer
    MessageConsumer consumer = session.createConsumer(destination);

    // Receive the first message.
    TextMessage msg = (TextMessage) consumer.receive(1000);
    assertEquals("Expected " + outbound[0].getText() + " but received " + msg.getText(), outbound[0], msg);
    msg = (TextMessage) consumer.receive(1000);
    assertEquals("Expected " + outbound[1].getText() + " but received " + msg.getText(), outbound[1], msg);
    msg = (TextMessage) consumer.receive(1000);
    assertEquals("Expected " + outbound[2].getText() + " but received " + msg.getText(), outbound[2], msg);

    consumer.close();
    producer.close();

}

From source file:org.apache.activemq.jms.pool.PooledSession.java

@Override
public void close() throws JMSException {
    if (ignoreClose) {
        return;// www.j  a  v a 2 s.  c om
    }

    if (closed.compareAndSet(false, true)) {
        boolean invalidate = false;
        try {
            // lets reset the session
            getInternalSession().setMessageListener(null);

            // Close any consumers and browsers that may have been created.
            for (Iterator<MessageConsumer> iter = consumers.iterator(); iter.hasNext();) {
                MessageConsumer consumer = iter.next();
                consumer.close();
            }

            for (Iterator<QueueBrowser> iter = browsers.iterator(); iter.hasNext();) {
                QueueBrowser browser = iter.next();
                browser.close();
            }

            if (transactional && !isXa) {
                try {
                    getInternalSession().rollback();
                } catch (JMSException e) {
                    invalidate = true;
                    LOG.warn(
                            "Caught exception trying rollback() when putting session back into the pool, will invalidate. "
                                    + e,
                            e);
                }
            }
        } catch (JMSException ex) {
            invalidate = true;
            LOG.warn(
                    "Caught exception trying close() when putting session back into the pool, will invalidate. "
                            + ex,
                    ex);
        } finally {
            consumers.clear();
            browsers.clear();
            for (PooledSessionEventListener listener : this.sessionEventListeners) {
                listener.onSessionClosed(this);
            }
            sessionEventListeners.clear();
        }

        if (invalidate) {
            // lets close the session and not put the session back into the pool
            // instead invalidate it so the pool can create a new one on demand.
            if (sessionHolder != null) {
                try {
                    sessionHolder.close();
                } catch (JMSException e1) {
                    LOG.trace("Ignoring exception on close as discarding session: " + e1, e1);
                }
            }
            try {
                sessionPool.invalidateObject(key, sessionHolder);
            } catch (Exception e) {
                LOG.trace("Ignoring exception on invalidateObject as discarding session: " + e, e);
            }
        } else {
            try {
                sessionPool.returnObject(key, sessionHolder);
            } catch (Exception e) {
                javax.jms.IllegalStateException illegalStateException = new javax.jms.IllegalStateException(
                        e.toString());
                illegalStateException.initCause(e);
                throw illegalStateException;
            }
        }

        sessionHolder = null;
    }
}

From source file:org.apache.aries.transaction.jms.internal.PooledSession.java

@Override
public void close() throws JMSException {
    if (!ignoreClose) {
        boolean invalidate = false;
        try {// ww  w .ja  v a2s  . c  o  m
            // lets reset the session
            getInternalSession().setMessageListener(null);

            // Close any consumers and browsers that may have been created.
            for (Iterator<MessageConsumer> iter = consumers.iterator(); iter.hasNext();) {
                MessageConsumer consumer = iter.next();
                consumer.close();
            }

            for (Iterator<QueueBrowser> iter = browsers.iterator(); iter.hasNext();) {
                QueueBrowser browser = iter.next();
                browser.close();
            }

            if (transactional && !isXa) {
                try {
                    getInternalSession().rollback();
                } catch (JMSException e) {
                    invalidate = true;
                    LOG.warn(
                            "Caught exception trying rollback() when putting session back into the pool, will invalidate. "
                                    + e,
                            e);
                }
            }
        } catch (JMSException ex) {
            invalidate = true;
            LOG.warn(
                    "Caught exception trying close() when putting session back into the pool, will invalidate. "
                            + ex,
                    ex);
        } finally {
            consumers.clear();
            browsers.clear();
            for (PooledSessionEventListener listener : this.sessionEventListeners) {
                listener.onSessionClosed(this);
            }
            sessionEventListeners.clear();
        }

        if (invalidate) {
            // lets close the session and not put the session back into the pool
            // instead invalidate it so the pool can create a new one on demand.
            if (session != null) {
                try {
                    session.close();
                } catch (JMSException e1) {
                    LOG.trace("Ignoring exception on close as discarding session: " + e1, e1);
                }
                session = null;
            }
            try {
                sessionPool.invalidateObject(key, this);
            } catch (Exception e) {
                LOG.trace("Ignoring exception on invalidateObject as discarding session: " + e, e);
            }
        } else {
            try {
                sessionPool.returnObject(key, this);
            } catch (Exception e) {
                javax.jms.IllegalStateException illegalStateException = new javax.jms.IllegalStateException(
                        e.toString());
                illegalStateException.initCause(e);
                throw illegalStateException;
            }
        }
    }
}

From source file:org.apache.james.queue.jms.JMSMailQueue.java

private static void closeBrowser(QueueBrowser browser) {
    if (browser != null) {
        try {//from   w  w w  .  j av a  2  s .c om
            browser.close();
        } catch (JMSException e) {
            // Ignore. See JAMES-2509
        }
    }
}

From source file:org.apache.karaf.jms.pool.internal.PooledSession.java

@Override
public void close() throws JMSException {
    if (!ignoreClose) {
        boolean invalidate = false;
        try {// www. j av  a2 s.c om
            // lets reset the session
            getInternalSession().setMessageListener(null);

            // Close any consumers and browsers that may have been created.
            for (MessageConsumer consumer : consumers) {
                consumer.close();
            }

            for (QueueBrowser browser : browsers) {
                browser.close();
            }

            if (transactional && !isXa) {
                try {
                    getInternalSession().rollback();
                } catch (JMSException e) {
                    invalidate = true;
                    LOG.warn(
                            "Caught exception trying rollback() when putting session back into the pool, will invalidate. "
                                    + e,
                            e);
                }
            }
        } catch (JMSException ex) {
            invalidate = true;
            LOG.warn(
                    "Caught exception trying close() when putting session back into the pool, will invalidate. "
                            + ex,
                    ex);
        } finally {
            consumers.clear();
            browsers.clear();
            for (PooledSessionEventListener listener : this.sessionEventListeners) {
                listener.onSessionClosed(this);
            }
            sessionEventListeners.clear();
        }

        if (invalidate) {
            // lets close the session and not put the session back into the pool
            // instead invalidate it so the pool can create a new one on demand.
            if (session != null) {
                try {
                    session.close();
                } catch (JMSException e1) {
                    LOG.trace("Ignoring exception on close as discarding session: " + e1, e1);
                }
                session = null;
            }
            try {
                sessionPool.invalidateObject(key, this);
            } catch (Exception e) {
                LOG.trace("Ignoring exception on invalidateObject as discarding session: " + e, e);
            }
        } else {
            try {
                sessionPool.returnObject(key, this);
            } catch (Exception e) {
                javax.jms.IllegalStateException illegalStateException = new javax.jms.IllegalStateException(
                        e.toString());
                illegalStateException.initCause(e);
                throw illegalStateException;
            }
        }
    }
}

From source file:org.dhatim.routing.jms.JMSRouter.java

private void waitWhileAboveHighWaterMark() throws JMSException, SmooksRoutingException {
    if (highWaterMark == -1) {
        return;/*w  w w  . jav  a 2s.  com*/
    }

    if (session instanceof QueueSession) {
        QueueSession queueSession = (QueueSession) session;
        QueueBrowser queueBrowser = queueSession.createBrowser((Queue) destination);

        try {
            int length = getQueueLength(queueBrowser);
            long start = System.currentTimeMillis();

            if (logger.isDebugEnabled() && length >= highWaterMark) {
                logger.debug("Length of JMS destination Queue '" + jmsProperties.getDestinationName()
                        + "' has reached " + length + ".  High Water Mark is " + highWaterMark
                        + ".  Waiting for Queue length to drop.");
            }

            while (length >= highWaterMark && (System.currentTimeMillis() < start + highWaterMarkTimeout)) {
                try {
                    Thread.sleep(highWaterMarkPollFrequency);
                } catch (InterruptedException e) {
                    logger.error("Interrupted", e);
                    return;
                }
                length = getQueueLength(queueBrowser);
            }

            // Check did the queue length drop below the HWM...
            if (length >= highWaterMark) {
                throw new SmooksRoutingException("Failed to route JMS message to Queue destination '"
                        + ((Queue) destination).getQueueName() + "'. Timed out (" + highWaterMarkTimeout
                        + " ms) waiting for queue length to drop below High Water Mark (" + highWaterMark
                        + ").  Consider increasing 'highWaterMark' and/or 'highWaterMarkTimeout' param values.");
            }
        } finally {
            queueBrowser.close();
        }
    }
}

From source file:org.logicblaze.lingo.jms.JmsClient.java

public Message peek() throws JMSException {
    QueueBrowser browser = createBrowser();
    try {/* w  w w  . ja  v  a 2s.co  m*/
        Enumeration iter = browser.getEnumeration();
        if (iter.hasMoreElements()) {
            return (Message) iter.nextElement();
        }
        return null;
    } finally {
        try {
            browser.close();
        } catch (JMSException e) {
            onBrowserCloseException(e);
            return null;
        }
    }
}

From source file:org.logicblaze.lingo.jms.JmsClient.java

public void close(QueueBrowser browser) {
    try {/* www.  j  a  v a  2  s  . c  o m*/
        browser.close();
    } catch (JMSException e) {
        // ignore exception as we're called from iterators
        log.warn("Could not close queue browser due to: " + e, e);
    }
}

From source file:org.springframework.jms.support.JmsUtils.java

/**
 * Close the given JMS QueueBrowser and ignore any thrown exception.
 * This is useful for typical {@code finally} blocks in manual JMS code.
 * @param browser the JMS QueueBrowser to close (may be {@code null})
 *//*from ww w .j ava2  s.co  m*/
public static void closeQueueBrowser(@Nullable QueueBrowser browser) {
    if (browser != null) {
        try {
            browser.close();
        } catch (JMSException ex) {
            logger.trace("Could not close JMS QueueBrowser", ex);
        } catch (Throwable ex) {
            // We don't trust the JMS provider: It might throw RuntimeException or Error.
            logger.trace("Unexpected exception on closing JMS QueueBrowser", ex);
        }
    }
}

From source file:org.symplify.runtime.jms.JMSContextImpl.java

public Message peek(Channel channel) throws Exception {
    Message ret = null;/*w  ww.  j ava  2  s  .c  om*/
    javax.jms.Queue queue = getQueue(channel);

    if (queue != null) {
        javax.jms.QueueBrowser browser = m_session.createBrowser(queue);

        if (browser.getEnumeration().hasMoreElements()) {
            javax.jms.Message m = (javax.jms.Message) browser.getEnumeration().nextElement();

            if (m instanceof javax.jms.ObjectMessage) {
                ret = (Message) ((javax.jms.ObjectMessage) m).getObject();
            }
        }

        browser.close();

    } else {
        throw new Exception("Unable to find queue for channel");
    }

    if (logger.isDebugEnabled()) {
        logger.debug("Peek on channel " + channel + " = " + ret);
    }

    return (ret);
}