public void HandleClientMessage(IClientMessage message)
 {
     if (message.IsFlagSet(ClientMessage.ListenerEventFlag))
     {
         object state     = message.GetPartitionId();
         var    eventTask = new Task(o =>
         {
             if (!_isShutDown)
             {
                 HandleEventMessage(message);
             }
         }, state);
         eventTask.Start(_taskScheduler);
     }
     else
     {
         _client.GetClientExecutionService().Submit(() =>
         {
             if (!_isShutDown)
             {
                 HandleResponseMessage(message);
             }
         });
     }
 }
        public void Start()
        {
            if (!_live.CompareAndSet(false, true))
            {
                return;
            }

            //start Heartbeat
            _heartbeatToken = new CancellationTokenSource();
            _client.GetClientExecutionService().ScheduleWithFixedDelay(Heartbeat,
                                                                       (long)_heartbeatInterval.TotalMilliseconds,
                                                                       (long)_heartbeatInterval.TotalMilliseconds,
                                                                       TimeUnit.Milliseconds,
                                                                       _heartbeatToken.Token);
        }
Exemple #3
0
        private bool RetryRequest(ClientInvocation invocation, Exception exception)
        {
            var retryNr = invocation.IncrementAndGetRetryCount();

            if (retryNr > _retryCount)
            {
                return(false);
            }

            Logger.Finest("Retry #" + retryNr + " for request " + invocation.Message);

            _client.GetClientExecutionService().SubmitWithDelay(() =>
            {
                if (_isShutDown)
                {
                    FailRequestDueToShutdown(invocation);
                }
                else
                {
                    // get the appropriate connection for retry
                    var connection = GetConnectionForInvocation(invocation);
                    Send(connection, invocation);
                }
            }, _retryWaitTime).ContinueWith(t =>
            {
                if (t.IsFaulted)
                {
                    var innerException = t.Exception.InnerExceptions.First();
                    Logger.Finest("Retry of request " + invocation.Message + " failed with ", innerException);
                    HandleException(invocation, innerException);
                }
            });
            return(true);
        }
Exemple #4
0
        public void Start()
        {
            if (_live)
            {
                return;
            }
            _live = true;

            //start Heartbeat
            _heartbeatToken = new CancellationTokenSource();
            _client.GetClientExecutionService().ScheduleWithFixedDelay(Heartbeat,
                                                                       _heartbeatInterval,
                                                                       _heartbeatInterval,
                                                                       TimeUnit.Milliseconds,
                                                                       _heartbeatToken.Token);
        }
 public void HandleClientMessage(IClientMessage message)
 {
     if (message.IsFlagSet(ClientMessage.ListenerEventFlag))
     {
         _clientListenerService.HandleResponseMessage(message);
     }
     else
     {
         _client.GetClientExecutionService().Submit(() =>
         {
             if (!_isShutDown)
             {
                 HandleResponseMessage(message);
             }
         });
     }
 }
Exemple #6
0
        public void ConnectionRemoved(ClientConnection connection)
        {
            var executionService = (ClientExecutionService)_client.GetClientExecutionService();

            if (Equals(connection.GetAddress(), OwnerConnectionAddress))
            {
                if (_client.GetLifecycleService().IsRunning())
                {
                    executionService.SubmitInternal(() =>
                    {
                        try
                        {
                            FireConnectionEvent(LifecycleEvent.LifecycleState.ClientDisconnected);
                            ConnectToCluster();
                        }
                        catch (Exception e)
                        {
                            Logger.Warning("Could not re-connect to cluster shutting down the client", e);
                            _client.GetLifecycleService().Shutdown();
                        }
                    });
                }
            }
        }
 public Task <ClientConnection> GetOrConnectAsync(Address address)
 {
     lock (_pendingConnections)
     {
         if (!_pendingConnections.ContainsKey(address))
         {
             var task = _client.GetClientExecutionService().Submit(() => GetOrConnect(address));
             task.ContinueWith(t =>
             {
                 lock (_pendingConnections)
                 {
                     _pendingConnections.Remove(address);
                 }
             });
             _pendingConnections[address] = task;
         }
         return(_pendingConnections[address]);
     }
 }
        public string AddDistributedObjectListener(IDistributedObjectListener listener)
        {
            var request = ClientAddDistributedObjectListenerCodec.EncodeRequest(false);
            var context = new ClientContext(_client.GetSerializationService(), _client.GetClientClusterService(),
                                            _client.GetClientPartitionService(), _client.GetInvocationService(), _client.GetClientExecutionService(),
                                            _client.GetListenerService(),
                                            this, _client.GetClientConfig());
            //EventHandler<PortableDistributedObjectEvent> eventHandler = new _EventHandler_211(this, listener);

            DistributedEventHandler eventHandler = delegate(IClientMessage message)
            {
                ClientAddDistributedObjectListenerCodec.AbstractEventHandler.Handle(message,
                                                                                    (name, serviceName, type) =>
                {
                    var ns = new ObjectNamespace(serviceName, name);
                    ClientProxy proxy;
                    _proxies.TryGetValue(ns, out proxy);
                    if (proxy == null)
                    {
                        proxy = GetProxy(serviceName, name);
                    }
                    var _event = new DistributedObjectEvent(type, serviceName, proxy);
                    if (DistributedObjectEvent.EventType.Created.Equals(type))
                    {
                        listener.DistributedObjectCreated(_event);
                    }
                    else
                    {
                        if (DistributedObjectEvent.EventType.Destroyed.Equals(type))
                        {
                            listener.DistributedObjectDestroyed(_event);
                        }
                    }
                });
            };

            //PortableDistributedObjectEvent
            return(context.GetListenerService()
                   .StartListening(request, eventHandler,
                                   m => ClientAddDistributedObjectListenerCodec.DecodeResponse(m).response));
        }
Exemple #9
0
        public string AddDistributedObjectListener(IDistributedObjectListener listener)
        {
            var isSmart = _client.GetClientConfig().GetNetworkConfig().IsSmartRouting();
            var request = ClientAddDistributedObjectListenerCodec.EncodeRequest(isSmart);
            var context = new ClientContext(_client.GetSerializationService(), _client.GetClientClusterService(),
                                            _client.GetClientPartitionService(), _client.GetInvocationService(), _client.GetClientExecutionService(),
                                            _client.GetListenerService(), _client.GetNearCacheManager(), this, _client.GetClientConfig());
            DistributedEventHandler eventHandler = delegate(IClientMessage message)
            {
                ClientAddDistributedObjectListenerCodec.EventHandler.HandleEvent(message, (name, serviceName, eventType) =>
                {
                    var _event = new LazyDistributedObjectEvent(eventType, serviceName, name, this);
                    switch (eventType)
                    {
                    case DistributedObjectEvent.EventType.Created:
                        listener.DistributedObjectCreated(_event);
                        break;

                    case DistributedObjectEvent.EventType.Destroyed:
                        listener.DistributedObjectDestroyed(_event);
                        break;

                    default:
                        Logger.Warning(string.Format("Undefined DistributedObjectListener event type received: {0} !!!",
                                                     eventType));
                        break;
                    }
                });
            };

            return(context.GetListenerService().RegisterListener(request,
                                                                 m => ClientAddDistributedObjectListenerCodec.DecodeResponse(m).response,
                                                                 ClientRemoveDistributedObjectListenerCodec.EncodeRequest, eventHandler));
        }
Exemple #10
0
 public void RefreshPartitions()
 {
     _client.GetClientExecutionService().Submit(() => { GetPartitions(); }).IgnoreExceptions();
 }