public static void SendMsg(object msg, string topic) { switch (msg.GetType().Name) { case ("MsgCommandSign"): ((MsgCommandSign)msg).Topic = topic; _mqHelper.Publish((MsgCommandSign)msg); break; case ("MsgTrainTimeReport"): ((MsgTrainTimeReport)msg).Topic = topic; _mqHelper.Publish((MsgTrainTimeReport)msg); break; default: break; } // 以下为测试代码 //ConnectionFactory factory = new ConnectionFactory { HostName = "39.108.177.237", Port = 5672, UserName = "******", Password = "******" }; //using (IConnection conn = factory.CreateConnection()) //{ // using (IModel im = conn.CreateModel()) // { // string json = JsonConvert.SerializeObject(msg); // json += "/" + topic; // byte[] message = Encoding.UTF8.GetBytes(json); // im.ExchangeDeclare("amq.topic", ExchangeType.Topic, durable: true); // im.BasicPublish("amq.topic", topic, null, message); // } //} }
protected void Button1_Click(object sender, EventArgs e) { MessageInfo msg = new MessageInfo(); msg.MessageID = "1"; msg.MessageBody = "Msg " + DateTime.Now.ToString(); msg.MessageTitle = "1"; msg.MessageRouter = "order.notice.lisi"; MQHelper.Publish(msg); }
public void MQHelperUnitTests() { int counter = 0; Console.WriteLine("绑定发布者 ..."); MQHelper.BindPublisher(); Console.WriteLine("订阅主题 ..."); var handler = MQHelper.Subscribe("测试订阅者", new[] { "1" }, (source, topic, message) => { counter++; Console.WriteLine($"收到 {source} 发来的消息:{topic} - {message}"); }); // 发布者和订阅者任何一端断开或重连,另一端不需要任何操作,可以继续收发 MQHelper.Publish("测试发布者", "1.1", "股票打新咯!!!-1"); MQHelper.DisconnectPublisher(); Thread.Sleep(100); MQHelper.Publish("测试发布者", "1.2", "股票打新咯!!!-2"); Thread.Sleep(100); MQHelper.BindPublisher(); Thread.Sleep(100); MQHelper.Publish("测试发布者", "1.3", "股票打新咯!!!-3"); Thread.Sleep(100); handler.Disconnect(); Thread.Sleep(100); MQHelper.Publish("测试发布者", "1.4", "股票打新咯!!!-4"); Thread.Sleep(100); Thread.Sleep(100); handler.Connect(); Thread.Sleep(100); MQHelper.Publish("测试发布者", "1.5", "股票打新咯!!!-5"); // 自动过滤自己发布的消息,避免消息循环 Thread.Sleep(100); MQHelper.Publish("测试订阅者", "1.6", "股票打新咯!!!-6"); Thread.Sleep(100); // 释放订阅者 handler.Dispose(); Thread.Sleep(100); MQHelper.Publish("测试订阅者", "1.7", "股票打新咯!!!-7"); Console.WriteLine($"测试结束,计数器: {counter}"); // 仅收到 1、3、5 消息即为正常 Assert.AreEqual(3, counter); }
public ActionResult Index() { Message msg = new Message() { MessageID = 1.ToString(), MessageBody = DateTime.Now.ToString(), MessageRouter = "testKey", //在127.0.0.1:15672上定义好的Exchanges MessageTitle = "测试" }; MQHelper.Publish(msg); return(View()); }
//入组执行发送 private void FilterSend(string url, int primaryKey) { //DateTime minTime = _ingroupService.GetMinAndMaxTime().Item1; DateTime maxTime = _ingroupService.GetMinAndMaxTime().Item2.AddDays(1); DateTime minTime = Convert.ToDateTime("2017-04-05"); for (DateTime dt = minTime; dt <= maxTime; dt = dt.AddDays(60)) { var starTime = dt; var endTime = dt.AddDays(60) < maxTime?dt.AddDays(60) : maxTime; string message = string.Format("{0}?dateBegin={1}&dateEnd={2}&patientNo={3}", url, starTime, endTime); //var body = Encoding.UTF8.GetBytes(message); MyMessage msg = new MyMessage(); msg.MessageID = "1"; msg.MessageRouter = "Filter_Condition"; msg.MessageBody = message; MQHelper.Publish(msg); } }
/// <summary> /// EasyNetQ封装方法 /// </summary> public static void EasyNetQProducer() { try { while (true) { var value = Console.ReadLine(); Message msg = new Message(); msg.MessageID = "1"; msg.MessageBody = DateTime.Now.ToString() + ":" + value; msg.MessageTitle = "1"; msg.MessageRouter = "llj"; MQHelper.Publish(msg); } } catch (EasyNetQException ex) { //处理连接消息服务器异常 // MessageHelper.WriteFuntionExceptionLog("Publish", ex.Message + " | " + ex.StackTrace); } }