Example usage for javax.jms MessageConsumer close

List of usage examples for javax.jms MessageConsumer close

Introduction

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

Prototype


void close() throws JMSException;

Source Link

Document

Closes the message consumer.

Usage

From source file:org.apache.activemq.store.jdbc.JmsTransactionCommitFailureTest.java

private Message receiveMessage(String queueName, long receiveTimeout) throws JMSException {
    Message message = null;//  w ww.  ja va2  s. c o  m
    Connection con = connectionFactory.createConnection();
    try {
        con.start();
        try {
            Session session = con.createSession(true, Session.SESSION_TRANSACTED);
            try {
                Queue destination = session.createQueue(queueName);
                MessageConsumer consumer = session.createConsumer(destination);
                try {
                    message = consumer.receive(receiveTimeout);
                    session.commit();
                } finally {
                    consumer.close();
                }
            } finally {
                session.close();
            }
        } finally {
            con.stop();
        }
    } finally {
        con.close();
    }
    return message;
}

From source file:org.apache.activemq.store.kahadb.SubscriptionRecoveryTest.java

private void createInactiveDurableSub(Topic topic) throws Exception {
    Connection connection = cf.createConnection();
    connection.setClientID("Inactive");
    Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    MessageConsumer consumer = session.createDurableSubscriber(topic, "Inactive");
    consumer.close();
    connection.close();/*from   ww  w . ja  v a 2  s  .  co m*/
}

From source file:org.apache.activemq.store.kahadb.SubscriptionRecoveryTest.java

private void consumeDurableMessages(Topic topic, int count) throws Exception {
    Connection connection = cf.createConnection();
    connection.setClientID("Inactive");
    Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    MessageConsumer consumer = session.createDurableSubscriber(topic, "Inactive");
    connection.start();/* w  w w.j a  v  a2 s .  c om*/
    for (int i = 0; i < count; ++i) {
        if (consumer.receive(TimeUnit.SECONDS.toMillis(10)) == null) {
            fail("should have received a message");
        }
    }
    consumer.close();
    connection.close();
}

From source file:org.apache.activemq.store.kahadb.SubscriptionRecoveryTest.java

private void tryConsumeExpectNone(Topic topic) throws Exception {
    Connection connection = cf.createConnection();
    connection.setClientID("Inactive");
    Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    MessageConsumer consumer = session.createDurableSubscriber(topic, "Inactive");
    connection.start();//www .j ava2 s.c o  m
    if (consumer.receive(TimeUnit.SECONDS.toMillis(10)) != null) {
        fail("Should be no messages for this durable.");
    }
    consumer.close();
    connection.close();
}

From source file:org.apache.activemq.store.kahadb.SubscriptionRecoveryTest.java

private int consumeFromInactiveDurableSub(Topic topic) throws Exception {
    Connection connection = cf.createConnection();
    connection.setClientID("Inactive");
    connection.start();/*from w  ww .j  a  v  a  2 s.  com*/
    Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    MessageConsumer consumer = session.createDurableSubscriber(topic, "Inactive");

    int count = 0;

    while (consumer.receive(10000) != null) {
        count++;
    }

    consumer.close();
    connection.close();

    return count;
}

From source file:org.apache.activemq.usecases.DurableSubscriberWithNetworkRestartTest.java

public void testSendOnAReceiveOnBWithTransportDisconnect() throws Exception {
    bridge(SPOKE, HUB);// w  ww.  j  av a2s. com
    startAllBrokers();

    verifyDuplexBridgeMbean();

    // Setup connection
    URI hubURI = brokers.get(HUB).broker.getTransportConnectors().get(0).getPublishableConnectURI();
    URI spokeURI = brokers.get(SPOKE).broker.getTransportConnectors().get(0).getPublishableConnectURI();
    ActiveMQConnectionFactory facHub = new ActiveMQConnectionFactory(hubURI);
    ActiveMQConnectionFactory facSpoke = new ActiveMQConnectionFactory(spokeURI);
    Connection conHub = facHub.createConnection();
    Connection conSpoke = facSpoke.createConnection();
    conHub.setClientID("clientHUB");
    conSpoke.setClientID("clientSPOKE");
    conHub.start();
    conSpoke.start();
    Session sesHub = conHub.createSession(false, Session.AUTO_ACKNOWLEDGE);
    Session sesSpoke = conSpoke.createSession(false, Session.AUTO_ACKNOWLEDGE);

    ActiveMQTopic topic = new ActiveMQTopic("TEST.FOO");
    String consumerName = "consumerName";

    // Setup consumers
    MessageConsumer remoteConsumer = sesHub.createDurableSubscriber(topic, consumerName);
    sleep(1000);
    remoteConsumer.close();

    // Setup producer
    MessageProducer localProducer = sesSpoke.createProducer(topic);
    localProducer.setDeliveryMode(DeliveryMode.PERSISTENT);

    final String payloadString = new String(new byte[10 * 1024]);
    // Send messages
    for (int i = 0; i < MESSAGE_COUNT; i++) {
        Message test = sesSpoke.createTextMessage("test-" + i);
        test.setStringProperty("payload", payloadString);
        localProducer.send(test);
    }
    localProducer.close();

    final String options = "?persistent=true&useJmx=true&deleteAllMessagesOnStartup=false";
    for (int i = 0; i < 2; i++) {
        brokers.get(SPOKE).broker.stop();
        sleep(1000);
        createBroker(new URI("broker:(tcp://localhost:61616)/" + SPOKE + options));
        bridge(SPOKE, HUB);
        brokers.get(SPOKE).broker.start();
        LOG.info("restarted spoke..:" + i);

        assertTrue("got mbeans on restart", Wait.waitFor(new Wait.Condition() {
            @Override
            public boolean isSatisified() throws Exception {
                return countMbeans(brokers.get(HUB).broker, "networkBridge", 20000) == (dynamicOnly ? 1 : 2);
            }
        }));
    }
}

From source file:org.apache.activemq.usecases.RequestReplyToTopicViaThreeNetworkHopsTest.java

public void testMessages(Session sess, MessageProducer req_prod, Destination resp_dest, int num_msg)
        throws Exception {
    MessageConsumer resp_cons;
    TextMessage msg;/*  ww  w .  jav  a  2s.  c o m*/
    MessageClient cons_client;
    int cur;
    int tot_expected;

    resp_cons = sess.createConsumer(resp_dest);

    cons_client = new MessageClient(resp_cons, num_msg);
    cons_client.start();

    cur = 0;
    while ((cur < num_msg) && (!fatalTestError)) {
        msg = sess.createTextMessage("MSG AAAA " + cur);
        msg.setIntProperty("SEQ", 100 + cur);
        msg.setStringProperty("TEST", "TOPO");
        msg.setJMSReplyTo(resp_dest);

        if (cur == (num_msg - 1))
            msg.setBooleanProperty("end-of-response", true);

        sendWithRetryOnDeletedDest(req_prod, msg);
        LOG.debug("Sent:" + msg);

        cur++;
    }

    //
    // Give the consumer some time to receive the response.
    //
    cons_client.waitShutdown(5000);

    //
    // Now shutdown the consumer if it's still running.
    //
    if (cons_client.shutdown())
        LOG.debug("Consumer client shutdown complete");
    else
        LOG.debug("Consumer client shutdown incomplete!!!");

    //
    // Check that the correct number of messages was received.
    //
    tot_expected = num_msg * (echoResponseFill + 1);

    if (cons_client.getNumMsgReceived() == tot_expected) {
        LOG.debug("Have " + tot_expected + " messages, as-expected");
    } else {
        testError = true;

        if (cons_client.getNumMsgReceived() == 0)
            fatalTestError = true;

        LOG.error("Have " + cons_client.getNumMsgReceived() + " messages; expected " + tot_expected
                + " on destination " + resp_dest);
    }

    resp_cons.close();
}

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 .  jav a 2s .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

protected static void closeConsumer(MessageConsumer consumer) {
    if (consumer != null) {
        try {/*from  w w  w  .  j a  va2s .  c  o  m*/
            consumer.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 {//from w ww .  j  a va2s  .c o m
            // 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;
            }
        }
    }
}