void SendMessage(MessageInfo messageInfo, Inbox inbox, NativeActivityContext context) { if (messageInfo == null) { return; } var process = Process.GetProcessFromContext(context.DataContext); IMessaging messaging = context.GetExtension <IMessaging>(); IMessage msg = messaging.CreateMessage(); msg.Template = messageInfo.Template; msg.Key = messageInfo.Key; msg.DataSource = process.DataSource; msg.Schema = process.Schema; msg.Model = process.ModelName; msg.ModelId = process.ModelId; msg.Source = $"Inbox:{inbox.Id}"; msg.Params.Append(messageInfo.Params, replaceExisiting: true); msg.Environment.Add("InboxId", inbox.Id); msg.Environment.Add("ProcessId", process.Id); messaging.QueueMessage(msg); TrackRecord(context, $"Message queued successfully {{Id: {msg.Id}}}"); }
/// <summary> /// Call a remote procedure with a specified timeout /// </summary> /// <param name="endpoint">The endpoint to which to route the request</param> /// <param name="body">The request body</param> /// <param name="receiveTimeout">Timeout in ms, after which a null response is returned</param> /// <returns>The response</returns> public JContainer Call(string endpoint, JContainer body, int receiveTimeout) { var message = _messaging.CreateMessage(); message.To = _target; message.ReplyTo = _replyTo; message.Body = Json.Serialize(body); message.Properties.Headers = AmqpRpc.CreateHeaders(endpoint, null); _messaging.Send(message); var reply = _messaging.Receive(receiveTimeout); if (AmqpRpc.GetStatusCode(reply) != 200) { throw new AmqpRpcError(Encoding.UTF8.GetString(reply.Body)); } // TODO handle null reply (timeout) return(Json.Deserialize(reply.Body)); }
public void TestDirect() { using (IConnector conn = Factory.CreateConnector(_builder)) { //create two parties IMessaging foo = Factory.CreateMessaging(); foo.Connector = conn; foo.Identity = "foo"; foo.Sent += TestHelper.Sent; foo.SetupReceiver += delegate(IModel channel) { DeclareQueue(channel, foo.Identity); }; foo.Init(); IMessaging bar = Factory.CreateMessaging(); bar.Connector = conn; bar.Identity = "bar"; bar.Sent += TestHelper.Sent; bar.SetupReceiver += delegate(IModel channel) { DeclareQueue(channel, bar.Identity); }; bar.Init(); //send message from foo to bar IMessage mf = foo.CreateMessage(); mf.Body = TestHelper.Encode("message1"); mf.To = "bar"; foo.Send(mf); //receive message at bar and reply IReceivedMessage rb = bar.Receive(); TestHelper.LogMessage("recv", bar, rb); IMessage mb = bar.CreateReply(rb); mb.Body = TestHelper.Encode("message2"); bar.Send(mb); bar.Ack(rb); //receive reply at foo IReceivedMessage rf = foo.Receive(); TestHelper.LogMessage("recv", foo, rf); foo.Ack(rf); } }
protected void TestRelayedHelper(MessagingClosure senderSetup, MessagingClosure receiverSetup, ConnectionBuilder builder) { using (IConnector conn = Factory.CreateConnector(builder), relayConn = Factory.CreateConnector(builder)) { //create relay IMessaging relay = Factory.CreateMessaging(); relay.Connector = relayConn; relay.Identity = "relay"; relay.ExchangeName = "out"; relay.SetupSender += delegate(IModel channel) { DeclareExchange(channel, relay.ExchangeName, "direct"); }; relay.SetupReceiver += delegate(IModel channel) { DeclareExchange(channel, "in", "fanout"); DeclareQueue(channel, relay.QueueName); BindQueue(channel, relay.QueueName, "in", ""); }; relay.Init(); //run relay new System.Threading.Thread (delegate() { //receive messages and pass it on IReceivedMessage r; try { while (true) { r = relay.Receive(); relay.Send(r); relay.Ack(r); } } catch (EndOfStreamException) { } catch (AlreadyClosedException) { } catch (OperationInterruptedException) { } }).Start(); //create two parties IMessaging foo = Factory.CreateMessaging(); foo.Connector = conn; foo.Identity = "foo"; foo.SetupSender += senderSetup(foo); foo.SetupReceiver += receiverSetup(foo); foo.ExchangeName = "in"; foo.Sent += TestHelper.Sent; foo.Init(); IMessaging bar = Factory.CreateMessaging(); bar.Connector = conn; bar.Identity = "bar"; bar.SetupSender += senderSetup(bar); bar.SetupReceiver += receiverSetup(bar); bar.ExchangeName = "in"; bar.Sent += TestHelper.Sent; bar.Init(); //send message from foo to bar IMessage mf = foo.CreateMessage(); mf.Body = TestHelper.Encode("message1"); mf.To = "bar"; foo.Send(mf); //receive message at bar and reply IReceivedMessage rb = bar.Receive(); TestHelper.LogMessage("recv", bar, rb); IMessage mb = bar.CreateReply(rb); mb.Body = TestHelper.Encode("message2"); bar.Send(mb); bar.Ack(rb); //receive reply at foo IReceivedMessage rf = foo.Receive(); TestHelper.LogMessage("recv", foo, rf); foo.Ack(rf); } }
void Run(Address me, Address you, AmqpTcpEndpoint server, int sleep) { ConnectionFactory connectionFactory = new ConnectionFactory(); connectionFactory.Endpoint = server; using (IConnector conn = Factory.CreateConnector(new ConnectionBuilder(connectionFactory))) { IMessaging m = Factory.CreateMessaging(); m.Connector = conn; m.Identity = me; m.Sent += Sent; m.SetupSender += delegate(IModel channel) { //We declare the recipient queue here to avoid //sending messages into the ether. That's an ok //thing to do for testing channel.QueueDeclare(you, true, false, false, null); }; m.SetupReceiver += delegate(IModel channel) { channel.QueueDeclare(me, true, false, false, null); }; m.Init(); byte[] body = new byte[0]; for (int i = 0;; i++) { //send IMessage msg = m.CreateMessage(); msg.Body = body; msg.To = you; msg.Properties.SetPersistent(true); m.Send(msg); pending.Add(msg.MessageId, true); //handle inbound while (true) { IReceivedMessage r = m.ReceiveNoWait(); if (r == null) { break; } if (r.CorrelationId == null) { recv++; DisplayStats(); m.Send(m.CreateReply(r)); } else { if (pending.ContainsKey(r.CorrelationId)) { pending.Remove(r.CorrelationId); pend--; } else { disc++; } DisplayStats(); } m.Ack(r); } //Slow down to prevent pending from growing w/o bounds System.Threading.Thread.Sleep(sleep + pend); } } }