public void Send(MessageContainer message) { var fn = Guid.NewGuid().ToString("N") + ".json"; using (var sw = new StreamWriter(fn, false, Encoding.UTF8)) { sw.Write(JsonConvert.SerializeObject(message)); } }
void _transport_OnMessageToUnknownDestination(MessageContainer message, IMessageTransport transport) { string dest = message.To; log.Debug("Message to remote destination {0}", dest); Uri uri = new Uri(dest); string sn = string.Format("MessageTransport_{0}", uri.Scheme); IMessageTransport mt = ServiceLocator.GetInstance <IMessageTransport>(sn); if (mt == null) { throw new Exception("No message transport configured for destination " + dest); } mt.Send(message); }
void httpTransport_OnMessageArrived(MessageContainer message, IMessageTransport transport) { if (message.RetryCount > 0) { //we don't check if already received if message is sent for the first time if (ReceivedMessageRegistry.HasBeenReceived(message.UniqueId)) { log.Warn("Message {0} has already been received. Ignoring", message.UniqueId); return; } } log.Info("Forwarding incoming http message {0} (from {1}) to {2}", message, message.From, _bus.Endpoint); message.To = _bus.Endpoint; _bus.Send(message); ReceivedMessageRegistry.RegisterReceived(message.UniqueId); }
public void Send(MessageContainer message) { try { WebClient wc = new WebClient(); StringWriter sw = new StringWriter(); message.From = this.Endpoint; _ser.Serialize(sw, message); string ret = wc.UploadString(message.To, sw.ToString()); } catch (Exception ex) { log.Error("Error sending message {0}: {1}", message, ex); throw; } }
public object Clone() { MessageContainer mc = new MessageContainer(); mc.From = this.From; mc.To = this.To; if (Cc != null) { mc.Cc = new List <string>(this.Cc); } if (this.Headers != null) { mc.Headers = new Dictionary <string, string>(this.Headers); } mc.Body = this.Body; mc.BodyStr = this.BodyStr; return(mc); }
public MessagePreprocessResult HandleIncomingMessage(MessageContainer message, IMessageTransport transport) { log.Info("Preprocessing message: {0}", message.BodyStr); var m = typeRe.Match(message.BodyStr); if (!m.Success) { log.Warn("Did not match message type. Ignoring the message!"); return MessagePreprocessResult.CancelFurtherProcessing; } string mtype = m.Groups[1].Captures[0].Value; int idx = mtype.IndexOf(','); if (idx > 0) mtype = mtype.Substring(0, idx); log.Info("Message type is {0}", mtype); if (mtype.StartsWith("NGinnBPM.MessageBus.Messages")) { log.Info("It's a control message so we process it as usual"); return MessagePreprocessResult.ContinueProcessing; } List<MessageContainer> messages = new List<MessageContainer>(); //now get destination endpoints from the subscribers database //and prepare a message clone for each destination List<string> destinations = new List<string>(); foreach (string typeName in new string[] { mtype, "System.Object" }) { foreach (string destination in SubscriptionManager.GetTargetEndpoints(typeName)) { if (!destinations.Contains(destination)) destinations.Add(destination); } } foreach (string destination in destinations) { var msg = message.Clone() as MessageContainer; msg.To = destination; //set destination. We don't update msg.From so the original endpoint is unchanged messages.Add(msg); } if (messages.Count > 0) { //send messages to their destinations transport.SendBatch(messages, MessageBusContext.ReceivingConnection); log.Info("Sent message to {0} destinations", messages.Count); } return MessagePreprocessResult.CancelFurtherProcessing; }
void _transport_OnMessageArrived(MessageContainer message, IMessageTransport transport) { Debug.Assert(message.BodyStr != null && message.Body == null); List <IPreprocessMessages> callbacks = null; MessagePreprocessResult disp = PreprocessIncomingMessage(message, transport, out callbacks); if (disp == MessagePreprocessResult.CancelFurtherProcessing) { return; } Exception e2 = null; try { DeserializeMessage(message); Debug.Assert(message.Body != null); DispatchIncomingMessage(message); } catch (Exception ex) { e2 = ex; throw; } finally { if (callbacks != null) { callbacks.Reverse(); callbacks.ForEach(x => { try { x.AfterMessageProcessed(message, this, this._transport, e2); } catch (Exception e3) { log.Warn("Callback error: {0}", e3); } }); } } }
public string Serialize(MessageContainer mc) { if (string.IsNullOrEmpty(mc.BodyStr)) { if (mc.Body != null) { var sw = new StringWriter(); _ser.Serialize(sw, mc.Body); if (!EmbedMessageTypeInBody) { mc.SetHeader(MessageContainer.HDR_MessageType, TypeToString(mc.Body.GetType())); } mc.BodyStr = sw.ToString(); mc.Body = null; } } var sw2 = new StringWriter(); _ser.Serialize(sw2, mc); return(sw2.ToString()); }
public string Serialize(MessageContainer mc) { if (string.IsNullOrEmpty(mc.BodyStr)) { if (mc.Body != null) { var sw = new StringWriter(); _ser.Serialize(sw, mc.Body); if (!EmbedMessageTypeInBody) { mc.SetHeader(MessageContainer.HDR_MessageType, TypeToString(mc.Body.GetType())); } mc.BodyStr = sw.ToString(); mc.Body = null; } } var sw2 = new StringWriter(); _ser.Serialize(sw2, mc); return sw2.ToString(); }
public void Send(string destination, object[] msgs) { foreach (var msg in msgs) { Dispatcher.DispatchMessageToOutgoingMessageHandlers(msg, this); } if (destination == null) { destination = Endpoint; } List <MessageContainer> lst = new List <MessageContainer>(); foreach (object obj in msgs) { MessageContainer mc = new MessageContainer(); mc.From = MessageTransport.Endpoint; mc.To = destination; mc.UniqueId = CreateNewMessageUniqueId(); mc.Body = obj; lst.Add(mc); } SendMessages(lst, null); }
internal void HandleIncomingMessage(TextReader input) { MessageContainer mc = null; try { JsonReader jsr = new JsonTextReader(input); mc = _ser.Deserialize <MessageContainer>(jsr); if (mc.To == null || mc.To.Length == 0) { mc.To = Endpoint; } if (OnMessageArrived != null) { OnMessageArrived(mc, this); } } catch (Exception ex) { log.Error("Error handling incoming message: {0}", ex); throw; } }
protected virtual MessagePreprocessResult PreprocessIncomingMessage(MessageContainer mc, IMessageTransport t, out List <IPreprocessMessages> callbacks) { List <IPreprocessMessages> callb = null; callbacks = callb; //todo: allow for ordering of message preprocessors foreach (IPreprocessMessages pm in this.ServiceLocator.GetAllInstances <IPreprocessMessages>()) { ServiceLocator.ReleaseInstance(pm); var res = pm.HandleIncomingMessage(mc, this, t); if (callb == null) { callb = new List <IPreprocessMessages>(); } callb.Add(pm); if (res != MessagePreprocessResult.ContinueProcessing) { return(res); } } callbacks = callb; return(MessagePreprocessResult.ContinueProcessing); }
/// <summary> /// This method dispatches the message directly to their handlers (local only) /// without persisting it in database and in a non-transactional way. /// Effectively bypasses NGinn MessageBus, just forwards the message to handler components /// </summary> /// <param name="mc"></param> internal void DispatchLocalNonPersistentMessage(MessageContainer mc, DeliveryMode mode) { if (mc.To != Endpoint) { throw new Exception("Local only please"); } if (mc.From != Endpoint) { throw new Exception("Local only please"); } if (mc.DeliverAt > DateTime.Now) { throw new Exception("Delivery date unsupported"); } if (mc.Cc != null) { foreach (string s in mc.Cc) { if (s != Endpoint) { throw new Exception("Non-local endpoint on cc"); } } } if (mode == DeliveryMode.LocalAsync) { System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(delegate(object v) { DispatchLocal(mc, mode); })); } else { DispatchLocal(mc, mode); } }
public void Publish() { mc.To = mbus.Endpoint; if (_deliveryMode != DeliveryMode.DurableAsync) { foreach (object body in _bodies) { mc.Body = body; mbus.DispatchLocalNonPersistentMessage(mc, _deliveryMode); } } else { List <MessageContainer> l = new List <MessageContainer>(); foreach (object body in _bodies) { MessageContainer mc2 = mc.Clone() as MessageContainer; mc2.Body = body; l.Add(mc2); } mbus.NotifyAndDistributeMessages(l, _dbTran); } }
public void Send(MessageContainer message) { if (string.IsNullOrEmpty(message.BodyStr)) throw new Exception("BodyStr"); var db = OpenDatabase(_connStringAlias); db.GetCollection(_collectionName).Insert(Wrap(message)); _waiter.Set(); }
protected virtual void DeserializeMessage(MessageContainer mc) { Debug.Assert(mc.Body == null); mc.Body = this.MessageSerializer.Deserialize(new StringReader(mc.BodyStr)); }
internal CurMsg(MessageContainer mc) { Message = mc; DeliveryMode = DeliveryMode.DurableAsync; }
/// <summary> /// Alternative version that selects & updates the row in a single query. /// However, the testing has shown that it's actually slower than the original, two-query, version /// I'm leaving it here to remember that this has already been tried and failed. /// </summary> /// <param name="conn"></param> /// <param name="retryTime"></param> /// <returns></returns> private MessageContainer SelectNextMessageForProcessing2008(IDbConnection conn, out DateTime? retryTime) { var mc = new MessageContainer(); retryTime = null; using (var cmd = conn.CreateCommand()) { cmd.CommandText = string.Format(@"UPDATE TOP(1) {0} with(readpast) SET subqueue = 'X', last_processed = getdate() OUTPUT inserted.id, inserted.from_endpoint, inserted.to_endpoint, inserted.retry_count, inserted.retry_time, inserted.correlation_id, inserted.msg_text, inserted.msg_headers, inserted.unique_id WHERE id in (select top(1) id from {0} with(readpast) where subqueue = 'I' order by retry_time)", _queueTable); using (var dr = cmd.ExecuteReader()) { if (!dr.Read()) return null; mc.From = Convert.ToString(dr["from_endpoint"]); mc.To = Convert.ToString(dr["to_endpoint"]); mc.HeadersString = Convert.ToString(dr["msg_headers"]); mc.SetHeader(MessageContainer.HDR_RetryCount, Convert.ToInt32(dr["retry_count"]).ToString()); ; mc.CorrelationId = Convert.ToString(dr["correlation_id"]); mc.BusMessageId = Convert.ToString(dr["id"]); mc.UniqueId = Convert.ToString(dr["unique_id"]); retryTime = Convert.ToDateTime(dr["retry_time"]); mc.BodyStr = dr.GetString(dr.GetOrdinal("msg_text")); } } return mc; }
public void Send(MessageContainer message) { List<MessageContainer> lst = new List<MessageContainer>(); lst.Add(message); SendBatch(lst, null); }
public void Send(string destination, object[] msgs) { foreach (var msg in msgs) { Dispatcher.DispatchMessageToOutgoingMessageHandlers(msg, this); } if (destination == null) destination = Endpoint; List<MessageContainer> lst = new List<MessageContainer>(); foreach (object obj in msgs) { MessageContainer mc = new MessageContainer(); mc.From = MessageTransport.Endpoint; mc.To = destination; mc.UniqueId = CreateNewMessageUniqueId(); mc.Body = obj; lst.Add(mc); } SendMessages(lst, null); }
internal CurMsgInfo(MessageContainer mc) { Message = mc; }
private void DispatchLocal(MessageContainer mc, DeliveryMode dm) { var pm = _currentMessage; var mb = MessageBusContext.CurrentMessageBus; try { _currentMessage = new CurMsg(mc); MessageBusContext.CurrentMessageBus = this; _currentMessage.DeliveryMode = dm; Dispatcher.DispatchMessage(mc.Body, this); } catch (Exception ex) { log.Warn("Error async processing message {0}: {1}", mc.BusMessageId, ex.ToString()); if (dm != DeliveryMode.LocalAsync) { throw; } } finally { _currentMessage = pm; MessageBusContext.CurrentMessageBus = mb; } }
private void Notify(object[] msgs, DeliveryMode mode) { foreach (var msg in msgs) { Dispatcher.DispatchMessageToOutgoingMessageHandlers(msg, this); } List<MessageContainer> lst = new List<MessageContainer>(); foreach (Object obj in msgs) { MessageContainer mc = new MessageContainer(); mc.From = Endpoint; mc.To = Endpoint; mc.Body = obj; mc.UniqueId = CreateNewMessageUniqueId(); lst.Add(mc); } if (mode == DeliveryMode.DurableAsync) { NotifyAndDistributeMessages(lst, null); } else { lst.ForEach(mc => this.DispatchLocalNonPersistentMessage(mc, mode)); } }
public static void TestQueueOps() { AccessOraDb(null, con => { var qt ="mq_test2"; var qops = SqlHelper.GetQueueOps(SqlHelper.GetDialect(con.GetType())); qops.CleanupProcessedMessages(con, qt, null); qops.MoveScheduledMessagesToInputQueue(con, qt); var mc = new MessageContainer { Body = "ala ma kota", From = "sql://oradb/mq_test2", To = "sql://oradb/mq_test2", Label = "lbl", BodyStr = "a tu takki kot", HeadersString = null }; var msgs = new Dictionary<string, ICollection<MessageContainer>> { {"mq_test2", new List<MessageContainer>{mc }} }; qops.InsertMessageBatchToLocalDatabaseQueues(con, msgs); DateTime? rt; bool more; mc = qops.SelectAndLockNextInputMessage(con, qt, () => new string[] {}, out rt, out more); log.Info("MC: {0}", mc == null ? " - nul - " : mc.BusMessageId); }); }
void _transport_OnMessageArrived(MessageContainer message, IMessageTransport transport) { Debug.Assert(message.BodyStr != null && message.Body == null); List<IPreprocessMessages> callbacks = null; MessagePreprocessResult disp = PreprocessIncomingMessage(message, transport, out callbacks); if (disp == MessagePreprocessResult.CancelFurtherProcessing) { return; } Exception e2 = null; try { DeserializeMessage(message); Debug.Assert(message.Body != null); DispatchIncomingMessage(message); } catch (Exception ex) { e2 = ex; throw; } finally { if (callbacks != null) { callbacks.Reverse(); callbacks.ForEach(x => { try { x.AfterMessageProcessed(message, this, this._transport, e2); } catch (Exception e3) { log.Warn("Callback error: {0}", e3); } }); } } }
public NewMessageFluent(MessageBus bus) { mbus = bus; mc = new MessageContainer(); mc.From = mbus.Endpoint; }
protected MessageContainer Unwrap(MongoMessageWrapper m) { var mc = new MessageContainer(); mc.Headers = new Dictionary<string, string>(); if (m.Headers != null) { foreach (string s in m.Headers.Keys) { mc.SetHeader(s, m.Headers[s]); } } mc.BodyStr = m.Payload; mc.BusMessageId = m._id.ToString(); mc.RetryCount = m.RetryCount; mc.From = m.From; mc.To = m.To; return mc; }
protected MongoMessageWrapper Wrap(MessageContainer mc) { var mw = new MongoMessageWrapper { From = string.IsNullOrEmpty(mc.From) ? this.Endpoint : mc.From, To = mc.To, InsertTime = DateTime.Now, RetryCount = 0, RetryTime = mc.DeliverAt, SubQueue = "I", Payload = mc.BodyStr }; var hl = MongoMessageWrapper.GetSkipHeaderNames(); if (mc.Headers != null && mc.Headers.Any(x => !hl.Contains(x.Key))) { mw.Headers = new Dictionary<string, string>(); foreach (string k in mc.Headers.Keys) { if (!hl.Contains(k)) mw.Headers[k] = mc.Headers[k]; } } return mw; }
/// <summary> /// This method dispatches the message directly to their handlers (local only) /// without persisting it in database and in a non-transactional way. /// Effectively bypasses NGinn MessageBus, just forwards the message to handler components /// </summary> /// <param name="mc"></param> internal void DispatchLocalNonPersistentMessage(MessageContainer mc, DeliveryMode mode) { if (mc.To != Endpoint) throw new Exception("Local only please"); if (mc.From != Endpoint) throw new Exception("Local only please"); if (mc.DeliverAt > DateTime.Now) throw new Exception("Delivery date unsupported"); if (mc.Cc != null) { foreach (string s in mc.Cc) if (s != Endpoint) throw new Exception("Non-local endpoint on cc"); } if (mode == DeliveryMode.LocalAsync) { System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(delegate(object v) { DispatchLocal(mc, mode); })); } else { DispatchLocal(mc, mode); } }
protected virtual MessagePreprocessResult PreprocessIncomingMessage(MessageContainer mc, IMessageTransport t, out List<IPreprocessMessages> callbacks) { List<IPreprocessMessages> callb = null; callbacks = callb; //todo: allow for ordering of message preprocessors foreach (IPreprocessMessages pm in this.ServiceLocator.GetAllInstances<IPreprocessMessages>()) { ServiceLocator.ReleaseInstance(pm); var res = pm.HandleIncomingMessage(mc, this, t); if (callb == null) callb = new List<IPreprocessMessages>(); callb.Add(pm); if (res != MessagePreprocessResult.ContinueProcessing) return res; } callbacks = callb; return MessagePreprocessResult.ContinueProcessing; }
/// <summary> /// Forwards message to a remote endpoint /// </summary> /// <param name="mc"></param> protected virtual void ForwardMessageToRemoteEndpoint(MessageContainer mc) { if (mc.To.StartsWith("sql://")) { string alias, table; if (!SqlUtil.ParseSqlEndpoint(mc.To, out alias, out table)) throw new Exception("Invalid target endpoint: " + mc.To); List<MessageContainer> l = new List<MessageContainer>(); l.Add(mc); var d = new Dictionary<string, ICollection<MessageContainer>>(); d[table] = l; var cs = GetConnectionString(alias); if (cs == null) throw new Exception("Unknown connection string alias: " + alias); InsertMessageBatchToLocalDatabaseQueues(cs, d); } else { if (OnMessageToUnknownDestination != null) { OnMessageToUnknownDestination(mc, this); } else throw new Exception("Don't know how to send message to destination: " + mc.To); } }
/// <summary> /// Deliver the message to handlers /// </summary> /// <param name="mc"></param> protected virtual void DispatchIncomingMessage(MessageContainer mc) { log.Debug("MB {2} Dispatching incoming message {0}/{1}/{3}", mc.To, mc.BusMessageId, this.Endpoint, mc.Body.GetType().Name); try { MessageBusContext.CurrentMessageBus = this; _currentMessage = new CurMsg(mc); if (UseTransactionScope && Transaction.Current == null) { TransactionOptions to = new TransactionOptions(); to.IsolationLevel = IsolationLevel.ReadCommitted; to.Timeout = TimeSpan.FromSeconds(30); TransactionScopeOption tso = MessageHandlerTransactionScopeOption; using (TransactionScope ts = new TransactionScope(tso, to)) { Dispatcher.DispatchMessage(mc.Body, this); ts.Complete(); } } else { Dispatcher.DispatchMessage(mc.Body, this); } } finally { _currentMessage = null; MessageBusContext.CurrentMessageBus = null; } }
public object Clone() { MessageContainer mc = new MessageContainer(); mc.From = this.From; mc.To = this.To; if (Cc != null) mc.Cc = new List<string>(this.Cc); if (this.Headers != null) mc.Headers = new Dictionary<string, string>(this.Headers); mc.Body = this.Body; mc.BodyStr = this.BodyStr; return mc; }
void _transport_OnMessageToUnknownDestination(MessageContainer message, IMessageTransport transport) { string dest = message.To; log.Debug("Message to remote destination {0}", dest); Uri uri = new Uri(dest); string sn = string.Format("MessageTransport_{0}", uri.Scheme); IMessageTransport mt = ServiceLocator.GetInstance<IMessageTransport>(sn); if (mt == null) throw new Exception("No message transport configured for destination " + dest); mt.Send(message); }