引用RabbitMQ.Client.dll
//参数:message 消息
// hostName 请求服务器地址,默认本机
// QueueName 队列名称
// port 请求端口号 默认 5672
public static bool Push(string message, string hostName = "localhost", string QueueName = "push_queue", int port = 5672)
{
if (hostName == null) { hostName = "localhost"; }
if (QueueName == null) { QueueName = "push_queue"; }
if (port == null) { port = 5672; }
try
{
ConnectionFactory factory = new ConnectionFactory();
factory.HostName = hostName;
factory.Port = port;
IConnection conn = factory.CreateConnection();
IModel channel = conn.CreateModel();
//在MQ上定义一个持久化队列
channel.QueueDeclare(QueueName, true, false, false, null);
byte[] bytes = Encoding.UTF8.GetBytes(message);
//设置消息持久化
IBasicProperties properties = channel.CreateBasicProperties();
properties.DeliveryMode = 2;
channel.BasicPublish("", QueueName, properties, bytes);
return true;
}
catch (Exception ex)
{
return false;
}
}