public void ClearAddressSubscriptions(Address address) { using (var connection = _connectionFactory.OpenConnection()) { var subscriberAddress = address.ToString(); var table = Configuration.Instance.GetSetting<string>("subscriptionTable"); connection.Delete(new { SubscriberAddress = subscriberAddress }, table); _memoryStore.ClearAddressSubscriptions(address); _logger.DebugFormat("Cleaned up subscriptions of subscriber address '{0}'", subscriberAddress); } }
public void Subscribe(Address address, Type messageType) { using (var connection = _connectionFactory.OpenConnection()) { var subscriberAddress = address.ToString(); var messageTypeName = messageType.AssemblyQualifiedName; var table = Configuration.Instance.GetSetting<string>("subscriptionTable"); var count = connection.GetCount(new { SubscriberAddress = subscriberAddress, MessageType = messageTypeName }, table); if (count == 0) { connection.Insert(new { UniqueId = Guid.NewGuid(), SubscriberAddress = subscriberAddress, MessageType = messageTypeName }, table); _memoryStore.Subscribe(address, messageType); _logger.DebugFormat("Subscriber '{0}' subscribes message '{1}'.", subscriberAddress, messageTypeName); } } }
public void ClearAddressSubscriptions(Address address) { using (var session = _sessionFactory.OpenSession()) { var criteria = session.CreateCriteria<Subscription>(); var subscriberAddress = address.ToString(); criteria.Add(Expression.Eq("SubscriberAddress", subscriberAddress)); var subscriptions = criteria.List<Subscription>(); foreach (var subscription in subscriptions) { session.Delete(subscription); } session.Flush(); _memoryStore.ClearAddressSubscriptions(address); _logger.DebugFormat("Cleaned up subscriptions of subscriber address '{0}'", subscriberAddress); } }
public void Unsubscribe(Address address, Type messageType) { using (var connection = _connectionFactory.OpenConnection()) { var subscriberAddress = address.ToString(); var messageTypeName = messageType.AssemblyQualifiedName; var table = Configuration.Instance.GetSetting<string>("subscriptionTable"); connection.Delete(new { SubscriberAddress = subscriberAddress, MessageType = messageTypeName }, table); _memoryStore.Unsubscribe(address, messageType); _logger.DebugFormat("Subscriber '{0}' unsubscribes message '{1}'.", subscriberAddress, messageTypeName); } }
void IMessageTransport.SendMessage(Message message, Address targetAddress) { if (message == null) { throw new ArgumentNullException("message"); } if (targetAddress == null) { throw new ArgumentNullException("targetAddress"); } var queuePath = MsmqUtilities.GetFullPath(targetAddress); try { using (var messageQueue = new MessageQueue(queuePath, QueueAccessMode.SendAndReceive)) { var toSend = MsmqUtilities.Convert(message); if (message.ReplyToAddress != null) { toSend.ResponseQueue = new MessageQueue(MsmqUtilities.GetReturnAddress(message.ReplyToAddress.ToString(), targetAddress.ToString())); } messageQueue.Send(toSend, MessageQueueTransactionType.Automatic); message.Id = toSend.Id; } } catch (MessageQueueException ex) { if (ex.MessageQueueErrorCode == MessageQueueErrorCode.QueueNotFound) { throw new EventSourcingException("消息队列未找到: [{0}]", targetAddress); } else { throw new EventSourcingException("发送消息到队列时遇到异常,队列地址:{0},异常详情:{1}", targetAddress, ex); } } catch (Exception ex) { throw new EventSourcingException("发送消息到队列时遇到异常,队列地址:{0},异常详情:{1}", targetAddress, ex); } }
public void Subscribe(Address address, Type messageType) { using (var session = _sessionFactory.OpenSession()) { var criteria = session.CreateCriteria<Subscription>(); var subscriberAddress = address.ToString(); var messageTypeName = messageType.AssemblyQualifiedName; criteria.Add(Expression.Eq("SubscriberAddress", subscriberAddress)); criteria.Add(Expression.Eq("MessageType", messageTypeName)); var subscription = criteria.UniqueResult<Subscription>(); if (subscription == null) { subscription = new Subscription { UniqueId = Guid.NewGuid().ToString(), SubscriberAddress = subscriberAddress, MessageType = messageTypeName }; session.Save(subscription); session.Flush(); _memoryStore.Subscribe(address, messageType); _logger.DebugFormat("Subscriber '{0}' subscribes message '{1}'.", subscriberAddress, messageTypeName); } } }