예제 #1
0
        internal void ListenMembershipEvents(Address ownerConnectionAddress)
        {
            if (Logger.IsFinestEnabled())
            {
                Logger.Finest("Starting to listen for membership events from " + ownerConnectionAddress);
            }
            _initialListFetched = new ManualResetEventSlim();
            try
            {
                var clientMessage = ClientAddMembershipListenerCodec.EncodeRequest(false);
                DistributedEventHandler handler = m => ClientAddMembershipListenerCodec.EventHandler
                                                  .HandleEvent(m, HandleMember, HandleMemberCollection, HandleMemberAttributeChange);

                try
                {
                    var connection = _connectionManager.GetConnection(ownerConnectionAddress);
                    if (connection == null)
                    {
                        throw new InvalidOperationException(
                                  "Can not load initial members list because owner connection is null. Address "
                                  + ownerConnectionAddress);
                    }
                    var invocationService = (ClientInvocationService)_client.GetInvocationService();
                    var future            = invocationService.InvokeListenerOnConnection(clientMessage, handler, connection);
                    var response          = ThreadUtil.GetResult(future);
                    //registration id is ignored as this listener will never be removed
                    var registirationId = ClientAddMembershipListenerCodec.DecodeResponse(response).response;
                    WaitInitialMemberListFetched();
                }
                catch (Exception e)
                {
                    throw ExceptionUtil.Rethrow(e);
                }
            }
            catch (Exception e)
            {
                if (_client.GetLifecycleService().IsRunning())
                {
                    if (Logger.IsFinestEnabled())
                    {
                        Logger.Warning("Error while registering to cluster events! -> " + ownerConnectionAddress, e);
                    }
                    else
                    {
                        Logger.Warning("Error while registering to cluster events! -> " + ownerConnectionAddress +
                                       ", Error: " + e);
                    }
                }
            }
        }
예제 #2
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();
                        }
                    });
                }
            }
        }
예제 #3
0
        internal void TrySyncConnectToAllConnections()
        {
            if (!IsSmart)
            {
                return;
            }
            long timeLeftMillis = ((ClientInvocationService)_client.GetInvocationService()).InvocationTimeoutMillis;

            do
            {
                // Define the cancellation token.
                using (var source = new CancellationTokenSource())
                {
                    var token = source.Token;
                    var clientClusterService = _client.GetClientClusterService();
                    var tasks = clientClusterService.GetMemberList().Select(member => Task.Factory.StartNew(() =>
                    {
                        try
                        {
                            _connectionManager.GetOrConnectAsync(member.GetAddress()).Wait(token);
                        }
                        catch (Exception)
                        {
                            // if an exception occur cancel the process
                            source.Cancel();
                        }
                    }, token)).ToArray();

                    var start = Clock.CurrentTimeMillis();
                    try
                    {
                        if (Task.WaitAll(tasks, (int)timeLeftMillis, token))
                        {
                            //All succeed
                            return;
                        }
                    }
                    catch (Exception)
                    {
                        //waitAll did not completed
                    }
                    timeLeftMillis -= Clock.CurrentTimeMillis() - start;
                }
            } while (_client.GetLifecycleService().IsRunning() && timeLeftMillis > 0);
            throw new TimeoutException("Registering listeners is timed out.");
        }