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 testTopicMessageSize() throws Exception {
    AtomicLong publishedMessageSize = new AtomicLong();

    Connection connection = cf.createConnection();
    connection.setClientID("clientId");
    connection.start();//  w  ww .j  a  v a2 s.  com
    Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    MessageConsumer consumer = session.createConsumer(session.createTopic(defaultTopicName));

    publishTestTopicMessages(200, publishedMessageSize);

    verifyPendingStats(defaultTopicName, 200, publishedMessageSize.get());
    verifyPendingDurableStats(defaultQueueName, 0, 0);

    // consume all messages
    consumeTestMessages(consumer, 200);

    // All messages should now be gone
    verifyPendingStats(defaultTopicName, 0, 0);
    verifyPendingDurableStats(defaultQueueName, 0, 0);

    connection.close();
}

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

private void sendMessage(String queueName, int count, boolean transacted) throws JMSException {
    Connection con = connectionFactory.createConnection();
    try {//from w  w  w  . j  a v a  2  s .  co  m
        Session session = con.createSession(transacted,
                transacted ? Session.SESSION_TRANSACTED : Session.AUTO_ACKNOWLEDGE);
        try {
            Queue destination = session.createQueue(queueName);
            MessageProducer producer = session.createProducer(destination);
            try {
                for (int i = 0; i < count; i++) {
                    TextMessage message = session.createTextMessage();
                    message.setIntProperty("MessageId", messageCounter);
                    message.setText("Message-" + messageCounter++);
                    producer.send(message);
                }
                if (transacted) {
                    session.commit();
                }
            } finally {
                producer.close();
            }
        } finally {
            session.close();
        }
    } finally {
        con.close();
    }
}

From source file:org.wso2.carbon.esb.scenario.test.common.jms.ActiveMQJMSClient.java

/**
 * Function to produce message to ActiveMQ Queue
 *
 * @param queueName name of the target queue
 * @param messageStr message to place/* w  w w .j av  a2 s.  c  o m*/
 * @throws JMSException
 */
public void produceMessageToQueue(String queueName, String messageStr) throws JMSException {

    Connection connection = null;
    Session session = null;

    try {
        // Create a ConnectionFactory
        ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(brokerUrl);

        // Create a Connection
        connection = connectionFactory.createConnection();
        connection.start();
        connection.setExceptionListener(this);

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

        // Create the destination (Topic or Queue)
        Destination destination = session.createQueue(queueName);

        // Create a MessageProducer from the Session to the Topic or Queue
        MessageProducer producer = session.createProducer(destination);
        producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);

        // Create a messages
        TextMessage message = session.createTextMessage(messageStr);

        // Tell the producer to send the message
        producer.send(message);

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

From source file:io.fabric8.mq.controller.coordination.BrokerJMXPropertiesTest.java

@Test
public void testNumberOfTopicProducers() throws Exception {
    String uriString = brokerService.getDefaultSocketURIString();
    ConnectionFactory factory = new ActiveMQConnectionFactory(uriString);
    Connection connection = factory.createConnection();
    connection.start();/*from w  w w  .  j a  v a 2  s  .  c  o m*/
    Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    int numberOfProducers = 10;
    List<MessageProducer> producers = new ArrayList<>();
    Topic topic = session.createTopic("topic.test");
    for (int i = 0; i < numberOfProducers; i++) {

        MessageProducer producer = session.createProducer(topic);
        producers.add(producer);

    }
    ObjectName root = BrokerJmxUtils.getRoot(client);
    Assert.assertNotNull(root);
    List<ObjectName> list = BrokerJmxUtils.getDestinations(client, root, "Topic");
    Assert.assertNotNull(list);
    Assert.assertFalse(list.isEmpty());
    ObjectName result = null;
    for (ObjectName objectName : list) {
        if (objectName.getKeyProperty("destinationName").equals("topic.test")) {
            result = objectName;
        }
    }
    Assert.assertNotNull(result);
    Object producerCount = BrokerJmxUtils.getAttribute(client, result, "ProducerCount");
    Assert.assertNotNull(producerCount);
    int pc = Integer.parseInt(producerCount.toString());
    Assert.assertEquals(pc, numberOfProducers);
    connection.close();
}

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

protected void publishTestTopicMessages(int publishSize, int deliveryMode, AtomicLong publishedMessageSize)
        throws Exception {
    // create a new queue
    Connection connection = cf.createConnection();
    connection.setClientID("clientId2");
    connection.start();/*from  w  ww.jav a 2s.c o m*/

    // Start the connection
    Session session = connection.createSession(false, TopicSession.AUTO_ACKNOWLEDGE);
    Topic topic = session.createTopic(defaultTopicName);

    try {
        // publish a bunch of non-persistent messages to fill up the temp
        // store
        MessageProducer prod = session.createProducer(topic);
        prod.setDeliveryMode(deliveryMode);
        for (int i = 0; i < publishSize; i++) {
            prod.send(
                    createMessage(i, session, JournalPendingMessageTest.maxMessageSize, publishedMessageSize));
        }

    } finally {
        connection.close();
    }
}

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

protected void tearDown() throws java.lang.Exception
{
    super.tearDown();

    // close all the connections used by this test.
    for (Connection c : _connections)
    {//  w w  w  . ja  v a2  s .  c o  m
        c.close();
    }
}

From source file:org.wso2.andes.test.utils.QpidBrokerTestCase.java

protected void tearDown() throws java.lang.Exception
{
    try//from  www .ja  va  2  s.  c  o  m
    {
        // close all the connections used by this test.
        for (Connection c : _connections)
        {
            c.close();
        }
    }
    finally
    {
        // Ensure any problems with close does not interfer with property resets
        revertSystemProperties();
        revertLoggingLevels();
    }
}

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

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

    Connection connection = cf.createConnection();
    connection.start();//from  ww w.j a  v  a 2 s. c  o m
    Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    MessageProducer producer = session.createProducer(session.createQueue(defaultQueueName));
    producer.setDeliveryDelay(2000);
    producer.send(session.createTextMessage("test"));

    verifyPendingStats(defaultQueueName, 1, publishedMessageSize.get());
    verifyPendingDurableStats(defaultQueueName, 1, publishedMessageSize.get());
    verifyScheduledStats(defaultQueueName, 1, publishedMessageSize.get());

    consumeTestQueueMessages(1);

    verifyPendingStats(defaultQueueName, 0, 0);
    verifyPendingDurableStats(defaultQueueName, 0, 0);
    verifyScheduledStats(defaultQueueName, 0, 0);

    connection.close();
}

From source file:org.wso2.carbon.event.input.adapter.jms.internal.util.JMSListener.java

private boolean checkJMSConnection(JMSTaskManager stm) {

    Connection connection = null;
    boolean connectionEstablished = false;
    Hashtable<String, String> jmsProperties = stm.getJmsProperties();
    try {/*from w w w.j  av a 2 s . c o m*/
        ConnectionFactory jmsConFactory = null;
        try {
            jmsConFactory = JMSUtils.lookup(new InitialContext(stm.getJmsProperties()), ConnectionFactory.class,
                    stm.getConnFactoryJNDIName());
        } catch (NamingException e) {
            log.error("Error looking up connection factory : " + stm.getConnFactoryJNDIName()
                    + "using JNDI properties : " + jmsProperties, e);
        }
        connection = JMSUtils.createConnection(jmsConFactory,
                jmsProperties.get(JMSConstants.PARAM_JMS_USERNAME),
                jmsProperties.get(JMSConstants.PARAM_JMS_PASSWORD), stm.isJmsSpec11(), stm.isQueue(),
                stm.isSubscriptionDurable(), stm.getDurableSubscriberClientId());
        connectionEstablished = connection != null;
    } catch (JMSException ignore) {
        // we silently ignore this as a JMSException can be expected when connection is not available
    } finally {
        if (connectionEstablished) {
            try {
                connection.close();
            } catch (JMSException e) {
                log.debug("Error while closing established Test JMS connection: " + e.getMessage(), e);
            }
        }
    }

    return connectionEstablished;
}

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

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

    Connection connection = cf.createConnection();
    connection.setClientID("clientId");
    connection.start();/*from  www . ja  v a 2s.co  m*/
    Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    MessageConsumer consumer = session.createConsumer(session.createTopic(defaultTopicName));

    publishTestTopicMessages(100, DeliveryMode.NON_PERSISTENT, publishedNonPersistentMessageSize);
    publishTestTopicMessages(100, DeliveryMode.PERSISTENT, publishedMessageSize);

    verifyPendingStats(defaultTopicName, 200,
            publishedMessageSize.get() + publishedNonPersistentMessageSize.get());

    // consume all messages
    consumeTestMessages(consumer, 200);

    // All messages should now be gone
    verifyPendingStats(defaultTopicName, 0, 0);

    connection.close();
}