📄 destinationmanager.java
字号:
/**
* Redistribution and use of this software and associated documentation
* ("Software"), with or without modification, are permitted provided
* that the following conditions are met:
*
* 1. Redistributions of source code must retain copyright
* statements and notices. Redistributions must also contain a
* copy of this document.
*
* 2. Redistributions in binary form must reproduce the
* above copyright notice, this list of conditions and the
* following disclaimer in the documentation and/or other
* materials provided with the distribution.
*
* 3. The name "Exolab" must not be used to endorse or promote
* products derived from this Software without prior written
* permission of Exoffice Technologies. For written permission,
* please contact info@exolab.org.
*
* 4. Products derived from this Software may not be called "Exolab"
* nor may "Exolab" appear in their names without prior written
* permission of Exoffice Technologies. Exolab is a registered
* trademark of Exoffice Technologies.
*
* 5. Due credit should be given to the Exolab Project
* (http://www.exolab.org/).
*
* THIS SOFTWARE IS PROVIDED BY EXOFFICE TECHNOLOGIES AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
* NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* EXOFFICE TECHNOLOGIES OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*
* Copyright 2001-2003 (C) Exoffice Technologies Inc. All Rights Reserved.
*
* $Id: DestinationManager.java,v 1.36 2003/08/16 06:37:43 tanderson Exp $
*
* Date Author Changes
* 3/1/2001 jima Created
*/
package org.exolab.jms.messagemgr;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Map;
import java.util.Vector;
import javax.jms.DeliveryMode;
import javax.jms.JMSException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.transaction.TransactionManager;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.exolab.core.service.ServiceException;
import org.exolab.jms.client.JmsDestination;
import org.exolab.jms.client.JmsQueue;
import org.exolab.jms.client.JmsTopic;
import org.exolab.jms.config.AdministeredDestinations;
import org.exolab.jms.config.AdministeredQueue;
import org.exolab.jms.config.AdministeredTopic;
import org.exolab.jms.config.Configuration;
import org.exolab.jms.config.ConfigurationManager;
import org.exolab.jms.config.MessageManagerConfiguration;
import org.exolab.jms.config.Subscriber;
import org.exolab.jms.gc.GarbageCollectable;
import org.exolab.jms.gc.GarbageCollectionService;
import org.exolab.jms.message.MessageImpl;
import org.exolab.jms.persistence.DatabaseService;
import org.exolab.jms.persistence.PersistenceAdapter;
import org.exolab.jms.persistence.PersistenceException;
import org.exolab.jms.persistence.SQLHelper;
import org.exolab.jms.server.NamingHelper;
// @todo - need to restructure dirs
/**
* The destination manager is responsible for creating and managing the
* lifecycle of {@link DestinationCache} objects. The destination manager
* is also responsible for managing messages, that are received by the
* message manager, which do not have any registered {@link DestinationCache}
*
* @version $Revision: 1.36 $ $Date: 2003/08/16 06:37:43 $
* @author <a href="mailto:jima@exoffice.com">Jim Alateras</a>
*/
public class DestinationManager
implements MessageManagerEventListener, GarbageCollectable {
/**
* This structure maintains a list of active caches.
*/
private Map _caches = Collections.synchronizedMap(new HashMap());
/**
* A list of administered and non-administered destinations are maintained
* in this data structure.
*/
private HashMap _destinationCache = new HashMap();
/**
* The maximum no. of messages that each destination cache can hold
*/
private final int _maxCacheSize;
/**
* Maintains a list of wildcard destinations, which can either be durable
* or transient
*/
private LinkedList _wildcardDestinations = new LinkedList();
/**
* Maintains a linked list of DestinationEventListener objects. These
* listeners will be informated when destination are added or destroyed
*/
private LinkedList _listeners = new LinkedList();
/**
* Manage the singleton instance of the DestinationManager
*/
private static volatile DestinationManager _instance = null;
/**
* Caches the root context for all jndi binding.
*/
private InitialContext _context = null;
/**
* The logger
*/
private static final Log _log =
LogFactory.getLog(DestinationManager.class);
/**
* Construct a new <code>DestinationManager</code>
*
* @throws ServiceException if the service cannot be initialised
*/
private DestinationManager() throws ServiceException {
MessageManagerConfiguration config =
ConfigurationManager.getConfig().getMessageManagerConfiguration();
_maxCacheSize = config.getDestinationCacheSize();
init();
// register with the GarbageCollectionService
GarbageCollectionService.instance().register(this);
}
/**
* Create the singleton instance of the destination manager
*
* @return the singleton instance
* @throws ServiceException if the service cannot be initialised
*/
public static DestinationManager createInstance() throws ServiceException {
_instance = new DestinationManager();
return _instance;
}
/**
* Return the singleton destination manager
*
* @return the singleton instance, or <code>null</code> if it hasn't
* been initialised
*/
public static DestinationManager instance() {
return _instance;
}
/**
* Create a destination of the specified type. If the destination already
* exists then simply return it. If it doesn't exist then create it.
*
* @param dest - the destination to create
* @return DestinationCache - the created destination cache
*/
public synchronized DestinationCache createDestinationCache(
JmsDestination dest) {
return createDestinationCache(null, dest);
}
/**
* Create a destination of the specified type. If the destination already
* exists then simply return it. If it doesn't exist then create it. If
* a connection is supplied then create the cache using the connection
* context, otherwise create it without a context.
*
* @param connection - the connection to use.
* @param dest - the destination to create
* @return DestinationCache - the created destination cache
*/
public synchronized DestinationCache createDestinationCache(
Connection connection, JmsDestination dest) {
// check to see if it exists first
DestinationCache cache = (DestinationCache) _caches.get(dest);
if (cache == null) {
// create a destination based on its type
try {
if (dest instanceof JmsTopic) {
cache = (connection != null) ?
new TopicDestinationCache(connection, (JmsTopic) dest) :
new TopicDestinationCache((JmsTopic) dest);
} else if (dest instanceof JmsQueue) {
cache = (connection != null) ?
new QueueDestinationCache(connection, (JmsQueue) dest) :
new QueueDestinationCache((JmsQueue) dest);
}
// set the configured size of each destination cache
cache.setMaximumSize(_maxCacheSize);
// notify the listeners that a new destination has been added
// to the destination manager
notifyDestinationAdded(dest, cache);
//cache it first
_caches.put(dest, cache);
} catch (Exception exception) {
_log.error("Failed to createDestinationCache", exception);
}
}
return cache;
}
/**
* Delete the specfied destination
*
* @param cahe - the destination to destroy
*/
public synchronized void destroyDestinationCache(DestinationCache cache) {
destroyDestinationCache(cache.getDestination());
}
/**
* Delete the specfied destination
*
* @deprecated use destroyDestination(JmsDestination) instead
* @param name - destination name
*/
public synchronized void destroyDestinationCache(String name) {
// currently we are doing a linear search through the
// destinations until we find the corresponding {@link JmsDestination}
// We want to discourage the use of this function.
Iterator iter = _caches.keySet().iterator();
while (iter.hasNext()) {
JmsDestination dest = (JmsDestination) iter.next();
if (dest.getName().equals(name)) {
destroyDestinationCache(dest);
break;
}
}
}
/**
* Delete the specfied destination
*
* @param dest - the destination to destroy
*/
public synchronized void destroyDestinationCache(JmsDestination dest) {
DestinationCache cache = (DestinationCache) _caches.remove(dest);
if (cache != null) {
cache.destroy();
// notify the listeners that a destination has been removed from
// the destination manager
notifyDestinationRemoved(dest, cache);
}
}
/**
* Return the JmsDestination corresponding to the specified destination
* name
*
* @param name - destination name
* @return JmsDestination - the corresponding object or null
*/
public synchronized JmsDestination destinationFromString(String name) {
return (JmsDestination) _destinationCache.get(name);
}
/**
* Register the specified DestinationEventListener. If the listener is
* already registered then do not re-register it again.
*
* @param listener - listener to add
*/
void addDestinationEventListener(DestinationEventListener listener) {
synchronized (_listeners) {
if (!_listeners.contains(listener)) {
_listeners.add(listener);
}
}
}
/**
* Remove the specified DestinationEventListener from the list
*
* @param listener - listener to remove
*/
void removeDestinationEventListener(DestinationEventListener listener) {
synchronized (_listeners) {
_listeners.remove(listener);
}
}
/**
* Return the destination cache associated with the dest object
*
* @param dest - the destination
* @return DestinationCache - associated destination object or null
*/
public DestinationCache getDestinationCache(JmsDestination dest) {
return (DestinationCache) _caches.get(dest);
}
/**
* Return the destination object associated with destination
*
* @param dest - the name of the destination
* @return DestinationCache - associated destination object or null
*/
public DestinationCache getDestinationCache(String dest) {
return getDestinationCache(destinationFromString(dest));
}
/**
* Return the {@link DestinationCache} for this message.
*
* @param message - the message to query
* @return DestinationCache - the corresponding cahce or null
*/
public DestinationCache getDestinationCache(MessageImpl message) {
DestinationCache cache = null;
if (message != null) {
try {
cache = getDestinationCache(
(JmsDestination) message.getJMSDestination());
} catch (JMSException exception) {
// ignore it, probably means thant no destination has been
// assigned.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -