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

Alachisoft.NCache.Common.Messaging.MessageMetaData Maven / Gradle / Ivy

There is a newer version: 5.3.3
Show newest version
package Alachisoft.NCache.Common.Messaging;

//  Copyright (c) 2019 Alachisoft
//
//  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
import Alachisoft.NCache.Common.Enum.*;
import com.alachisoft.ncache.runtime.caching.DeliveryOption;
import com.alachisoft.ncache.runtime.util.NCDateTime;
import com.alachisoft.ncache.serialization.core.io.ICompactSerializable;
import com.alachisoft.ncache.serialization.core.io.NCacheObjectInput;
import com.alachisoft.ncache.serialization.core.io.NCacheObjectOutput;

import java.io.IOException;

public class MessageMetaData implements Cloneable, ICompactSerializable
{
    private String _messageId;
    private java.util.HashSet _recepientList;
    private java.util.HashSet _subscriptons;
    private java.util.HashSet _durableSubscriptions;
    private java.util.ArrayList _recipientIdentifierList;

    private Object _mutex = new Object();

    public MessageMetaData(String messageId)
    {
        _messageId = messageId;
        _recepientList = new java.util.HashSet();
        _subscriptons = new java.util.HashSet();
        _durableSubscriptions = new java.util.HashSet();
        _recipientIdentifierList = new java.util.ArrayList();
    }


    public MessageMetaData()
    {
        _recepientList = new java.util.HashSet();
    }

    public final java.util.HashSet getRecepientList()
    {
        synchronized (_mutex)
        {
            return _recepientList;
        }
    }

    public final String getMessageId()
    {
        return _messageId;
    }

    public final java.util.HashSet getSubscriptions()
    {
        synchronized (_mutex)
        {
            return _subscriptons;
        }
    }
    public final boolean getIsRemovable()
    {
        //lock (_recepientList)
        synchronized (_mutex)
        {
            return _recepientList.isEmpty();
        }
    }
    public final void AddDurableSubscription(SubscriptionIdentifier subIdentifier)
    {
        synchronized (_mutex)
        {
            if (!_durableSubscriptions.contains(subIdentifier))
            {
                _durableSubscriptions.add(subIdentifier);
            }
        }

    }

    public final java.util.HashSet GetDurableSubscriptions()
    {
        java.util.HashSet subIdList = new java.util.HashSet();
        synchronized (_mutex)
        {
            for (SubscriptionIdentifier item : _durableSubscriptions)
            {
                subIdList.add((SubscriptionIdentifier)item.clone());
            }
            return subIdList;
        }
    }

    public final void AddToRecepientList(SubscriptionIdentifier subscriptionIdentifier)
    {
        synchronized (_mutex)
        {
            if (!_recipientIdentifierList.contains(subscriptionIdentifier))
            {
                _recipientIdentifierList.add(subscriptionIdentifier);
            }
        }

    }

    public final void RemoveFromReciepientList(SubscriptionIdentifier subscriptionIdentifier)
    {
        synchronized (_mutex)
        {
            if (_recipientIdentifierList != null)
            {
                _recipientIdentifierList.remove(subscriptionIdentifier);
            }
            if (_durableSubscriptions != null)
            {
                _durableSubscriptions.remove(subscriptionIdentifier);
            }
        }
    }
    private long privateExpirationTime;
    public final long getExpirationTime()
    {
        return privateExpirationTime;
    }
    public final void setExpirationTime(long value)
    {
        privateExpirationTime = value;
    }

    private double privateTimeToLive;
    public final double getTimeToLive()
    {
        return privateTimeToLive;
    }
    public final void setTimeToLive(double value)
    {
        privateTimeToLive = value;
    }

    private java.util.Date privateAssigmentTime;
    public final java.util.Date getAssigmentTime()
    {
        return privateAssigmentTime;
    }
    public final void setAssigmentTime(java.util.Date value)
    {
        privateAssigmentTime = value;
    }

    private String privateTopicName;
    public final String getTopicName()
    {
        return privateTopicName;
    }
    public final void setTopicName(String value)
    {
        privateTopicName = value;
    }

    private DeliveryOption privateDeliveryOption;
    public final DeliveryOption getDeliveryOption()
    {
        return privateDeliveryOption;
    }
    public final void setDeliveryOption(DeliveryOption value)
    {
        privateDeliveryOption = value;
    }

    private boolean privateIsAssigned;
    public final boolean getIsAssigned()
    {
        return privateIsAssigned;
    }
    public final void setIsAssigned(boolean value)
    {
        privateIsAssigned = value;
    }

