/// <summary> /// Finish close broker operation /// </summary> /// <param name="result">IAsyncResult instance</param> private void OnCloseBroker(IAsyncResult result) { object[] objArr = (object[])result.AsyncState; bool suspended = (bool)objArr[0]; int retry = (int)objArr[1]; BrokerManagementServiceClient client = (BrokerManagementServiceClient)objArr[2]; try { client.EndCloseBroker(result); } catch (Exception e) { TraceHelper.TraceEvent(this.sessionId, TraceEventType.Error, "[BrokerInfo] Failed to close broker: {0}\nRetryCount = {1}", e, retry); // Perform retry if retry>0 if (retry > 0) { retry--; BrokerManagementServiceClient newClient = this.CreateClient(); try { newClient.BeginCloseBroker(suspended, new AsyncCallback(this.callbackToCloseBroker), new object[] { suspended, retry, newClient }); } catch (Exception ex) { // Do not retry if BeginCloseBroker failed TraceHelper.TraceEvent(this.sessionId, TraceEventType.Error, "[BrokerInfo] Retry failed when calling BeginCloseBroker: {0}", ex); Utility.AsyncCloseICommunicationObject(newClient); } } else { // Kill broker worker process if it failed to close broker try { this.brokerProcess.KillBrokerProcess(); } catch (Exception ex) { TraceHelper.TraceError(this.sessionId, "[BrokerInfo] Failed to kill broker worker process: {0}", ex); } } } finally { Utility.AsyncCloseICommunicationObject(client); } }
/// <summary> /// Send a ctrl event to the broker process to close the broker /// </summary> /// <param name="suspended">indicating the suspended flag</param> public void CloseBroker(bool suspended) { BrokerManagementServiceClient client = this.CreateClient(); try { // Call CloseBroker asynchronously and don't care about result client.BeginCloseBroker(suspended, new AsyncCallback(this.callbackToCloseBroker), new object[] { suspended, RetryLimit, client }); } catch (Exception e) { TraceHelper.TraceEvent(this.sessionId, TraceEventType.Warning, "[BrokerInfo] Failed to close broker: {0}", e); Utility.AsyncCloseICommunicationObject(client); throw; } }
/// <summary> /// Attach to the broker /// </summary> public void Attach() { BrokerManagementServiceClient client = this.CreateClient(); try { client.Attach(); TraceHelper.TraceEvent(this.sessionId, TraceEventType.Information, "[BrokerInfo] Successfully attached to the broker."); } catch (Exception e) { TraceHelper.TraceEvent(this.sessionId, TraceEventType.Error, "[BrokerInfo] Attach failed: {0}", e); throw; } finally { Utility.AsyncCloseICommunicationObject(client); } }
/// <summary> /// Start broker process /// </summary> public void StartBroker() { bool failoverMode = false; if (Interlocked.Increment(ref this.retryCount) > 1) { // Bug 7150: Need to set attach to true when retrying failoverMode = true; this.brokerInfo.Attached = true; } if (this.customBroker == null || String.IsNullOrEmpty(this.customBroker.Executive)) { this.brokerProcess = this.pool.GetBrokerProcess(); } else { this.brokerProcess = CreateCustomBrokerProcess(this.customBroker); } // Log a trace mapping broker worker pid to session id. TraceHelper.TraceEvent( this.sessionId, TraceEventType.Information, "[BrokerInfo].StartBroker: Init broker worker {0} for session {1}.", this.brokerProcess.Id, this.sessionId); BrokerManagementServiceClient client = this.CreateClient(); try { this.result = client.Initialize(this.sessionStartInfo, this.brokerInfo); // Set broker's unique id to the initialization result when the process // is (re)started. this.result.BrokerUniqueId = this.UniqueId; this.brokerProcess.Exited += new EventHandler(this.BrokerProcess_Exited); } catch (Exception e) { TraceHelper.TraceEvent(this.sessionId, TraceEventType.Error, "[BrokerInfo] Failed to initialize broker: {0}", e.ToString()); // If in failover mode, close this broker and do not retry anymore if (failoverMode) { try { this.CloseBroker(true); } catch (Exception ex) { TraceHelper.TraceEvent(TraceEventType.Warning, "[BrokerInfo].StartBroker: Exception {0}", ex); } } throw; } finally { Utility.AsyncCloseICommunicationObject(client); } }