Ejemplo n.º 1
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.");
        }
Ejemplo n.º 2
0
        private void InvokeInternal(ClientInvocation invocation, Address address = null, ClientConnection connection = null)
        {
            try
            {
                if (connection == null)
                {
                    if (address == null)
                    {
                        address = GetRandomAddress();
                    }
                    connection = GetConnection(address);
                    if (connection == null)
                    {
                        if (address != null && _client.GetClientClusterService().GetMember(address) == null)
                        {
                            throw new TargetNotMemberException(string.Format("Target {0} is not a member.", address));
                        }

                        //Create an async connection and send the invocation afterward.
                        _clientConnectionManager.GetOrConnectAsync(address).ContinueWith(t =>
                        {
                            if (t.IsFaulted)
                            {
                                HandleInvocationException(invocation, t.Exception.Flatten().InnerExceptions.First());
                            }
                            else
                            {
                                InvokeInternal(invocation, address, t.Result);
                            }
                        })
                        .ContinueWith(t =>
                        {
                            HandleInvocationException(invocation, t.Exception.Flatten().InnerExceptions.First());
                        }, TaskContinuationOptions.OnlyOnFaulted |
                                      TaskContinuationOptions.ExecuteSynchronously);
                        return;
                    }
                }
                //Sending Invocation via connection
                UpdateInvocation(invocation, connection);
                ValidateInvocation(invocation, connection);

                if (!TrySend(invocation, connection))
                {
                    //Sending failed.
                    if (_client.GetConnectionManager().Live)
                    {
                        throw new TargetDisconnectedException(connection.GetAddress(), "Error writing to socket.");
                    }
                    throw new HazelcastException("Client is shut down.");
                }
                //Successfully sent.
            }
            catch (Exception e)
            {
                HandleInvocationException(invocation, e);
            }
        }
        protected IFuture <IClientMessage> Invoke(ClientInvocation invocation, Address address = null)
        {
            try
            {
                if (address == null)
                {
                    address = GetRandomAddress();
                }

                // try to get an existing connection, if not establish a new connection asyncronously
                var connection = GetConnection(address);
                if (connection != null)
                {
                    Send(connection, invocation);
                }
                else
                {
                    _clientConnectionManager.GetOrConnectAsync(address).ContinueWith(t =>
                    {
                        if (t.IsFaulted)
                        {
                            var innerException = t.Exception.InnerExceptions.First();
                            HandleException(invocation, innerException);
                        }
                        else
                        {
                            try
                            {
                                Send(t.Result, invocation);
                            }
                            catch (Exception e)
                            {
                                HandleException(invocation, e);
                            }
                        }
                    });
                }
            }
            catch (Exception e)
            {
                HandleException(invocation, e);
            }
            return(invocation.Future);
        }