
com.alachisoft.ncache.client.internal.caching.MessagingServiceImpl Maven / Gradle / Ivy
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.alachisoft.ncache.client.internal.caching;
import Alachisoft.NCache.Common.Enum.TopicOperationType;
import Alachisoft.NCache.Common.Threading.ThreadPool;
import Alachisoft.NCache.Management.Statistics.StatisticsCounter;
import com.alachisoft.ncache.client.*;
import com.alachisoft.ncache.client.internal.messaging.MessageManager;
import com.alachisoft.ncache.client.internal.messaging.TopicIdentity;
import com.alachisoft.ncache.client.services.MessagingService;
import com.alachisoft.ncache.runtime.caching.Topic;
import com.alachisoft.ncache.runtime.caching.messaging.TopicPriority;
import com.alachisoft.ncache.runtime.caching.messaging.TopicSearchOptions;
import com.alachisoft.ncache.runtime.events.EventDataFilter;
import com.alachisoft.ncache.runtime.events.EventType;
import com.alachisoft.ncache.runtime.exceptions.CacheException;
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.concurrent.FutureTask;
import static tangible.DotNetToJavaStringHelper.isNullOrEmpty;
class MessagingServiceImpl implements MessagingService {
private MessageManager pubSubManager;
private CacheImpl cacheContainer;
MessagingServiceImpl(EventManager eventManager, StatisticsCounter perfStatsCollector, CacheImpl cache) {
pubSubManager = new MessageManager(eventManager, perfStatsCollector);
pubSubManager.initialize();
cacheContainer = cache;
}
public MessageManager getPubSubManager() {
return pubSubManager;
}
public CacheImpl getCacheContainer() {
return cacheContainer;
}
public void setCacheContainer(CacheImpl cache) {
cacheContainer = cache;
}
//region Topic Related Operations
@Override
public Topic getTopic(String topicName) throws CacheException {
return getTopic(topicName, TopicSearchOptions.ByName);
}
@Override
public Topic getTopic(String topicName, TopicSearchOptions searchOptions) throws CacheException {
if (isNullOrEmpty(topicName)) {
throw new IllegalArgumentException("Value cannot be null or empty."+System.lineSeparator()+"Parameter name: topicName");
}
TopicIdentity topicPair = new TopicIdentity(topicName, searchOptions);
return pubSubManager.getOrCreateTopic(topicPair, searchOptions == TopicSearchOptions.ByName ? TopicOperationType.Get : TopicOperationType.GetPatternBased, false);
}
@Override
public Topic createTopic(String topicName, TopicPriority topicPriority) throws CacheException, IllegalArgumentException {
if (isNullOrEmpty(topicName)) {
throw new IllegalArgumentException("Value cannot be null or empty."+System.lineSeparator()+"Parameter name: topicName");
}
TopicIdentity topicPair = new TopicIdentity(topicName, TopicSearchOptions.ByName, topicPriority);
return pubSubManager.getOrCreateTopic(topicPair, TopicOperationType.Create, false);
}
@Override
public Topic createTopic(String topicName) throws CacheException, IllegalArgumentException {
return createTopic(topicName, TopicPriority.Normal);
}
@Override
public void deleteTopic(String topicName) throws CacheException {
if (topicName==null) {
throw new IllegalArgumentException("Value cannot be null."+System.lineSeparator()+"Parameter name: topicName");
}
if (topicName=="") {
throw new IllegalArgumentException("Value cannot be empty.\nParameter name: topicName");
}
pubSubManager.deleteTopic(topicName);
}
@Override
public FutureTask deleteTopicAsync(String topicName) throws CacheException {
if (topicName==null) {
throw new IllegalArgumentException("Value cannot be null."+System.lineSeparator()+"Parameter name: topicName");
}
if (topicName=="") {
throw new IllegalArgumentException("Value cannot be empty.\nParameter name: topicName");
}
FutureTask task = new FutureTask(() -> {
pubSubManager.deleteTopic(topicName);
return null;
});
ThreadPool.getInstance().submitTask(task);
return task;
}
public Topic getTopic(String topicName, boolean flag) throws CacheException {
TopicIdentity topicPair = new TopicIdentity(topicName, TopicSearchOptions.ByName);
return pubSubManager.getOrCreateTopic(topicPair, TopicOperationType.Get, flag);
}
protected void onReregisterTopic() {
try {
if (pubSubManager != null && pubSubManager.getReregisterTopicListener() != null) {
pubSubManager.onTopicReregisterListener();
}
} catch (Exception ex) {
}
}
//endregion
//region Register Notification Operations
@Override
public void registerCQ(ContinuousQuery query) throws CacheException {
if (query == null) {
throw new IllegalArgumentException("Value cannot be null."+System.lineSeparator()+"Parameter name: query");
}
if (!(query.notifyAddInternal() || query.notifyUpdateInternal() || query.notifyRemoveInternal())) {
throw new IllegalArgumentException("There is no event registered for this query. Please register at least one event (add/update/remove) before activating continuous query.");
}
cacheContainer.registerCQ(query);
cacheContainer.getCQManager().register(query, cacheContainer);
}
@Override
public void addCacheNotificationListener(String key, CacheDataModificationListener listener, EnumSet eventTypes, EventDataFilter eventDataFilter) throws CacheException {
if (key==null || key.isEmpty()) {
throw new IllegalArgumentException("Value cannot be null."+System.lineSeparator()+"Parameter name: key");
}
if (listener == null) {
throw new IllegalArgumentException("Value cannot be null."+System.lineSeparator()+"Parameter name: listener");
}
if (eventTypes == null) {
throw new IllegalArgumentException("Value cannot be null."+System.lineSeparator()+"Parameter name: eventTypes");
}
if (eventDataFilter == null) {
throw new IllegalArgumentException("Value cannot be null."+System.lineSeparator()+"Parameter name: eventDataFilter");
}
cacheContainer.registerCacheNotificationInternal(key, listener, eventTypes, eventDataFilter, true);
}
@Override
public void addCacheNotificationListener(Iterable keys, CacheDataModificationListener listener, EnumSet eventTypes, EventDataFilter eventDataFilter) throws CacheException {
if (keys == null) {
throw new IllegalArgumentException("Value cannot be null."+System.lineSeparator()+"Parameter name: keys");
}
ArrayList stringArray = new ArrayList<>();
for (String key : keys) {
if (key==null) {
throw new IllegalArgumentException("Value cannot be null."+System.lineSeparator()+"Parameter name: key");
}
if(key.isEmpty())
{
throw new IllegalArgumentException("Value cannot be Empty."+System.lineSeparator()+"Parameter name: key");
}
stringArray.add(key);
}
String[] keysList = new String[0];
keysList = (String[]) stringArray.toArray(keysList);
if (listener == null) {
throw new IllegalArgumentException("Value cannot be null."+System.lineSeparator()+"Parameter name: listener");
}
if (eventTypes == null) {
throw new IllegalArgumentException("Value cannot be null."+System.lineSeparator()+"Parameter name: eventTypes");
}
if (eventDataFilter == null) {
throw new IllegalArgumentException("Value cannot be null."+System.lineSeparator()+"Parameter name: eventDataFilter");
}
cacheContainer.addCacheDataNotificationListener(keysList, listener, eventTypes, eventDataFilter, true);
}
@Override
public CacheEventDescriptor addCacheNotificationListener(CacheDataModificationListener listener, EnumSet eventTypes, EventDataFilter eventDataFilter) throws CacheException {
if (listener == null) {
throw new IllegalArgumentException("Value cannot be null."+System.lineSeparator()+"Parameter name: listener");
}
if (eventTypes == null) {
throw new IllegalArgumentException("Value cannot be null."+System.lineSeparator()+"Parameter name: eventType");
}
if (eventDataFilter == null) {
throw new IllegalArgumentException("Value cannot be null."+System.lineSeparator()+"Parameter name: eventDataFilter");
}
return cacheContainer.registerCacheNotificationInternal(null, listener, eventTypes, eventDataFilter, true);
}
//endregion
//region Un-Register Notification Operations
@Override
public void unRegisterCQ(ContinuousQuery query) throws Exception {
if (query == null) {
throw new IllegalArgumentException("Value cannot be null."+System.lineSeparator()+"Parameter name: query");
}
cacheContainer.removeCQ(query);
cacheContainer.getCQManager().unRegister(query);
}
@Override
public void removeCacheNotificationListener(CacheEventDescriptor descriptor) throws Exception {
if (descriptor == null) {
throw new IllegalArgumentException("Value cannot be null."+System.lineSeparator()+"Parameter name: descriptor");
}
cacheContainer.removeCacheNotification(descriptor);
}
@Override
public void removeCacheNotificationListener(String key, CacheDataModificationListener listener, EnumSet eventTypes) throws CacheException {
if (key == null) {
throw new IllegalArgumentException("Value cannot be null."+System.lineSeparator()+"Parameter name: key");
}
if(key.isEmpty())
throw new IllegalArgumentException("Value cannot be empty.\nParameter name: key");
if (listener == null) {
throw new IllegalArgumentException("Value cannot be null."+System.lineSeparator()+"Parameter name: listener");
}
if (eventTypes == null) {
throw new IllegalArgumentException("Value cannot be null."+System.lineSeparator()+"Parameter name: eventTypes");
}
cacheContainer.removeCacheNotification(key, listener, eventTypes);
}
@Override
public void removeCacheNotificationListener(Iterable keys, CacheDataModificationListener listener, EnumSet eventTypes) throws CacheException {
if (keys == null) {
throw new IllegalArgumentException("Value cannot be null."+System.lineSeparator()+"Parameter name: keys");
}
ArrayList stringArray = new ArrayList<>();
for (String key : keys) {
if (key==null) {
throw new IllegalArgumentException("Value cannot be null."+System.lineSeparator()+"Parameter name: key");
}
if(key.isEmpty())
throw new IllegalArgumentException("Value cannot be empty.\nParameter name: key");
stringArray.add(key);
}
String[] keysList = new String[0];
keysList = (String[]) stringArray.toArray(keysList);
if (listener == null) {
throw new IllegalArgumentException("Value cannot be null."+System.lineSeparator()+"Parameter name: listener");
}
if (eventTypes == null) {
throw new IllegalArgumentException("Value cannot be null."+System.lineSeparator()+"Parameter name: eventTypes");
}
cacheContainer.removeCacheNotification(keysList, listener, eventTypes);
}
void OnReregisterTopic() {
try {
if (pubSubManager != null && pubSubManager.getReregisterTopicListener() != null) {
pubSubManager.onTopicReregisterListener();
}
} catch (Exception ex) {
}
}
//endregion
//region IDisposable Impl
protected void dispose() {
pubSubManager.dispose();
}
//endregion
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy