Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* This file is part of FFMQ.
*
* FFMQ is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* FFMQ is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with FFMQ; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
package net.timewalker.ffmq4.local.destination;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import javax.jms.DeliveryMode;
import javax.jms.JMSException;
import javax.jms.Topic;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import net.timewalker.ffmq4.FFMQException;
import net.timewalker.ffmq4.FFMQSubscriberPolicy;
import net.timewalker.ffmq4.common.message.AbstractMessage;
import net.timewalker.ffmq4.common.message.MessageSelector;
import net.timewalker.ffmq4.common.message.selector.SelectorIndexKey;
import net.timewalker.ffmq4.local.MessageLockSet;
import net.timewalker.ffmq4.local.destination.subscription.LocalTopicSubscription;
import net.timewalker.ffmq4.local.session.LocalMessageConsumer;
import net.timewalker.ffmq4.local.session.LocalSession;
import net.timewalker.ffmq4.management.destination.definition.TopicDefinition;
import net.timewalker.ffmq4.storage.data.DataStoreFullException;
import net.timewalker.ffmq4.storage.message.MessageSerializationLevel;
import net.timewalker.ffmq4.utils.Committable;
import net.timewalker.ffmq4.utils.ErrorTools;
import net.timewalker.ffmq4.utils.concurrent.SynchronizationBarrier;
/**
*
Implementation for a local JMS {@link Topic}
*/
public final class LocalTopic extends AbstractLocalDestination implements Topic, LocalTopicMBean
{
private static final Log log = LogFactory.getLog(LocalTopic.class);
// Definition
private TopicDefinition topicDef;
// Subscribers map
private Map subscriptionMap = new HashMap<>();
private List flatSubscriptions = new ArrayList<>();
private Map>> indexedSubscriptionMap = new HashMap<>();
private ReentrantReadWriteLock subscriptionsLock = new ReentrantReadWriteLock(); // Protects subscriptions, subscriptionMap & indexedSubscriptionMap
// Stats
private AtomicLong sentToTopicCount = new AtomicLong();
private AtomicLong dispatchedFromTopicCount = new AtomicLong();
// Runtime
private Set committables = new HashSet<>();
private boolean pendingChanges;
/**
* Constructor
*/
public LocalTopic( TopicDefinition topicDef )
{
super(topicDef);
this.topicDef = topicDef;
}
/**
* Get the queue definition
*/
public TopicDefinition getDefinition()
{
return topicDef;
}
/*
* (non-Javadoc)
* @see javax.jms.Topic#getTopicName()
*/
@Override
public String getTopicName()
{
return getName();
}
/* (non-Javadoc)
* @see net.timewalker.ffmq4.local.destination.AbstractLocalDestination#registerConsumer(net.timewalker.ffmq4.local.session.LocalMessageConsumer)
*/
@Override
public void registerConsumer(LocalMessageConsumer consumer)
{
super.registerConsumer(consumer);
subscriptionsLock.writeLock().lock();
try
{
LocalTopicSubscription subscription = subscriptionMap.remove(consumer.getSubscriberId());
if (subscription == null)
{
// New subscription
storeNewSubscription(consumer);
}
else
{
// Remove old subscription
removeSubscription(subscription);
// Replace by new subscription
storeNewSubscription(consumer);
}
}
finally
{
subscriptionsLock.writeLock().unlock();
}
}
private void storeNewSubscription(LocalMessageConsumer consumer)
{
LocalTopicSubscription subscription = new LocalTopicSubscription(consumer);
String[] partitionKeys = topicDef.getPartitionsKeysToIndex();
if (partitionKeys != null && subscription.getMessageSelector() != null)
{
List indexableKeys = subscription.getMessageSelector().getIndexableKeys();
SelectorIndexKey key = findBestMatch(partitionKeys, indexableKeys);
if (key != null)
{
subscription.setIndexKey(key);
addToIndexMap(subscription);
}
}
if (!subscription.isIndexed())
flatSubscriptions.add(subscription);
subscriptionMap.put(consumer.getSubscriberId(),subscription);
}
private void addToIndexMap( LocalTopicSubscription subscription )
{
SelectorIndexKey key = subscription.getIndexKey();
Map