alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Spring Framework example source code file (SimpleServerSessionFactory.java)

This example Spring Framework source code file (SimpleServerSessionFactory.java) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Spring Framework tags/keywords

jmsexception, listenersessionmanager, serversession, serversession, serversessionfactory, session, session, simpleasynctaskexecutor, simpleserversession, simpleserversession, simpleserversessionfactory, string, taskexecutor, taskexecutor

The Spring Framework SimpleServerSessionFactory.java source code

/*
 * Copyright 2002-2008 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.serversession;

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

import org.springframework.core.task.SimpleAsyncTaskExecutor;
import org.springframework.core.task.TaskExecutor;
import org.springframework.jms.support.JmsUtils;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;

/**
 * The simplest possible implementation of the ServerSessionFactory SPI:
 * creating a new ServerSession with a new JMS Session every time.
 * This is the default used by ServerSessionMessageListenerContainer.
 *
 * <p>The execution of a ServerSession (and its MessageListener) gets delegated
 * to a TaskExecutor. By default, a SimpleAsyncTaskExecutor will be used,
 * creating a new Thread for every execution attempt. Alternatives are a
 * TimerTaskExecutor, sharing a single Thread for the execution of all
 * ServerSessions, or a TaskExecutor implementation backed by a thread pool.
 *
 * <p>To reuse JMS Sessions and/or to limit the number of concurrent
 * ServerSession executions, consider using a pooling ServerSessionFactory:
 * for example, CommonsPoolServerSessionFactory.
 *
 * @author Juergen Hoeller
 * @since 2.0
 * @deprecated as of Spring 2.5, in favor of DefaultMessageListenerContainer
 * and JmsMessageEndpointManager. To be removed in Spring 3.0.
 * @see org.springframework.core.task.TaskExecutor
 * @see org.springframework.core.task.SimpleAsyncTaskExecutor
 * @see org.springframework.scheduling.timer.TimerTaskExecutor
 * @see CommonsPoolServerSessionFactory
 * @see ServerSessionMessageListenerContainer
 */
public class SimpleServerSessionFactory implements ServerSessionFactory {

	/**
	 * Default thread name prefix: "SimpleServerSessionFactory-".
	 */
	public static final String DEFAULT_THREAD_NAME_PREFIX =
			ClassUtils.getShortName(SimpleServerSessionFactory.class) + "-";


	private TaskExecutor taskExecutor = new SimpleAsyncTaskExecutor(DEFAULT_THREAD_NAME_PREFIX);


	/**
	 * Specify the TaskExecutor to use for executing ServerSessions
	 * (and consequently, the underlying MessageListener).
	 * <p>Default is a SimpleAsyncTaskExecutor, creating a new Thread for
	 * every execution attempt. Alternatives are a TimerTaskExecutor,
	 * sharing a single Thread for the execution of all ServerSessions,
	 * or a TaskExecutor implementation backed by a thread pool.
	 * @see org.springframework.core.task.SimpleAsyncTaskExecutor
	 * @see org.springframework.scheduling.timer.TimerTaskExecutor
	 */
	public void setTaskExecutor(TaskExecutor taskExecutor) {
		Assert.notNull(taskExecutor, "taskExecutor is required");
		this.taskExecutor = taskExecutor;
	}

	/**
	 * Return the TaskExecutor to use for executing ServerSessions.
	 */
	protected TaskExecutor getTaskExecutor() {
		return taskExecutor;
	}


	/**
	 * Creates a new SimpleServerSession with a new JMS Session
	 * for every call.
	 */
	public ServerSession getServerSession(ListenerSessionManager sessionManager) throws JMSException {
		return new SimpleServerSession(sessionManager);
	}

	/**
	 * This implementation is empty, as there is no state held for
	 * each ListenerSessionManager.
	 */
	public void close(ListenerSessionManager sessionManager) {
	}


	/**
	 * ServerSession implementation that simply creates a new
	 * JMS Session and executes it via the specified TaskExecutor.
	 */
	private class SimpleServerSession implements ServerSession {

		private final ListenerSessionManager sessionManager;

		private final Session session;

		public SimpleServerSession(ListenerSessionManager sessionManager) throws JMSException {
			this.sessionManager = sessionManager;
			this.session = sessionManager.createListenerSession();
		}

		public Session getSession() {
			return session;
		}

		public void start() {
			getTaskExecutor().execute(new Runnable() {
				public void run() {
					try {
						sessionManager.executeListenerSession(session);
					}
					finally {
						JmsUtils.closeSession(session);
					}
				}
			});
		}
	}

}

Other Spring Framework examples (source code examples)

Here is a short list of links related to this Spring Framework SimpleServerSessionFactory.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.