    private boolean privateIsNotify;
    public final boolean getIsNotify()
    {
        return privateIsNotify;
    }
    public final void setIsNotify(boolean value)
    {
        privateIsNotify = value;
    }

    private MessageFailureReason privateMessageFailureReason;
    public final MessageFailureReason getMessageFailureReason()
    {
        return privateMessageFailureReason;
    }
    public final void setMessageFailureReason(MessageFailureReason value)
    {
        privateMessageFailureReason = value;
    }

    private SubscriptionType privateSubscriptionType;
    public final SubscriptionType getSubscriptionType()
    {
        return privateSubscriptionType;
    }
    public final void setSubscriptionType(SubscriptionType value)
    {
        privateSubscriptionType = value;
    }
/*
    public final int getSize()
    {
        return MemoryUtil.NetLongSize + 2 * MemoryUtil.NetIntSize + MemoryUtil.NetDateTimeSize + 3 * MemoryUtil.NetByteSize + 3 * MemoryUtil.NetEnumSize + MemoryUtil.GetStringSize(getTopicName()) + MemoryUtil.GetStringSize(getMessageId());
    }

    public final int getInMemorySize()
    {
        return MemoryUtil.NetLongSize + 2 * MemoryUtil.NetIntSize + MemoryUtil.NetDateTimeSize + 3 * MemoryUtil.NetByteSize + 3 * MemoryUtil.NetEnumSize + MemoryUtil.GetStringSize(getTopicName()) + MemoryUtil.GetStringSize(getMessageId());
    }
*/
    public final boolean getDelivered()
    {
        synchronized (_mutex)
        {
            if (getIsAssigned())
            {
                return _subscriptons.isEmpty() && _recepientList.isEmpty() && _durableSubscriptions.isEmpty();
            }
            return false;
        }
    }
    private java.util.Date privateAbsoluteExpiratoinTime;
    public final java.util.Date getAbsoluteExpiratoinTime()
    {
        return privateAbsoluteExpiratoinTime;
    }
    private void setAbsoluteExpiratoinTime(java.util.Date value)
    {
        privateAbsoluteExpiratoinTime = value;
    }
    private boolean privateDeliveryFailed;
    public final boolean getDeliveryFailed()
    {
        return privateDeliveryFailed;
    }
    public final void setDeliveryFailed(boolean value)
    {
        privateDeliveryFailed = value;
    }
    private boolean privateEverAcknowledged;
    public final boolean getEverAcknowledged()
    {
        return privateEverAcknowledged;
    }
    public final void setEverAcknowledged(boolean value)
    {
        privateEverAcknowledged = value;
    }
    public final boolean getHasSubscriptions()
    {
        return _subscriptons.size() > 0;
    }

    public final void RemoveRecepient(String recepient)
    {
        synchronized (_mutex)
        {
            _recepientList.remove(recepient);
        }
    }

    public final void AddRecepient(String recepient)
    {
        synchronized (_mutex)
        {
            _recepientList.add(recepient);
        }
    }
    public final java.util.ArrayList getSubscriptionIdentifierList()
    {
        return _recipientIdentifierList;
    }
    public final void RegisterSubscription(SubscriptionListener subscriptionListener)
    {
        synchronized (_mutex)
        {
            if (!_subscriptons.contains(subscriptionListener))
            {
                if (getAssigmentTime() == null)
                {
                    setAssigmentTime(NCDateTime.getUTCNow());
                }
                setIsAssigned(true);
                _subscriptons.add(subscriptionListener);
            }
        }
    }

    public final void UnregisterSubscription(SubscriptionListener subscriptionListener)
    {
        synchronized (_mutex)
        {
            if (_subscriptons.contains(subscriptionListener))
            {
                _subscriptons.remove(subscriptionListener);
            }
        }
    }

    public final Object clone()
    {
        synchronized (_mutex)
        {
            MessageMetaData metaData = new MessageMetaData(getMessageId());
            metaData.setTimeToLive(getTimeToLive());
            metaData.setAssigmentTime(getAssigmentTime());
            metaData.setTopicName(getTopicName());
            metaData.setDeliveryOption(getDeliveryOption());
            metaData.setMessageFailureReason(getMessageFailureReason());
            metaData.setSubscriptionType(getSubscriptionType());
            metaData.setIsAssigned(getIsAssigned());
            metaData.setIsNotify(getIsNotify());
            metaData.setAbsoluteExpiratoinTime(getAbsoluteExpiratoinTime());
            metaData.setEverAcknowledged(getEverAcknowledged());
            metaData.setExpirationTime(getExpirationTime());
            metaData._recepientList = new java.util.HashSet(getRecepientList());
            metaData._subscriptons = new java.util.HashSet(_subscriptons);
            metaData._durableSubscriptions = new java.util.HashSet(_durableSubscriptions);
            metaData._recipientIdentifierList = new java.util.ArrayList(_recipientIdentifierList);
            return metaData;
        }

    }

    public final java.util.Date InitializeExpiration()
    {
        synchronized (_mutex)
        {
            NCDateTime dateTime=new NCDateTime(NCDateTime.getUTCNow());
            dateTime.addTicks(getExpirationTime());
            setAbsoluteExpiratoinTime(dateTime.getDate());
            return getAbsoluteExpiratoinTime();
        }

    }

    public final void OnMessageRemoved()
    {
        RevokeSubscriptions();
    }

    public final void RevokeSubscriptions()
    {
        synchronized (_mutex)
        {
            for (SubscriptionListener clientSubscriptionListener : _subscriptons)
            {
                clientSubscriptionListener.OnMessageRemoved(this.getMessageId());
            }
            _subscriptons.clear();
            _recepientList.clear();
        }

    }

    public final void RevokeSubscriptions(SubscriptionListener subscriptionListener)
    {
        if (_subscriptons.contains(subscriptionListener))
        {
            subscriptionListener.OnMessageRemoved(this.getMessageId());
        }
    }
/*
    public static MessageMetaData ReadMetaDataInfo(CompactReader reader)
    {
        boolean flag = reader.ReadBoolean();

        if (flag)
        {
            MessageMetaData messageMetaData = new MessageMetaData();
            messageMetaData.Deserialize(reader);
            return messageMetaData;
        }
        return null;
    }

    public static void WriteMetaDataInfo(CompactWriter writer, MessageMetaData messageMetaData)
    {
        if (messageMetaData == null)
        {
            writer.Write(false);
            return;
        }
        else
        {
            writer.Write(true);
            messageMetaData.Serialize(writer);
        }
    }

    public final void Deserialize(CompactReader reader)
    {
        _mutex = new Object();
        Object tempVar = reader.ReadObject();
        _messageId = (String)((tempVar instanceof String) ? tempVar : null);
        setTimeToLive(reader.ReadDouble());
        Object tempVar2 = reader.ReadObject();
        setAssigmentTime((java.util.Date)((tempVar2 instanceof java.util.Date) ? tempVar2 : null));
        Object tempVar3 = reader.ReadObject();
        setTopicName((String)((tempVar3 instanceof String) ? tempVar3 : null));
        setDeliveryOption((DeliveryOption)reader.ReadInt32());
        setMessageFailureReason((MessageFailureReason)reader.ReadInt32());
        setSubscriptionType((SubscriptionType)reader.ReadInt32());
        setIsAssigned(reader.ReadBoolean());
        setIsNotify(reader.ReadBoolean());
        Object tempVar4 = reader.ReadObject();
        setAbsoluteExpiratoinTime((java.util.Date)((tempVar4 instanceof java.util.Date) ? tempVar4 : null));
        setDeliveryFailed(reader.ReadBoolean());
        setEverAcknowledged(reader.ReadBoolean());
        setExpirationTime(reader.ReadInt64());
        _recepientList = SerializationUtility.DeserializeHashSet(reader);
        _subscriptons = new java.util.HashSet();
        _durableSubscriptions = SerializationUtility.DeserializeHashSet(reader);
        _recipientIdentifierList = SerializationUtility.DeserializeList(reader);

    }

    public final void Serialize(CompactWriter writer)
    {
        synchronized (_mutex)
        {
            writer.WriteObject(_messageId);
            writer.Write(getTimeToLive());
            writer.WriteObject(getAssigmentTime());
            writer.WriteObject(getTopicName());
            writer.Write((int)getDeliveryOption());
            writer.Write((int)getMessageFailureReason());
            writer.Write((int)getSubscriptionType());
            writer.Write(getIsAssigned());
            writer.Write(getIsNotify());
            writer.WriteObject(getAbsoluteExpiratoinTime());
            writer.Write(getDeliveryFailed());
            writer.Write(getEverAcknowledged());
            writer.Write(getExpirationTime());
            SerializationUtility.SerializeHashSet(_recepientList, writer);
            SerializationUtility.SerializeHashSet(_durableSubscriptions, writer);
            SerializationUtility.SerializeList(_recipientIdentifierList, writer);
        }

    }
*/
    @Override
    public void serialize(NCacheObjectOutput out) throws IOException {

    }

    @Override
    public void deserialize(NCacheObjectInput in) throws IOException, ClassNotFoundException {

    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy