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

com.alachisoft.ncache.client.internal.communication.BulkEventStructure Maven / Gradle / Ivy

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

import Alachisoft.NCache.Caching.EventId;
import Alachisoft.NCache.Caching.OpCode;
import Alachisoft.NCache.Common.Net.Address;
import Alachisoft.NCache.Common.Threading.AsyncProcessor;
import Alachisoft.NCache.Management.Statistics.StatisticsCounter;
import Alachisoft.NCache.Persistence.EventType;
import com.alachisoft.ncache.client.ClientInfo;
import com.alachisoft.ncache.client.internal.asynctasks.ClientConnectivityChangeTask;
import com.alachisoft.ncache.client.ConnectivityStatus;
import com.alachisoft.ncache.client.internal.caching.EventTypeInternal;
import com.alachisoft.ncache.common.protobuf.BulkEventItemResponseProtocol;
import com.alachisoft.ncache.common.protobuf.ClientConnectivityChangeEventResponseProtocol;
import com.alachisoft.ncache.common.protobuf.DSUpdatedCallbackResultProtocol;
import com.alachisoft.ncache.runtime.exceptions.OperationFailedException;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.List;

public class BulkEventStructure implements Runnable {

    public Address remoteServerAddress = null;
    public List bulkEventList;
    public Broker parent;
    public StatisticsCounter _perfStatsCollector;
    boolean _notifyAsync;
    private AsyncProcessor _eventProcessor = null;

    public BulkEventStructure(Address remoteServerAddress, List bulkEventList, Broker parent, StatisticsCounter _perfStatsCollector, AsyncProcessor _eventProcessor, boolean _notifyAsync) {
        this.remoteServerAddress = remoteServerAddress;
        this.bulkEventList = bulkEventList;
        this.parent = parent;
        this._perfStatsCollector = _perfStatsCollector;
        this._eventProcessor = _eventProcessor;
        this._notifyAsync = _notifyAsync;
    }

    @Override
    public void run() {
        RaiseBulkEvent();
    }

    private void RaiseBulkEvent() {
        try {

            for (BulkEventItemResponseProtocol.BulkEventItemResponse eventItem : bulkEventList) {
                try {
                    if (_perfStatsCollector != null) {
                        _perfStatsCollector.incrementEventsTriggeredPerSeconds();
                    }
                    EventId eventId = new Alachisoft.NCache.Caching.EventId();

                    switch (eventItem.getEventType()) {
                        case POLL_NOTIFY_EVENT:
                            parent.getCache().getEventsListener().OnPollNotified((short) eventItem.getPollNotifyEvent().getCallbackId(),EventTypeInternal.forValue(eventItem.getPollNotifyEvent().getEventType()) );
                            break;

                        case CACHE_CLEARED_EVENT:

                            eventId = new Alachisoft.NCache.Caching.EventId();
                            eventId.setEventUniqueID( eventItem.getCacheClearedEvent().getEventId().getEventUniqueId());
                            eventId.setEventCounter(eventItem.getCacheClearedEvent().getEventId().getEventCounter());
                            eventId.setOperationCounter(eventItem.getCacheClearedEvent().getEventId().getOperationCounter());
                            eventId.setEventType(EventType.CACHE_CLEARED_EVENT);

                            if (_notifyAsync)
                            {
                                parent.getCache().getEventsListener().OnCacheCleared(_notifyAsync);
                            }

                            break;

                        case  DS_UPDATED_CALLBACK:
                            int _opCode = eventItem.getDSUpdatedCallback().getOpCode();
                            OpCode operationCode = OpCode.forValue(_opCode);
                            com.alachisoft.ncache.runtime.events.EventType eventType =  com.alachisoft.ncache.runtime.events.EventType.getEventType(_opCode);
                            java.util.HashMap resultDic = new java.util.HashMap();


                        for (DSUpdatedCallbackResultProtocol.DSUpdatedCallbackResult result : eventItem.getDSUpdatedCallback().getResultList())
                        {

                        }

                        if (_notifyAsync)
                        {
                            if (parent.getCache() != null && parent.getCache().getEventsListener() != null)
                            {
                                parent.getCache().getAsyncEventListener().onDataSourceUpdated((short)eventItem.getDSUpdatedCallback().getCallbackId(), resultDic,operationCode, _notifyAsync);
                            }
                        }

                        case CLIENT_CONNECTIVITY:

                            ClientConnectivityChangeEventResponseProtocol.ClientConnectivityChangeEventResponse eventResponse = eventItem.getClientConnectivityChangeEvent();
                            com.alachisoft.ncache.common.protobuf.ClientInfoProtocol.ClientInfo clientInfoProto = eventResponse.getClientInfo();
                            ClientInfo clientInfo = new ClientInfo();
                            clientInfo.setProcessID(clientInfoProto.getProcessId());
                            clientInfo.setAppName(clientInfoProto.getAppName());
                            clientInfo.setClientID(clientInfoProto.getClientId());
                            clientInfo.setMachineName(clientInfoProto.getMachineName());
                            try {
                                clientInfo.setiPAddress(InetAddress.getByName(clientInfoProto.getIpAddress()));
                            } catch (UnknownHostException e) {
                            }
                            clientInfo.setStatus(ConnectivityStatus.forValue(eventResponse.getStatus()));
                            if (_notifyAsync) {
                                if (parent != null && parent.getCache() != null && parent.getCache().getEventsListener() != null) {
                                    parent.getCache().getEventsListener().OnClientConnectivityChange(eventResponse.getCacheId(), clientInfo, ConnectivityStatus.forValue(eventResponse.getStatus()));
                                }
                            } else {
                                _eventProcessor.Enqueue(new ClientConnectivityChangeTask(parent, eventResponse.getCacheId(), clientInfo, ConnectivityStatus.forValue(eventResponse.getStatus())));
                            }
                            break;

                    }
                } catch (RuntimeException ex) {
                    if (parent.getLogger().getIsErrorLogsEnabled()) {
                        parent.getLogger().getNCacheLog().Error("Broker.RaiseBulkEvent", "An error occurred while raising bulk event of type : " + eventItem.getEventType() + ". Error :" + ex.toString());
                    }
                }            }
        } catch (RuntimeException ex) {
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy