📄 consumers.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 2000-2001,2003 (C) Exoffice Technologies Inc. All Rights Reserved.
*/
package org.exolab.jms.persistence;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Vector;
import javax.jms.JMSException;
import javax.sql.DataSource;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.exolab.jms.client.JmsDestination;
import org.exolab.jms.client.JmsTopic;
/**
* This class provides persistency for ConsumerState objects
* in an RDBMS database
*
* @version $Revision: 1.9 $ $Date: 2003/08/07 13:33:06 $
* @author <a href="mailto:tima@intalio.com">Tim Anderson</a>
* @see org.exolab.jms.persistence.ConsumerState
* @see org.exolab.jms.persistence.RDBMSAdapter
*/
class Consumers {
/**
* A cache for all durable consumers
*/
private HashMap _consumers;
/**
* A refernce to the singleton instance of this class
*/
private static Consumers _instance;
/**
* Monitor used to synchronize access to the initialization of
* the singleton
*/
private static final Boolean block = new Boolean(true);
/**
* The name of the column that uniquely identifies the consumer
*/
private static final String CONSUMER_ID_SEED = "consumerId";
/**
* The name of the table that maintains a list of message handles
* per consumer
*/
private static final String CONSUMER_MESSAGE = "message_handles";
/**
* The logger
*/
private static final Log _log = LogFactory.getLog(Consumers.class);
/**
* Returns the singleton instance.
*
* Note that initialise() must have been invoked first for this
* to return a valid instance.
*
* @return Consumers the singleton instance
*/
public static Consumers instance() {
return _instance;
}
/**
* Initialise the singleton instance
*
* @param Connection - the connection to use
* @return Consumers - singleton instance
* @throws PersistenceException - if the call cannot complete
*/
public static Consumers initialise(Connection connection)
throws PersistenceException {
if (_instance == null) {
synchronized (block) {
if (_instance == null) {
_instance = new Consumers();
_instance.load(connection);
}
}
}
return _instance;
}
/**
* Add a new durable consumer to the database if it does not already
* exist. A durable consumer is specified by a destination name and
* a consumer name.
* <p>
* The destination must resolve to a valid JmsDestination object
*
* @param connection - the database connection to use
* @param destination - the name of the destination
* @param consumer - the name of the consumer
* @throws PersistenceException - if the consumer cannot be added
*/
public synchronized void add(Connection connection, String dest,
String consumer)
throws PersistenceException {
JmsDestination destination = null;
Destinations singleton = Destinations.instance();
long destinationId = 0;
synchronized (singleton) {
destination = singleton.get(dest);
if (destination == null) {
raise("add", consumer, dest, "destination is invalid");
}
destinationId = singleton.getId(dest);
}
// check that for a topic the consumer name is not the same as the
// destination name
if ((destination instanceof JmsTopic) &&
(consumer.equals(dest))) {
raise("add", consumer, dest,
"The consumer name and destination name cannot be the same");
}
// get the next id from the seed table
long consumerId = SeedGenerator.instance().next(connection,
CONSUMER_ID_SEED);
PreparedStatement insert = null;
try {
insert = connection.prepareStatement(
"insert into consumers values (?,?,?,?)");
long created = (new Date()).getTime();
insert.setString(1, consumer);
insert.setLong(2, destinationId);
insert.setLong(3, consumerId);
insert.setLong(4, created);
insert.executeUpdate();
Consumer map = new Consumer(consumer, consumerId,
destinationId, created);
// check to see if the durable consumer already exists. If it
// does then do not add it but signal and error
if (!_consumers.containsKey(consumer)) {
_consumers.put(consumer, map);
} else {
_log.error("Durable consumer with name " + consumer
+ " already exists.");
}
} catch (Exception exception) {
throw new PersistenceException(
"Failed to add consumer, destination=" + dest +
", name=" + consumer, exception);
} finally {
SQLHelper.close(insert);
}
}
/**
* Remove a consumer from the database. If the destination is of
* type queue then the destination name and the consumer name are
* identical.
*
* @param connection - the connection to use
* @param name - the consumer name
* @throws PersistenceException - if the consumer cannot be removed
*/
public synchronized void remove(Connection connection, String name)
throws PersistenceException {
PreparedStatement delete = null;
// locate the consumer
Consumer map = (Consumer) _consumers.get(name);
if (map == null) {
raise("remove", name, "consumer does not exist");
}
try {
delete = connection.prepareStatement(
"delete from consumers where name=?");
delete.setString(1, name);
delete.executeUpdate();
// now delete all the corresponding handles in the consumer
// message table
remove(CONSUMER_MESSAGE, map.consumerId, connection);
// remove the consumer from the local cache
_consumers.remove(name);
} catch (SQLException exception) {
throw new PersistenceException(
"Failed to remove consumer=" + name, exception);
} finally {
SQLHelper.close(delete);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -