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

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

There is a newer version: 5.3.0
Show newest version
package com.alachisoft.ncache.client.internal.caching;

import Alachisoft.NCache.Common.Threading.ThreadPool;
import com.alachisoft.ncache.runtime.caching.*;
import com.alachisoft.ncache.runtime.caching.messaging.DeliveryMode;
import com.alachisoft.ncache.runtime.caching.messaging.MessageReceivedListener;
import com.alachisoft.ncache.runtime.exceptions.CacheException;

import java.util.ArrayList;

public class TopicSubscriptionImpl implements TopicSubscription {

    private final TopicImpl topic;
    private final String subscriberId;
    private final MessageReceivedListener listener;
    private String _subscriptionName;
    private SubscriptionPolicyType _subscriptionPolicyType;
    private int count = 1;
    private long _expiration;
    private long _creationTime;

    private ArrayList messageReceivedListeners = new ArrayList<>();
    private DeliveryMode _deliveryMode;

    public TopicSubscriptionImpl(TopicImpl topic, String subscriptionName, SubscriptionPolicyType subscriptionPolicyType, MessageReceivedListener listener,  DeliveryMode deliveryMode) {
        this.topic = topic;
        this.listener = listener;
        this._subscriptionName = subscriptionName;
        this._subscriptionPolicyType = subscriptionPolicyType;
        messageReceivedListeners.add(listener);
        subscriberId = (new Shorter()).getGuidString();
        _deliveryMode = deliveryMode;
    }

    public TopicSubscriptionImpl(TopicImpl topic, String subscriptionName, SubscriptionPolicyType subscriptionPolicyType, MessageReceivedListener listener){
        this(topic, subscriptionName, subscriptionPolicyType, listener, DeliveryMode.Sync);
    }

    @Override
    public Topic getTopic() {
        return topic;
    }

    public DeliveryMode getDeliveryMode()
    {
        return _deliveryMode;
    }

    @Override
    public void addMessageReceivedListener(MessageReceivedListener messageReceivedListener) throws CacheException {
        messageReceivedListeners.add(messageReceivedListener);
    }

    @Override
    public void removeMessageReceivedListener(MessageReceivedListener messageReceivedListener) throws CacheException {
        messageReceivedListeners.remove(messageReceivedListener);
    }

    public void OnMessageRecieved(MessageEventArgs args)
    {
        try
        {
            if (messageReceivedListeners != null && !messageReceivedListeners.isEmpty())
            {
                for (MessageReceivedListener t : messageReceivedListeners)
                {
                    if (_deliveryMode.equals(DeliveryMode.Sync)) {
                        t.onMessageReceived(this, args);
                    }else {
                        ThreadPool.getInstance().executeTask(new Runnable() {
                            @Override
                            public void run() {
                                t.onMessageReceived(this, args);
                            }
                        });
                    }
                }
            }
        }
        catch (RuntimeException ex)
        {
        }
    }

    public String getSubscriberId() {
        return subscriberId;
    }

    public String getSubscriptionName() {
        return _subscriptionName;
    }

    public SubscriptionPolicyType getSubscriptionPolicyType() {
        return _subscriptionPolicyType;
    }

    public long getExpiration() {
        return _expiration;
    }

    public void setExpiration(long value) {
        _expiration = value;
    }

    public long getCreationTime() {
        return _creationTime;
    }

    public void setCreationTime(long value) {
        _creationTime = value;
    }



    public void setSubscriptionPolicy(SubscriptionPolicyType subscriptionPolicyType) {
        _subscriptionPolicyType = subscriptionPolicyType;
    }

    public void UpdateCount() {
        count++;
    }

    @Override
    public final void unSubscribe() throws Exception {
        if (this.count == 1) {
            topic.unSubscribe(this, true);
        } else {
            this.count--;
        }
    }

    public void unSubscribeEventTopic() throws Exception {
        topic.unSubscribeEventTopic(this, true);
    }

    public void onMessageRecieved(MessageEventArgs args) {
        if (listener != null) {
            if (_deliveryMode.equals(DeliveryMode.Sync)) {
                listener.onMessageReceived(this, args);
            }
            else {
                ThreadPool.getInstance().executeTask(new Runnable() {
                    @Override
                    public void run() {
                        listener.onMessageReceived(this, args);
                    }
                });
            }
        }
    }

    public void dispose() {
        try {
            topic.unSubscribe(this, false);
        } catch (Exception ex) {

        }
    }


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy