예제 #1
0
파일: nats.cs 프로젝트: d3sw/one-nats
        private Exception reconnect()
        {
            if (_conn != null)
            {
                return(null);
            }
            // reconnect
            Exception error = null;

            lock (_token)
            {
                if (_conn != null)
                {
                    return(null);
                }
                // create a new clientID
                _clientID = string.Format("{0}-{1}", _serviceID, Guid.NewGuid());
                // fields
                var fields = getConnLogFields();
                // now create a new connect
                var opts = StanOptions.GetDefaultOptions();
                opts.NatsURL = _serverURL;
                try
                {
                    // reset event
                    _publishAbort.Reset();
                    // reconnect
                    var conn = nats.DefaultFactory.CreateConnection(_clusterID, _clientID, opts);
                    if (conn == null)
                    {
                        throw new ApplicationException(string.Format("nats connection failed, conn==null"));
                    }
                    // save conn
                    _conn = conn;
                    // log info
                    logInfo(fields, "nats connection completed");
                    // resubscribe all
                    foreach (var item in _subs.Values)
                    {
                        IStanSubscription sub = null;
                        internalSubscribe(item.subject, item.queue, item.options, item.cb, out sub);
                        item.sub = sub;
                    }
                }
                catch (Exception ex)
                {
                    error = ex;
                }
            }
            // return
            return(error);
        }
예제 #2
0
파일: nats.cs 프로젝트: d3sw/one-nats
 private void internalClose()
 {
     lock (_token)
     {
         // close all subscription
         foreach (var pair in _subs)
         {
             Exception error = null;
             var       item  = pair.Value;
             if (item.sub != null)
             {
                 var fields = getSubLogFields(item.subject, item.queue, item.options);
                 try
                 {
                     item.sub.Close();
                 }
                 catch (Exception ex)
                 {
                     error = ex;
                 }
                 item.sub = null;
                 if (error != null)
                 {
                     fields["error"] = error;
                     logError(fields, "nats subscription close failed");
                 }
                 else
                 {
                     logInfo(fields, "nats subscription closed");
                 }
             }
         }
         // close the connection
         if (_conn != null)
         {
             try { _conn.Close(); } catch { }
             _conn = null;
             var fields = getConnLogFields();
             logInfo(fields, "nats connection closed");
         }
     }
 }