|
|
@@ -0,0 +1,59 @@
|
|
|
+using System;
|
|
|
+using System.Collections.Generic;
|
|
|
+using System.Text;
|
|
|
+using RabbitMQ.Client;
|
|
|
+using RabbitMQ.Client.Events;
|
|
|
+using Library;
|
|
|
+using System.Threading;
|
|
|
+
|
|
|
+namespace MySystem
|
|
|
+{
|
|
|
+ public class RabbitMQPrize
|
|
|
+ {
|
|
|
+ public readonly static RabbitMQPrize Instance = new RabbitMQPrize();
|
|
|
+ string UserName,Password,HostName,VirtualHostName;
|
|
|
+ private RabbitMQPrize()
|
|
|
+ {
|
|
|
+ UserName = ConfigurationManager.AppSettings["MqPrizeUserName"].ToString();
|
|
|
+ Password = ConfigurationManager.AppSettings["MqPrizePassword"].ToString();
|
|
|
+ HostName = ConfigurationManager.AppSettings["MqPrizeHostName"].ToString();
|
|
|
+ VirtualHostName = ConfigurationManager.AppSettings["MqPrizeVirtualHostName"].ToString();
|
|
|
+ }
|
|
|
+
|
|
|
+ #region 单对单发送
|
|
|
+ public void SendMsg(string content, string QueueName)
|
|
|
+ {
|
|
|
+ _channel_send.BasicPublish("", QueueName, null, Encoding.Default.GetBytes(content));
|
|
|
+ function.WriteLog(DateTime.Now.ToString() + "\n" + QueueName + "\n" + content + "\n\n\n", "SendMsg2");
|
|
|
+ }
|
|
|
+ #endregion
|
|
|
+
|
|
|
+
|
|
|
+ #region 单对单接收
|
|
|
+ public static IModel _channel_send;
|
|
|
+ public void CreateConn(string QueueName)
|
|
|
+ {
|
|
|
+ var factory = new ConnectionFactory()
|
|
|
+ {
|
|
|
+ UserName = UserName,
|
|
|
+ Password = Password,
|
|
|
+ AutomaticRecoveryEnabled = true, //如果connection挂掉是否重新连接
|
|
|
+ TopologyRecoveryEnabled = true, //连接恢复后,连接的交换机,队列等是否一同恢复
|
|
|
+ VirtualHost = VirtualHostName,
|
|
|
+ };
|
|
|
+ List<AmqpTcpEndpoint> p = new List<AmqpTcpEndpoint>();
|
|
|
+ string[] HostNames = HostName.Split(',');
|
|
|
+ foreach (string subHostName in HostNames)
|
|
|
+ {
|
|
|
+ string[] subHostNameData = subHostName.Split(':');
|
|
|
+ p.Add(new AmqpTcpEndpoint(subHostNameData[0], int.Parse(subHostNameData[1])));
|
|
|
+ }
|
|
|
+ var conn = factory.CreateConnection(p);
|
|
|
+ _channel_send = conn.CreateModel();
|
|
|
+ _channel_send.ExchangeDeclare("kxs_direct_ranch", "direct", true);
|
|
|
+ _channel_send.QueueDeclare(QueueName, true, false, false);
|
|
|
+ _channel_send.QueueBind(QueueName, "kxs_direct_ranch", QueueName);
|
|
|
+ }
|
|
|
+ #endregion
|
|
|
+ }
|
|
|
+}
|