/**
*
* Copyright 2004 Protique Ltd
*
* 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.codehaus.activemq.service.boundedvm;
import java.util.Collections;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import javax.jms.JMSException;
import org.codehaus.activemq.broker.BrokerClient;
import org.codehaus.activemq.filter.AndFilter;
import org.codehaus.activemq.filter.DestinationMap;
import org.codehaus.activemq.filter.Filter;
import org.codehaus.activemq.filter.FilterFactory;
import org.codehaus.activemq.filter.FilterFactoryImpl;
import org.codehaus.activemq.filter.NoLocalFilter;
import org.codehaus.activemq.message.ActiveMQDestination;
import org.codehaus.activemq.message.ActiveMQMessage;
import org.codehaus.activemq.message.ConsumerInfo;
import org.codehaus.activemq.message.MessageAck;
import org.codehaus.activemq.message.util.MemoryBoundedQueue;
import org.codehaus.activemq.message.util.MemoryBoundedQueueManager;
import org.codehaus.activemq.service.MessageContainer;
import org.codehaus.activemq.service.MessageContainerManager;
import EDU.oswego.cs.dl.util.concurrent.ConcurrentHashMap;
import EDU.oswego.cs.dl.util.concurrent.SynchronizedBoolean;
/**
* A MessageContainerManager for transient topics
*
* @version $Revision: 1.11 $
*/
/**
* A manager of MessageContainer instances
*/
public class TransientTopicBoundedMessageManager implements MessageContainerManager {
private MemoryBoundedQueueManager queueManager;
private ConcurrentHashMap containers;
private DestinationMap destinationMap;
private FilterFactory filterFactory;
private SynchronizedBoolean started;
private Map destinations;
/**
* Constructor for TransientTopicBoundedMessageManager
*
* @param mgr
*/
public TransientTopicBoundedMessageManager(MemoryBoundedQueueManager mgr) {
this.queueManager = mgr;
this.containers = new ConcurrentHashMap();
this.destinationMap = new DestinationMap();
this.destinations = new ConcurrentHashMap();
this.filterFactory = new FilterFactoryImpl();
this.started = new SynchronizedBoolean(false);
}
/**
* start the manager
*
* @throws JMSException
*/
public void start() throws JMSException {
if (started.commit(false, true)) {
for (Iterator i = containers.values().iterator(); i.hasNext();) {
TransientTopicBoundedMessageContainer container = (TransientTopicBoundedMessageContainer) i.next();
container.start();
}
}
}
/**
* stop the manager
*
* @throws JMSException
*/
public void stop() throws JMSException {
if (started.commit(true, false)) {
for (Iterator i = containers.values().iterator(); i.hasNext();) {
TransientTopicBoundedMessageContainer container = (TransientTopicBoundedMessageContainer) i.next();
container.stop();
}
}
}
/**
* Add a consumer if appropiate
*
* @param client
* @param info
* @throws JMSException
*/
public synchronized void addMessageConsumer(BrokerClient client, ConsumerInfo info) throws JMSException {
ActiveMQDestination destination = info.getDestination();
if (destination.isTopic()) {
TransientTopicBoundedMessageContainer container = (TransientTopicBoundedMessageContainer) containers
.get(client);
if (container == null) {
MemoryBoundedQueue queue = queueManager.getMemoryBoundedQueue(client.toString());
container = new TransientTopicBoundedMessageContainer(client, queue);
containers.put(client, container);
if (started.get()) {
container.start();
}
}
container.addConsumer(createFilter(info), info);
destinationMap.put(destination,container);
String name = destination.getPhysicalName();
if (!destinations.containsKey(name)) {
destinations.put(name, destination);
}
}
}
/**
* @param client
* @param info
* @throws JMSException
*/
public synchronized void removeMessageConsumer(BrokerClient client, ConsumerInfo info) throws JMSException {
ActiveMQDestination destination = info.getDestination();
if (destination.isTopic()) {
TransientTopicBoundedMessageContainer container = (TransientTopicBoundedMessageContainer) containers
.get(client);
if (container != null) {
container.removeConsumer(info);
if (container.isInactive()) {
containers.remove(client);
container.close();
destinationMap.remove(destination, container);
}
// lets check if we've no more consumers for this destination
if (!hasConsumerFor(destination)) {
destinations.remove(destination.getPhysicalName());
}
}
}
}
/**
* Delete a durable subscriber
*
* @param clientId
* @param subscriberName
* @throws JMSException if the subscriber doesn't exist or is still active
*/
public void deleteSubscription(String clientId, String subscriberName) throws JMSException {
}
/**
* @param client
* @param message
* @throws JMSException
*/
public void sendMessage(BrokerClient client, ActiveMQMessage message) throws JMSException {
if (message != null && message.getJMSActiveMQDestination().isTopic()) {
Set set = destinationMap.get(message.getJMSActiveMQDestination());
for (Iterator i = set.iterator(); i.hasNext();) {
TransientTopicBoundedMessageContainer container = (TransientTopicBoundedMessageContainer) i.next();
container.targetAndDispatch(client,message);
}
}
}
/**
* @param client
* @param ack
* @throws JMSException
*
*/
public void acknowledgeMessage(BrokerClient client, MessageAck ack) throws JMSException {
}
/**
* @param client
* @param transactionId
* @param ack
* @throws JMSException
*
*/
public void acknowledgeTransactedMessage(BrokerClient client, String transactionId, MessageAck ack) throws JMSException {
}
/**
* @param client
* @param ack
* @throws JMSException
*
*/
public void redeliverMessage(BrokerClient client, MessageAck ack) throws JMSException {
}
/**
* @throws JMSException
*
*/
public void poll() throws JMSException {
}
/**
* A hook when the transaction is about to be commited; so apply all outstanding commands to the Journal if using a
* Journal (transaction log)
*
* @param client
* @param transactionId
* @throws JMSException
*/
public void commitTransaction(BrokerClient client, String transactionId) throws JMSException {
}
/**
* A hook when the transaction is about to be rolled back; so discard all outstanding commands that are pending to
* be written to the Journal
*
* @param client
* @param transactionId
*/
public void rollbackTransaction(BrokerClient client, String transactionId) {
}
/**
* For Transient topics - a MessageContainer maps on to the messages
* to be dispatched through a BrokerClient, not a destination
* @param physicalName
* @return the MessageContainer used for dispatching - always returns null
* @throws JMSException
*/
public MessageContainer getContainer(String physicalName) throws JMSException {
return null;
}
/**
* @return a map of all the destinations
*/
public Map getDestinations() {
return Collections.unmodifiableMap(destinations);
}
/**
* Create filter for a Consumer
*
* @param info
* @return the Fitler
* @throws javax.jms.JMSException
*/
protected Filter createFilter(ConsumerInfo info) throws JMSException {
Filter filter = filterFactory.createFilter(info.getDestination(), info.getSelector());
if (info.isNoLocal()) {
filter = new AndFilter(filter, new NoLocalFilter(info.getClientId()));
}
return filter;
}
protected boolean hasConsumerFor(ActiveMQDestination destination) {
for (Iterator i = containers.values().iterator(); i.hasNext();) {
TransientTopicBoundedMessageContainer container = (TransientTopicBoundedMessageContainer) i.next();
if (container.hasConsumerFor(destination)) {
return true;
}
}
return false;
}
}