package org.apache.activemq.pool;

import org.apache.activemq.*;
import org.apache.activemq.broker.BrokerService;
import org.apache.activemq.broker.TransportConnector;

import junit.framework.*;
import javax.jms.*;
import javax.jms.Message;

import org.apache.log4j.Logger;

public class PooledSessionExhaustionTest extends TestCase {
    private static final String QUEUE = "FOO";
    private static final int NUM_MESSAGES = 700;

    private Logger logger = Logger.getLogger(getClass());

    private BrokerService broker;
    private ActiveMQConnectionFactory factory;
    private PooledConnectionFactory pooledFactory;
    private String connectionUri;
    private int numReceived = 0;

    protected void setUp() throws Exception {
        broker = new BrokerService();
        broker.setPersistent(false);
        TransportConnector connector = broker.addConnector("tcp://localhost:0");
        broker.start();
        connectionUri = connector.getPublishableConnectString();
        factory = new ActiveMQConnectionFactory(connectionUri);
        pooledFactory = new PooledConnectionFactory(factory);
        pooledFactory.setMaxConnections(1);
        pooledFactory.setBlockIfSessionPoolIsFull(false);
    }

    public void sendMessages(ConnectionFactory connectionFactory) throws Exception {
        for (int i = 0; i < NUM_MESSAGES; i++) {
            Connection connection = connectionFactory.createConnection();
            connection.start();

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

            String msgTo = "hello";
            TextMessage message = session.createTextMessage(msgTo);
            producer.send(message);
            connection.close();
            logger.debug("sent " + i + " messages using " + connectionFactory.getClass());
        }
    }

    public void testCanExhaustSessions() throws Exception {
        Thread thread = new Thread(new Runnable() {
            public void run() {
                try {
                    ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(connectionUri);
                    Connection connection = connectionFactory.createConnection();
                    connection.start();

                    Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
                    Destination destination = session.createQueue(QUEUE);
                    MessageConsumer consumer = session.createConsumer(destination);
                    for (int i = 0; i < NUM_MESSAGES; ++i) {
                        Message msg = consumer.receive(5000);
                        if (msg == null) {
                            return;
                        }
                        numReceived++;
                        if (numReceived % 20 == 0) {
                            logger.debug("received " + numReceived + " messages ");
                            System.runFinalization();
                        }
                    }
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
        });
        thread.start();

        sendMessages(pooledFactory);
        thread.join();

        assertEquals(NUM_MESSAGES, numReceived);
    }
}

@@ -1,8 +1,812 @@
+/**%0A * Licensed to the Apache Software Foundation (ASF) under one or more%0A * contributor license agreements.  See the NOTICE file distributed with%0A * this work for additional information regarding copyright ownership.%0A * The ASF licenses this file to You under the Apache License, Version 2.0%0A * (the %22License%22); you may not use this file except in compliance with%0A * the License.  You may obtain a copy of the License at%0A *%0A *      http://www.apache.org/licenses/LICENSE-2.0%0A *%0A * Unless required by applicable law or agreed to in writing, software%0A * distributed under the License is distributed on an %22AS IS%22 BASIS,%0A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.%0A * See the License for the specific language governing permissions and%0A * limitations under the License.%0A */%0A
 package 
@@ -1484,24 +1484,38 @@
 eived = 0;%0A%0A
+    @Override%0A
     protecte
@@ -1963,16 +1963,16 @@
 ons(1);%0A
-
         
@@ -2020,32 +2020,183 @@
 (false);%0A    %7D%0A%0A
+    @Override%0A    protected void tearDown() throws Exception %7B%0A        broker.stop();%0A        broker.waitUntilStopped();%0A        broker = null;%0A    %7D%0A%0A
     public void 

Add missing license header and a tearDown method to ensure the created BrokerService gets stopped.