All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.alachisoft.ncache.client.internal.caching.MessagingServiceImpl Maven / Gradle / Ivy

There is a newer version: 5.3.0
Show newest version
/*
 * 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.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);
    }

    private 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) 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);
        return pubSubManager.getOrCreateTopic(topicPair, TopicOperationType.Create, false);
    }


    @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);
    }

    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 addCacheNotificationListener(String key, CacheDataModificationListener listener, EnumSet eventTypes) throws CacheException {
        EventDataFilter eventDataFilter = EventDataFilter.None;
        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) throws CacheException {
        EventDataFilter eventDataFilter = EventDataFilter.None;
        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);
    }

    //endregion

    //region Un-Register Notification Operations

    @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 - 2024 Weber Informatics LLC | Privacy Policy