Example usage for javax.jms Connection close

List of usage examples for javax.jms Connection close

Introduction

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

Prototype


void close() throws JMSException;

Source Link

Document

Closes the connection.

Usage

From source file:org.apache.activemq.artemis.tests.integration.persistence.metrics.JournalPendingMessageTest.java

@Test
public void testQueueLargeMessageSizeTX() throws Exception {

    ActiveMQConnectionFactory acf = (ActiveMQConnectionFactory) cf;
    acf.setMinLargeMessageSize(1000);/*from  w  w  w  . jav  a  2s .c  om*/
    Connection connection = cf.createConnection();
    connection.start();
    Session session = connection.createSession(true, Session.SESSION_TRANSACTED);

    String testText = StringUtils.repeat("t", 2000);
    MessageProducer producer = session.createProducer(session.createQueue(defaultQueueName));
    ActiveMQTextMessage message = (ActiveMQTextMessage) session.createTextMessage(testText);
    for (int i = 0; i < 10; i++) {
        producer.send(message);
    }

    //not commited so should be 0
    verifyPendingStats(defaultQueueName, 0, message.getCoreMessage().getPersistentSize() * 10);
    verifyPendingDurableStats(defaultQueueName, 0, message.getCoreMessage().getPersistentSize() * 10);

    session.commit();

    verifyPendingStats(defaultQueueName, 10, message.getCoreMessage().getPersistentSize() * 10);
    verifyPendingDurableStats(defaultQueueName, 10, message.getCoreMessage().getPersistentSize() * 10);

    connection.close();

    this.killServer();
    this.restartServer();

    verifyPendingStats(defaultQueueName, 10, message.getCoreMessage().getPersistentSize());
    verifyPendingDurableStats(defaultQueueName, 10, message.getCoreMessage().getPersistentSize());
}

From source file:org.mule.transport.jms.integration.AbstractJmsFunctionalTestCase.java

/**
 * Purge destinations for clean test setup. Especially applicable to WMQ tests, as messages from
 * other tests may still exist from other tests' runs.
 * <p/>//from   w w w  .  j  av a  2 s .c  om
 * Well-behaving tests should drain both inbound and outbound destinations, as well as any intermediary ones.
 * @param destination destination name without any protocol specifics
 */
protected void purge(final String destination) throws JMSException {
    Connection c = null;
    Session s = null;
    try {
        logger.debug("purging queue : " + destination);
        c = getConnection(false, false);
        assertNotNull(c);
        c.start();

        s = c.createSession(false, Session.AUTO_ACKNOWLEDGE);
        Destination d = s.createQueue(destination);
        MessageConsumer consumer = s.createConsumer(d);

        while (consumer.receiveNoWait() != null) {
            logger.debug("Destination " + destination + " isn't empty, draining it");
        }
    } catch (Exception e) {
        logger.error("unable to purge : " + destination);
    } finally {
        if (c != null) {
            c.stop();
            if (s != null) {
                s.close();
            }
            try {
                c.close();
            } catch (JMSException e) {
                logger.warn("Failed to close jms connection: " + e.getMessage());
            }
        }
    }
}

From source file:org.apache.activemq.artemis.tests.integration.persistence.metrics.JournalPendingMessageTest.java

@Test
public void testMessageSizeSharedDurable() throws Exception {
    AtomicLong publishedMessageSize = new AtomicLong();

    Connection connection = cf.createConnection();
    connection.setClientID("clientId");
    connection.start();/*from w  w  w.  jav  a 2 s.c  o m*/

    // The publish method will create a second shared consumer
    Session s = connection.createSession();
    MessageConsumer c = s.createSharedDurableConsumer(s.createTopic(defaultTopicName), "sub1");
    publishTestMessagesDurable(connection, new String[] { "sub1", }, 200, publishedMessageSize,
            DeliveryMode.PERSISTENT, true);

    // verify the count and size - double because two durables so two queue
    // bindings
    verifyPendingStats(defaultTopicName, 200, publishedMessageSize.get());
    verifyPendingDurableStats(defaultTopicName, 200, publishedMessageSize.get());
    c.close();

    // consume messages for sub1
    consumeDurableTestMessages(connection, "sub1", 200, publishedMessageSize);
    verifyPendingStats(defaultTopicName, 0, publishedMessageSize.get());
    verifyPendingDurableStats(defaultTopicName, 0, publishedMessageSize.get());

    connection.close();
}

From source file:org.gss_project.gss.server.ejb.AdminAPIBean.java

public void indexFile(Long fileId, boolean delete) {
    Connection qConn = null;
    Session session = null;//from  w  w w. j  a  v a 2 s  .co  m
    MessageProducer sender = null;
    try {
        Context jndiCtx = new InitialContext();
        ConnectionFactory factory = (QueueConnectionFactory) jndiCtx.lookup("java:/JmsXA");
        Queue queue = (Queue) jndiCtx.lookup("queue/gss-indexingQueue");
        qConn = factory.createConnection();
        session = qConn.createSession(false, Session.AUTO_ACKNOWLEDGE);
        sender = session.createProducer(queue);

        MapMessage map = session.createMapMessage();
        map.setObject("id", fileId);
        map.setBoolean("delete", delete);
        sender.send(map);
    } catch (NamingException e) {
        logger.error("Index was not updated: ", e);
    } catch (JMSException e) {
        logger.error("Index was not updated: ", e);
    } finally {
        try {
            if (sender != null)
                sender.close();
            if (session != null)
                session.close();
            if (qConn != null)
                qConn.close();
        } catch (JMSException e) {
            logger.warn(e);
        }
    }
}

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

/**
 * TEST TEMPORARY QUEUES//from w w  w.j ava 2 s . c om
 */
public void testTempQueue(String prod_broker_url, String cons_broker_url) throws Exception {
    int num_msg;

    Connection conn;
    Session sess;

    Destination cons_dest;

    num_msg = 5;

    LOG.info("TESTING TEMP QUEUES " + prod_broker_url + " -> " + cons_broker_url + " (" + num_msg
            + " messages)");

    //
    // Connect to the bus.
    //
    conn = createConnection(cons_broker_url);
    conn.start();
    sess = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);

    //
    // Create the destination on which messages are being tested.
    //
    LOG.trace("Creating destination");
    cons_dest = sess.createTemporaryQueue();

    testOneDest(conn, sess, cons_dest, num_msg);

    //
    // Cleanup
    //
    sess.close();
    conn.close();
}

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

/**
 * TEST TEMPORARY TOPICS//from   w  ww . j av a 2s  .co m
 */
public void testTempTopic(String prod_broker_url, String cons_broker_url) throws Exception {
    Connection conn;
    Session sess;
    Destination cons_dest;
    int num_msg;

    num_msg = 5;

    LOG.debug("TESTING TEMP TOPICS " + prod_broker_url + " -> " + cons_broker_url + " (" + num_msg
            + " messages)");

    //
    // Connect to the bus.
    //
    conn = createConnection(cons_broker_url);
    conn.start();
    sess = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);

    //
    // Create the destination on which messages are being tested.
    //
    LOG.trace("Creating destination");
    cons_dest = sess.createTemporaryTopic();

    testOneDest(conn, sess, cons_dest, num_msg);

    //
    // Cleanup
    //
    sess.close();
    conn.close();
}

From source file:org.apache.qpid.test.utils.QpidBrokerTestCase.java

/**
 * Consume all the messages in the specified queue. Helper to ensure
 * persistent tests don't leave data behind.
 *
 * @param queue the queue to purge/*from ww w.  j a v  a  2 s  .  c  o  m*/
 *
 * @return the count of messages drained
 *
 * @throws Exception if a problem occurs
 */
protected int drainQueue(Queue queue) throws Exception
{
    Connection connection = getConnection();

    Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

    MessageConsumer consumer = session.createConsumer(queue);

    connection.start();

    int count = 0;
    while (consumer.receive(1000) != null)
    {
        count++;
    }

    connection.close();

    return count;
}

From source file:org.mule.transport.jms.integration.AbstractJmsFunctionalTestCase.java

public Message receive(Scenario scenario) throws Exception {
    assertNotNull("scenario is null!", scenario);
    Connection connection = null;
    try {/*w  ww. j  a v  a2s  .  c  o  m*/
        connection = getConnection(false, false);
        connection.start();
        Session session = null;
        try {
            session = connection.createSession(scenario.isTransacted(), scenario.getAcknowledge());
            Destination destination = createOutputDestination(session, scenario);
            MessageConsumer consumer = null;
            try {
                consumer = session.createConsumer(destination);
                return scenario.receive(session, consumer);
            } finally {
                if (consumer != null) {
                    consumer.close();
                }
            }
        } finally {
            if (session != null) {
                session.close();
            }
        }
    } finally {
        if (connection != null) {
            try {
                connection.close();
            } catch (JMSException e) {
                logger.warn("Failed to close jms connection: " + e.getMessage());
            }
        }
    }
}

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

/**
 * TEST QUEUES//  w  w w .  j a  v a  2s  . c  o  m
 */
public void testQueue(String prod_broker_url, String cons_broker_url) throws Exception {
    int num_msg;

    Connection conn;
    Session sess;
    String queue_name;

    Destination cons_dest;

    num_msg = 5;

    LOG.info("TESTING QUEUES " + prod_broker_url + " -> " + cons_broker_url + " (" + num_msg + " messages)");

    conn = createConnection(cons_broker_url);
    conn.start();
    sess = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);

    //
    // Create the destination on which messages are being tested.
    //
    queue_name = "topotest2.perm.queue";
    LOG.trace("Removing existing Queue");
    removeQueue(conn, queue_name);
    LOG.trace("Creating Queue, " + queue_name);
    cons_dest = sess.createQueue(queue_name);

    testOneDest(conn, sess, cons_dest, num_msg);

    removeQueue(conn, queue_name);
    sess.close();
    conn.close();
}

From source file:org.apache.activemq.bugs.AMQ7067Test.java

@Test
public void testRollback() throws Exception {
    final Connection connection = ACTIVE_MQ_NON_XA_CONNECTION_FACTORY.createConnection();
    connection.start();//from  w ww .j a  va  2 s . c  o  m

    Session session = connection.createSession(true, Session.SESSION_TRANSACTED);
    Queue holdKahaDb = session.createQueue("holdKahaDb");
    MessageProducer holdKahaDbProducer = session.createProducer(holdKahaDb);
    TextMessage helloMessage = session.createTextMessage(StringUtils.repeat("a", 10));
    holdKahaDbProducer.send(helloMessage);
    Queue queue = session.createQueue("test");
    produce(connection, queue, 100, 512 * 1024);
    session.rollback();
    produce(connection, queue, 100, 512 * 1024);

    System.out.println(String.format("QueueSize %s: %d", holdKahaDb.getQueueName(),
            getQueueSize(holdKahaDb.getQueueName())));
    purgeQueue(queue.getQueueName());

    Wait.waitFor(new Wait.Condition() {
        @Override
        public boolean isSatisified() throws Exception {
            return 0 == getQueueSize(queue.getQueueName());
        }
    });

    // force gc
    broker.getPersistenceAdapter().checkpoint(true);

    connection.close();
    curruptIndexFile(getDataDirectory());

    broker.stop();
    broker.waitUntilStopped();
    createBroker();
    broker.waitUntilStarted();

    // no sign of the test queue on recovery, rollback is the default for any inflight
    // this test serves as a sanity check on existing behaviour
    try {
        getQueueSize(holdKahaDb.getQueueName());
        fail("expect InstanceNotFoundException");
    } catch (UndeclaredThrowableException expected) {
        assertTrue(expected.getCause() instanceof InstanceNotFoundException);
    }
}