📄 abstracttopicconsumerendpoint.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 2005 (C) Exoffice Technologies Inc. All Rights Reserved.
*
* $Id: AbstractTopicConsumerEndpoint.java,v 1.1 2005/03/18 03:58:39 tanderson Exp $
*/
package org.exolab.jms.messagemgr;
import java.rmi.RemoteException;
import java.sql.Connection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import javax.jms.InvalidSelectorException;
import javax.jms.JMSException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.exolab.jms.client.JmsDestination;
import org.exolab.jms.client.JmsTopic;
import org.exolab.jms.message.MessageImpl;
import org.exolab.jms.persistence.PersistenceException;
import org.exolab.jms.scheduler.Scheduler;
import org.exolab.jms.selector.Selector;
import org.exolab.jms.server.JmsServerSession;
/**
* A {@link ConsumerEndpoint} for topics.
*
* @author <a href="mailto:tma@netspace.net.au">Tim Anderson</a>
* @version $Revision: 1.1 $ $Date: 2005/03/18 03:58:39 $
*/
abstract class AbstractTopicConsumerEndpoint extends AbstractConsumerEndpoint
implements DestinationEventListener {
/**
* Cache of all handles for this consumer.
*/
private MessageQueue _handles = new MessageQueue();
/**
* Maintains a map of TopicDestinationCache that this endpoint subscribes
* to, keyed on JmsTopic. A wildcard subscription may point to more than
* one.
*/
protected Map _caches = Collections.synchronizedMap(new HashMap());
/**
* The maximum number of messages that a dispatch can deliver at any one
* time
*/
private final int MAX_MESSAGES = 200;
/**
* The logger
*/
private static final Log _log =
LogFactory.getLog(AbstractTopicConsumerEndpoint.class);
/**
* Construct a new <code>TopicConsumerEndpoint</code>.
* <p/>
* The destination and selector determine where it will be sourcing its
* messages from, and scheduler is used to asynchronously deliver messages
* to the consumer.
*
* @param consumerId the identity of this consumer
* @param session the owning session
* @param topic the topic to access
* @param selector the message selector. May be <code>null</code>
* @param noLocal if true, inhibits the delivery of messages published by
* its own connection.
* @param scheduler used to schedule asynchronous message delivery.
* @throws InvalidSelectorException if the selector is invalid
* @throws JMSException if the destination caches can't be
* constructed
*/
public AbstractTopicConsumerEndpoint(long consumerId,
JmsServerSession session,
JmsTopic topic,
String selector, boolean noLocal,
Scheduler scheduler)
throws JMSException {
super(consumerId, session, topic, selector, noLocal, scheduler);
}
/**
* Return the next available message to the client.
* <p/>
* The <code>wait</code> parameter indicates how many milliseconds to wait
* for a message before returning. If <code>wait</code> is <code>0</code>
* then do not wait. If <code>wait</code> is <code>-1</code> then wait
* indefinitely for the next message.
*
* @param wait number of milliseconds to wait
* @return the next message or <code>null</code>
* @throws JMSException for any error
*/
public MessageHandle receive(long wait) throws JMSException {
MessageHandle handle = receiveNoWait();
if ((handle == null) && (wait >= 0)) {
// no message is currently available for this
// consumer. So set the flag to indicate that
// the consumer is waiting for a message
setWaitingForMessage();
}
return handle;
}
/**
* Returns the first available message
*/
public MessageHandle receiveNoWait() throws JMSException {
MessageHandle handle = null;
while ((handle = _handles.removeFirst()) != null) {
// make sure we can still get access to the message
MessageImpl message = handle.getMessage();
if (message != null) {
if (selects(message)) {
// got a message which is applicable for the endpoint
break;
} else {
// this message has been filtered out so we can destroy
// the handle.
handle.destroy();
}
}
handle = null;
}
return handle;
}
/**
* Return a delivered, but unacknowledged message to the cache.
*
* @param handle the handle of the message to return
*/
public void returnMessage(MessageHandle handle) {
addMessage(handle);
// schedule if needed
schedule();
}
/**
* Return the number of unsent messages in the cache for this consumer.
*
* @return the number of unsent messages
*/
public int getMessageCount() {
return _handles.size();
}
/**
* This event is called when a non-persistent message is added to the
* <code>DestinationCache</code>.
*
* @param handle a handle to the message
* @param message the added message
* @return <code>true</code> if the listener accepted the message; otherwise
* <code>false</ode>
* @throws JMSException if the listener fails to handle the message
*/
public boolean messageAdded(MessageHandle handle, MessageImpl message)
throws JMSException {
boolean accepted = true;
// if the 'noLocal' indicator is set, and the message arrived on
// the same connection, ignore the message
if (getNoLocal() && message.getConnectionId() == getConnectionId()) {
accepted = false;
} else {
// create a message handle for this consumer
handle = new TopicConsumerMessageHandle(handle, this);
if (!_handles.contains(handle)) {
// if the message is not already in the cache then add it
addMessage(handle);
schedule();
} else {
accepted = false;
_log.warn("Endpoint=" + this + " already has message cached: " +
handle);
}
}
return accepted;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -