📄 adminconnection.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.
*
* Date Author Changes
* 04/07/2000 jima Created
*/
package org.exolab.jms.server;
import java.sql.Connection;
import java.util.Enumeration;
import java.util.Vector;
import javax.jms.JMSException;
import javax.transaction.TransactionManager;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.exolab.core.service.ServiceManager;
import org.exolab.jms.authentication.AuthenticationMgr;
import org.exolab.jms.authentication.User;
import org.exolab.jms.client.JmsDestination;
import org.exolab.jms.client.JmsQueue;
import org.exolab.jms.client.JmsTopic;
import org.exolab.jms.messagemgr.ConsumerEndpoint;
import org.exolab.jms.messagemgr.ConsumerManager;
import org.exolab.jms.messagemgr.DestinationCache;
import org.exolab.jms.messagemgr.DestinationManager;
import org.exolab.jms.persistence.DatabaseService;
import org.exolab.jms.persistence.SQLHelper;
import org.exolab.jms.util.UUID;
/**
* A connection is created for every adminclient connecting to the JmsServer.
*
* @version $Revision: 1.2.2.1 $ $Date: 2004/05/04 01:26:49 $
* @author <a href="mailto:knut@lerpold.no">Knut Lerpold</a>
* @see org.exolab.jms.server.AdminConnectionManager
*/
public class AdminConnection {
/**
* This is the client that is responsible for this connection. It should
* map back to a client-side entity
*/
private String _clientId = null;
/**
* The connection id is unique within the context of a JMS server
*/
private String _identifierId = null;
/**
* The logger
*/
private static final Log _log =
LogFactory.getLog(AdminConnection.class);
/**
* Instantiate an instance of this class for the specified client identity.
* Generates a unique identifier for this connection.
*
* @param id client identity
*/
AdminConnection(String id) {
_clientId = id;
_identifierId = UUID.next();
}
/**
* Returns the client identifier
*
* @return String the client identifier
*/
public String getClientId() {
return _clientId;
}
/**
* Returns the identifier
*
* @return String the identifierId
*/
public String getIdentifierId() {
return _identifierId;
}
/**
* Close the admin connection
*/
public void close() {
}
/**
* Determine whether the client is active
* Could e.g. disconnect the client if it has been inactive for a
* period of time.
*
* for now always return true
*
* @return boolean true if active; false otherwise
*/
boolean isClientActive() {
boolean active = true;
return active;
}
/**
* Return the number of messages for a durable consumer.
*
* @param topic name of the topic
* @param name consumer name
* @return int number of unsent or unacked messages
*/
public int getDurableConsumerMessageCount(String topic, String name) {
int count = -1;
Connection connection = null;
try {
// first see if the cache is loaded in memory
DestinationManager dmgr = DestinationManager.instance();
ConsumerManager cmgr = ConsumerManager.instance();
JmsDestination dest = dmgr.destinationFromString(topic);
ConsumerEndpoint endpoint = null;
if ((dest != null)
&& ((name != null)
|| (name.length() > 0))) {
endpoint = cmgr.getConsumerEndpoint(name);
if ((endpoint != null)
&& (endpoint.getDestination().equals(dest))) {
// retrieve the number of handles for the endpoint, which
// reflects the number of messages
count = endpoint.getMessageCount();
} else {
// there is no cache with this name stored in memory. If
// this is an administered destination then read the count
// directly from the database.
if (dmgr.isAdministeredDestination(topic)) {
connection = DatabaseService.getConnection();
count = DatabaseService.getAdapter().
getDurableConsumerMessageCount(connection, topic, name);
connection.commit();
}
}
}
} catch (Exception exception) {
_log.error("Failed to get message count for topic=" + topic,
exception);
SQLHelper.rollback(connection);
} finally {
SQLHelper.close(connection);
}
return count;
}
/**
* First use the destination manager to return the number of persistent and
* non-persistent messages in a queue
*
* @param queue name of the queue
* @return int - the number of messages for that destination or -1 if the
* destination is invalid
*/
public int getQueueMessageCount(String queue) {
int count = -1;
Connection connection = null;
try {
// first see if the cache is loaded in memory
DestinationManager mgr = DestinationManager.instance();
JmsDestination dest = mgr.destinationFromString(queue);
DestinationCache cache = null;
if (dest != null) {
cache = mgr.getDestinationCache(dest);
if (cache != null) {
// retrieve the number of handles for the cache, which
// reflects the number of messages
count = cache.getMessageCount();
} else {
// there is no cache with this name stored in memory. If
// this is an administered destination then read the count
// directly from the database.
if (mgr.isAdministeredDestination(queue)) {
connection = DatabaseService.getConnection();
count = DatabaseService.getAdapter().
getQueueMessageCount(connection, queue);
connection.commit();
}
}
}
} catch (Exception exception) {
_log.error("Failed to get message count for queue=" + queue,
exception);
SQLHelper.rollback(connection);
} finally {
SQLHelper.close(connection);
}
return count;
}
/**
* Add the specified durable consumer to the database
*
* @param topic name of the destination
* @param name name of the consumer
* @return boolean true if successful
*/
public boolean addDurableConsumer(String topic, String name) {
boolean result = false;
try {
ConsumerManager.instance().createDurableConsumer(
new JmsTopic(topic), name);
result = true;
} catch (JMSException exception) {
_log.error("Failed to add durable consumer=" + name
+ " for topic=" + topic, exception);
}
return result;
}
/**
* Remove the consumer attached to the specified destination and with
* the passed in name
*
* @param name name of the consumer
* @return boolean true if successful
*/
public boolean removeDurableConsumer(String name) {
boolean result = false;
try {
ConsumerManager.instance().removeDurableConsumer(name);
result = true;
} catch (JMSException exception) {
_log.error("Failed to remove durable consumer=" + name, exception);
}
return result;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -