public bool PublishDistributed <T>(string topic, T eventMessage, DistributedConsumerType distributedConsumerType, string lable = "") { if (!SubscriptionManager.DistributedEventService.IsRunning) { throw new EventException("分布式事件处理服务未启动"); } WriteTrackLog("发布分布式事件通知", topic, lable, eventMessage); //WriteTrackLog("发布分布式事件通知,本地通知开始", topic, lable, eventMessage); var subscriptions = _subscriptionService.GetSubscriptions <T>(); subscriptions.ToList().ForEach(x => PublishToConsumer(x, lable, eventMessage)); //WriteTrackLog("发布分布式事件通知,本地通知完成", topic, lable, eventMessage); var consumerType = typeof(T); var objectName = consumerType.FullName; DistributedObjectNameAttribute objectNameAttr = consumerType.GetCustomAttribute <DistributedObjectNameAttribute>(); if (objectNameAttr != null && !string.IsNullOrEmpty(objectNameAttr.Name)) { objectName = objectNameAttr.Name; } var dEvent = new DistributedEvent(eventMessage, topic, lable, objectName); //WriteTrackLog(string.Format("发布分布式事件通知,请求远程通信开始,对象名:{0}", objectName), topic, lable, eventMessage); return(SubscriptionManager.EventTransport.SendEventMessage(dEvent, distributedConsumerType)); }
public bool SendEventMessage(DistributedEvent dEvent, DistributedConsumerType consumerType) { try { bool hasError = false; var list = GetConsumerAddress(dEvent.Topic); if (list.Count() == 0) { EventPublisher.WriteTrackLog(string.Format("发布分布式事件通知,没有检测到任何分布式订阅对象,通知已退出,对象名:{0}", dEvent.ObjectName), dEvent.Topic, dEvent.Lable, dEvent.GetValue()); return(true); } foreach (var item in list) { bool flag = false; var address = string.Format("{0}:{1}", GetIntranetIp(), this._port); if (item == address) { continue; } EventPublisher.WriteTrackLog(string.Format("发布分布式事件通知,请求远程通信,地址:{0},对象名:{1}", item, dEvent.ObjectName), dEvent.Topic, dEvent.Lable, dEvent.GetValue()); flag = SendEventMessage(dEvent, item); if (flag) { EventPublisher.WriteTrackLog(string.Format("发布分布式事件通知,请求远程通信完成,地址:{0},对象名:{1}", address, dEvent.ObjectName), dEvent.Topic, dEvent.Lable, dEvent.GetValue()); if (consumerType == DistributedConsumerType.OnlyOne) { break; } } else { hasError = true; } } EventPublisher.WriteTrackLog(string.Format("发布分布式事件通知,请求远程通信完成,对象名:{0}", dEvent.ObjectName), dEvent.Topic, dEvent.Lable, dEvent.GetValue()); return(hasError == true ? false : true); } catch (Exception ex) { EventPublisher.WriteTrackLog(string.Format("发布分布式事件通知,请求远程通信错误,对象名:{0}", dEvent.ObjectName), dEvent.Topic, dEvent.Lable, dEvent.GetValue(), ex); } return(false); }