protected override RawBusMessage HandleMessage(ICallHandler handler, RawBusMessage message, bool redelivered, ulong deliveryTag) { try { RawBusMessage replyMessage = base.HandleMessage(handler, message, redelivered, deliveryTag); Model.BasicAck(deliveryTag, false); return replyMessage; } catch (RejectMessageException) { // If reject message exception is thrown -> reject message without requeue it. // Message will be lost or transfered to dead letter exchange by broker Model.BasicNack(deliveryTag, false, false); throw; } catch (Exception) { Model.BasicNack(deliveryTag, false, true); throw; } }
protected override async Task<RawBusMessage> HandleMessage(ICallHandler handler, RawBusMessage message, bool redelivered, ulong deliveryTag) { try { RawBusMessage replyMessage = await base.HandleMessage(handler, message, redelivered, deliveryTag); Model.BasicAck(deliveryTag, false); return replyMessage; } catch (RejectMessageException) { // If reject message exception is thrown -> reject message without requeue it. // Message will be lost or transfered to dead letter exchange by broker Model.BasicNack(deliveryTag, false, false); throw; } catch (Exception ex) { bool requeue = _exceptionFilter.Filter(ex, message, redelivered, deliveryTag); Model.BasicNack(deliveryTag, false, requeue); if (requeue) { return null; } throw; } }
public void Deserialize(RawBusMessage rawBusMessage, XmlDictionaryReader bodyContent) { XmlObjectSerializer serializer; if (_map.TryGetValue(new DataContractKey(rawBusMessage.Name, rawBusMessage.Namespace), out serializer)) { rawBusMessage.Data = serializer.ReadObject(bodyContent); } }
public void DeliveryFailed(int errorCode, string text, RawBusMessage message) { _errorCode = errorCode; _text = text; _message = message; _ev.Set(); }
public void SetResponse(RawBusMessage message, Exception exception) { _message = message; _exception = exception; _callback(_message, _exception); _ev.Set(); }
private bool IsMessageSurvivesFilter(MessageFilterInfo filterInfo, RawBusMessage busMessage) { // TODO: Add header filtering if (filterInfo.ReceiveSelfPublish) return true; bool selfPublished = Equals(busMessage.BusId, _busId); return !selfPublished; }
private static void SetUserHeaders(RawBusMessage busMessage, Message message) { foreach (BusHeader busHeader in busMessage.Headers) { message.Headers.Add(MessageHeader.CreateHeader(busHeader.Name, MessagingConstants.Namespace.MessageBus, busHeader.Value, false, MessagingConstants.Actor.User)); } }
public byte[] Serialize(RawBusMessage busMessage) { object data = busMessage.Data; if (data.GetType() == typeof(byte[])) { return data as byte[]; } string body = JsonConvert.SerializeObject(data, Formatting.None); return _encoding.GetBytes(body); }
public byte[] Serialize(RawBusMessage busMessage) { using (Message message = Message.CreateMessage(_encoderFactory.MessageVersion, MessagingConstants.MessageAction.Regular, busMessage.Data)) { SetBusHeaders(busMessage, message); SetUserHeaders(busMessage, message); // Serialize the message to stream using (MemoryStream str = new MemoryStream()) { _encoderFactory.Encoder.WriteMessage(message, str); return str.ToArray(); } } }
public void Intercept(IInvocation invocation) { object data = _messageFactory.CreateMessage(invocation.Method, invocation.Arguments); RawBusMessage message = new RawBusMessage { Data = data, }; IEnumerable<BusHeader> headers = _headersProvider.GetMessageHeaders(); foreach (BusHeader header in headers) { message.Headers.Add(header); } _publisher.Send(message); }
public static RawBusMessage ToRawBusMessage <T>(this BusMessage <T> message) { if (message == null) { return(null); } RawBusMessage rawBusMessage = new RawBusMessage { Data = message.Data }; foreach (var header in message.Headers) { rawBusMessage.Headers.Add(header); } return(rawBusMessage); }
public RawBusMessage ReadMessage(Message message, Action<RawBusMessage, XmlDictionaryReader> deserializer) { string busId = message.Headers.GetHeader<string>(MessagingConstants.HeaderNames.BusId, MessagingConstants.Namespace.MessageBus, MessagingConstants.Actor.Bus); DateTime sent = message.Headers.GetHeader<DateTime>(MessagingConstants.HeaderNames.SentTime, MessagingConstants.Namespace.MessageBus, MessagingConstants.Actor.Bus); RawBusMessage rawBusMessage = new RawBusMessage { BusId = busId, Sent = sent }; foreach (MessageHeaderInfo headerInfo in message.Headers.Where(info => info.Actor == MessagingConstants.Actor.User && info.Namespace == MessagingConstants.Namespace.MessageBus)) { string value = message.Headers.GetHeader<string>(headerInfo.Name, headerInfo.Namespace, headerInfo.Actor); rawBusMessage.Headers.Add(new BusHeader { Name = headerInfo.Name, Value = value }); } using (XmlDictionaryReader bodyContents = message.GetReaderAtBodyContents()) { rawBusMessage.Name = bodyContents.Name; rawBusMessage.Namespace = bodyContents.NamespaceURI; deserializer(rawBusMessage, bodyContents); } return rawBusMessage; }
public void SetUp() { _busMessage = null; _exception = null; }
public void MessageFilteredOut(RawBusMessage busMessage) { _busMessage = busMessage; _ev.Set(); }
public void MessageDispatchException(RawBusMessage busMessage, Exception exception) { _busMessage = busMessage; _exception = exception; _ev.Set(); }
public void MessageDeserializeException(RawBusMessage busMessage, Exception exception) { _busMessage = busMessage; _exception = exception; _ev.Set(); }
protected abstract void OnMessageReturn(int replyCode, string replyText, RawBusMessage message);
protected virtual Task<RawBusMessage> HandleMessage(ICallHandler handler, RawBusMessage message, bool redelivered, ulong deliveryTag) { return handler.Dispatch(message); }
public void MessageArrived(string busId, RawBusMessage busMessage, string consumerTag) { }
public void Dispatch(RawBusMessage message) { _action(message); }
public byte[] Serialize(RawBusMessage data) { return ((ScaleoutMessage)data.Data).ToBytes(); }
public Task<RawBusMessage> Dispatch(RawBusMessage message) { _action(message); return Task.FromResult(new RawBusMessage()); }
public async Task<RawBusMessage> Dispatch(RawBusMessage message) { await _action(message); return new RawBusMessage(); }
public void DeliveryFailed(int errorCode, string text, RawBusMessage message) { }
public void UnregisteredMessageArrived(RawBusMessage busMessage) { _busMessage = busMessage; _ev.Set(); }
public void MessageSent(string busId, RawBusMessage busMessage) { }
private void SetBusHeaders(RawBusMessage busMessage, Message message) { SetBusHeader(message, MessagingConstants.HeaderNames.Name, busMessage.Name); SetBusHeader(message, MessagingConstants.HeaderNames.NameSpace, busMessage.Namespace); SetBusHeader(message, MessagingConstants.HeaderNames.BusId, busMessage.BusId); SetBusHeader(message, MessagingConstants.HeaderNames.SentTime, busMessage.Sent); }
public void Dispatch(RawBusMessage message) { }
public bool Filter(Exception exception, RawBusMessage message, bool redelivered, ulong deliveryTag) { return !redelivered; }
protected virtual async Task<bool> ConsumeMessage(bool redelivered, ulong deliveryTag, IBasicProperties properties, byte[] body) { DataContractKey dataContractKey = properties.GetDataContractKey(); var subscription = _subscriptions.Where(p => p.Value.FilterInfo.ContractKey.Equals(dataContractKey)).Select( pair => new { DataType = pair.Key, pair.Value.Handler }).FirstOrDefault(); if (subscription == null) { RawBusMessage rawBusMessage = _messageHelper.ConstructMessage(dataContractKey, properties, (object)body); _errorSubscriber.UnregisteredMessageArrived(rawBusMessage); return false; } object data; if (!_serializers.ContainsKey(properties.ContentType)) { RawBusMessage rawBusMessage = _messageHelper.ConstructMessage(dataContractKey, properties, (object)body); _errorSubscriber.MessageDeserializeException(rawBusMessage, new Exception("Unsupported content type")); return false; } ISerializer serializer; try { serializer = _serializers[properties.ContentType]; data = serializer.Deserialize(dataContractKey, subscription.DataType, body); } catch (Exception ex) { RawBusMessage rawBusMessage = _messageHelper.ConstructMessage(dataContractKey, properties, (object)body); _errorSubscriber.MessageDeserializeException(rawBusMessage, ex); return false; } RawBusMessage message = _messageHelper.ConstructMessage(dataContractKey, properties, data); if (!_receiveSelfPublish && _busId.Equals(message.BusId)) { _errorSubscriber.MessageFilteredOut(message); return false; } _trace.MessageArrived(_busId, message, ConsumerTag); RawBusMessage reply; try { reply = await HandleMessage(subscription.Handler, message, redelivered, deliveryTag); } catch (RejectMessageException ex) { reply = new RawBusMessage { Data = ex.ReplyData }; reply.Headers.Add(new RejectedHeader()); } catch (Exception ex) { _errorSubscriber.MessageDispatchException(message, ex); reply = new RawBusMessage(); reply.Headers.Add(new ExceptionHeader { Message = ex.Message }); } if (!_neverReply && reply != null && properties.IsReplyToPresent()) { _sendHelper.Send(new SendParams { BusId = _busId, BusMessage = reply, Model = Model, CorrelationId = properties.IsCorrelationIdPresent() ? properties.CorrelationId : "", Exchange = _replyExchange, RoutingKey = properties.ReplyTo, Serializer = serializer, MandatoryDelivery = false, PersistentDelivery = false }); _trace.MessageSent(_busId, reply); } return true; }
public RawBusMessage Dispatch(RawBusMessage message) { _action(message); return new RawBusMessage(); }