<commit_before>/*
 * Copyright 2002-2005 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.jms.listener;

import javax.jms.JMSException;
import javax.jms.ServerSession;
import javax.jms.Session;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.springframework.jms.support.JmsUtils;

/**
 * @author <NAME>
 * @since 1.3
 */
public abstract class AbstractPoolingServerSessionFactory implements ServerSessionFactory {

	protected final Log logger = LogFactory.getLog(getClass());

	private int maxSize;


	/**
	 * Set the maximum size of the pool.
	 */
	public void setMaxSize(int maxSize) {
		this.maxSize = maxSize;
	}

	/**
	 * Return the maximum size of the pool.
	 */
	public int getMaxSize() {
		return this.maxSize;
	}


	protected ServerSession createServerSession(ListenerSessionManager sessionManager) throws JMSException {
		return new PooledServerSession(sessionManager);
	}

	protected void destroyServerSession(ServerSession serverSession) {
		((PooledServerSession) serverSession).close();
	}

	protected abstract void serverSessionFinished(ServerSession serverSession, ListenerSessionManager sessionManager);


	private class PooledServerSession implements ServerSession {

		private final Session session;

		private final Object monitor = new Object();

		private boolean active = true;

		public PooledServerSession(final ListenerSessionManager sessionManager) throws JMSException {
			this.session = sessionManager.createListenerSession();

			new Thread() {
				public void run() {
					synchronized (monitor) {
						while (active) {
							try {
								monitor.wait();
							}
							catch (InterruptedException ex) {
							}
							if (active) {
								try {
									sessionManager.executeListenerSession(session);
								}
								finally {
									serverSessionFinished(PooledServerSession.this, sessionManager);
								}
							}
						}
					}
				}
			}.start();
		}

		public Session getSession() {
			return session;
		}

		public void start() {
			synchronized (this.monitor) {
				this.monitor.notify();
			}
		}

		public void close() {
			this.active = false;
			synchronized (this.monitor) {
				this.monitor.notify();
			}
			JmsUtils.closeSession(this.session);
		}
	}

}
<commit_msg>workaround for PooledServerSession startup<commit_after>/*
 * Copyright 2002-2005 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.jms.listener;

import javax.jms.JMSException;
import javax.jms.ServerSession;
import javax.jms.Session;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.springframework.jms.support.JmsUtils;

/**
 * @author <NAME>
 * @since 1.3
 */
public abstract class AbstractPoolingServerSessionFactory implements ServerSessionFactory {

	protected final Log logger = LogFactory.getLog(getClass());

	private int maxSize;


	/**
	 * Set the maximum size of the pool.
	 */
	public void setMaxSize(int maxSize) {
		this.maxSize = maxSize;
	}

	/**
	 * Return the maximum size of the pool.
	 */
	public int getMaxSize() {
		return this.maxSize;
	}


	protected ServerSession createServerSession(ListenerSessionManager sessionManager) throws JMSException {
		return new PooledServerSession(sessionManager);
	}

	protected void destroyServerSession(ServerSession serverSession) {
		((PooledServerSession) serverSession).close();
	}

	protected abstract void serverSessionFinished(ServerSession serverSession, ListenerSessionManager sessionManager);


	private class PooledServerSession implements ServerSession {

		private final Session session;

		private final Object monitor = new Object();

		private boolean active = false;

		public PooledServerSession(final ListenerSessionManager sessionManager) throws JMSException {
			this.session = sessionManager.createListenerSession();

			new Thread() {
				public void run() {
					active = true;
					synchronized (monitor) {
						while (active) {
							try {
								logger.debug("Waiting for PooledServerSession monitor");
								monitor.wait();
								logger.debug("Notified by PooledServerSession monitor");
							}
							catch (InterruptedException ex) {
							}
							if (active) {
								try {
									sessionManager.executeListenerSession(session);
								}
								finally {
									serverSessionFinished(PooledServerSession.this, sessionManager);
								}
							}
						}
					}
				}
			}.start();

			while (!this.active) {
			}
		}

		public Session getSession() {
			return session;
		}

		public void start() {
			synchronized (this.monitor) {
				logger.debug("Notifying PooledServerSession monitor");
				this.monitor.notify();
				logger.debug("Notified PooledServerSession monitor");
			}
		}

		public void close() {
			this.active = false;
			synchronized (this.monitor) {
				this.monitor.notify();
			}
			JmsUtils.closeSession(this.session);
		}
	}

}

{'fraction_non_alphanumeric': 0.08923536241499419, 'fraction_numerical': 0.004644219605241333, 'mean_word_length': 3.4932935916542474, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '263222', 'n_tokens_mistral': 2005, 'n_tokens_neox': 1611, 'n_words': 489}