Example usage for javax.jms Session close

List of usage examples for javax.jms Session close

Introduction

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

Prototype


void close() throws JMSException;

Source Link

Document

Closes the session.

Usage

From source file:org.apache.activemq.JmsConnectionStartStopTest.java

/**
 * Tests if the consumer receives the messages that were sent before the
 * connection was started./*  w  w w.  j a  va  2  s . c  o  m*/
 *
 * @throws JMSException
 */
public void testStoppedConsumerHoldsMessagesTillStarted() throws JMSException {
    Session startedSession = startedConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    Session stoppedSession = stoppedConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);

    // Setup the consumers.
    Topic topic = startedSession.createTopic("test");
    MessageConsumer startedConsumer = startedSession.createConsumer(topic);
    MessageConsumer stoppedConsumer = stoppedSession.createConsumer(topic);

    // Send the message.
    MessageProducer producer = startedSession.createProducer(topic);
    TextMessage message = startedSession.createTextMessage("Hello");
    producer.send(message);

    // Test the assertions.
    Message m = startedConsumer.receive(1000);
    assertNotNull(m);

    m = stoppedConsumer.receive(1000);
    assertNull(m);

    stoppedConnection.start();
    m = stoppedConsumer.receive(5000);
    assertNotNull(m);

    startedSession.close();
    stoppedSession.close();
}

From source file:org.apache.activemq.network.BrokerNetworkWithStuckMessagesTest.java

@SuppressWarnings({ "unchecked", "unused" })
private Object[] browseQueueWithJms(BrokerService broker) throws Exception {
    Object[] messages = null;// w ww  .  ja  va  2 s .  c  o m
    Connection connection = null;
    Session session = null;

    try {
        URI brokerUri = connector.getUri();
        ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(brokerUri.toString());
        connection = connectionFactory.createConnection();
        connection.start();
        session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        Queue destination = session.createQueue(queueName);
        QueueBrowser browser = session.createBrowser(destination);
        List<Message> list = new ArrayList<Message>();
        for (Enumeration<Message> enumn = browser.getEnumeration(); enumn.hasMoreElements();) {
            list.add(enumn.nextElement());
        }
        messages = list.toArray();
    } finally {
        if (session != null) {
            session.close();
        }
        if (connection != null) {
            connection.close();
        }
    }
    LOG.info("+Browsed with JMS: " + messages.length);

    return messages;
}

From source file:org.apache.activemq.network.MQTTNetworkOfBrokersFailoverTest.java

private CountDownLatch listenForConsumersOn(BrokerService broker) throws Exception {
    final CountDownLatch latch = new CountDownLatch(1);

    URI brokerUri = broker.getVmConnectorURI();

    final ActiveMQConnectionFactory cf = new ActiveMQConnectionFactory(brokerUri.toASCIIString());
    final Connection connection = cf.createConnection();
    connection.start();//from w  w w .j  ava2 s .c  o m
    final Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    Destination dest = session
            .createTopic("ActiveMQ.Advisory.Consumer.Queue.Consumer.foo:AT_LEAST_ONCE.VirtualTopic.foo.bar");
    MessageConsumer consumer = session.createConsumer(dest);
    consumer.setMessageListener(new MessageListener() {
        @Override
        public void onMessage(Message message) {
            latch.countDown();
            // shutdown this connection
            Dispatch.getGlobalQueue().execute(new Runnable() {
                @Override
                public void run() {
                    try {
                        session.close();
                        connection.close();
                    } catch (JMSException e) {
                        e.printStackTrace();
                    }
                }
            });
        }
    });

    return latch;
}

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 {//  www . ja  va2s. c  o  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.apache.activemq.store.jdbc.JmsTransactionCommitFailureTest.java

private Message receiveMessage(String queueName, long receiveTimeout) throws JMSException {
    Message message = null;//from  w  w w.  ja  v  a 2s . co  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.usecases.RequestReplyToTopicViaThreeNetworkHopsTest.java

/**
 * TEST TEMPORARY TOPICS//from  ww w  .jav a 2 s  . c  om
 */
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.activemq.usecases.RequestReplyToTopicViaThreeNetworkHopsTest.java

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

    Connection conn;
    Session sess;
    String topic_name;

    Destination cons_dest;

    num_msg = 5;

    LOG.info("TESTING TOPICS " + 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.
    //
    topic_name = "topotest2.perm.topic";
    LOG.trace("Removing existing Topic");
    removeTopic(conn, topic_name);
    LOG.trace("Creating Topic, " + topic_name);
    cons_dest = sess.createTopic(topic_name);

    testOneDest(conn, sess, cons_dest, num_msg);

    //
    // Cleanup
    //
    removeTopic(conn, topic_name);
    sess.close();
    conn.close();
}

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

/**
 * TEST TEMPORARY QUEUES/*from  w ww  .  ja va  2  s  . c o  m*/
 */
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 QUEUES/*from ww  w . j  av  a  2 s. c  om*/
 */
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.flume.source.jms.TestIntegrationActiveMQ.java

private void putQueue(List<String> events) throws Exception {
    ConnectionFactory factory = new ActiveMQConnectionFactory(USERNAME, PASSWORD, BROKER_BIND_URL);
    Connection connection = factory.createConnection();
    connection.start();/*from   ww  w  . ja va2s .c  o m*/

    Session session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);
    Destination destination = session.createQueue(DESTINATION_NAME);
    MessageProducer producer = session.createProducer(destination);

    for (String event : events) {
        TextMessage message = session.createTextMessage();
        message.setText(event);
        producer.send(message);
    }
    session.commit();
    session.close();
    connection.close();
}