c# rabbitmq 简单收发消息的示例代码
发布消息:(生产者)
//////发送消息 /// ///队列名 /// 消息内容 privatestaticvoidPublishInfo(stringqueue,stringmessage) { try { varfactory=newConnectionFactory(); factory.HostName="localhost";//RabbitMQ服务在本地运行 factory.UserName="guest";//用户名 factory.Password="guest";//密码 using(varconnection=factory.CreateConnection()) { using(varchannel=connection.CreateModel()) { booldurable=true;//将消息标记为持久性 channel.QueueDeclare(queue,durable,false,false,null); IBasicPropertiesproperties=channel.CreateBasicProperties(); properties.DeliveryMode=2; varbody=Encoding.UTF8.GetBytes(message); channel.BasicPublish("",queue,properties,body);//开始传递 } } } catch(System.Exceptionex) { Console.WriteLine(ex.ToString()); } Console.ReadLine(); }
使用:
PublishInfo("test","222222222222");
点击test进入队列内部:
消费消息:
方法一:旧版可用
privatestaticvoidConsumeInfo(stringqueue)
{
try
{
varfactory=newConnectionFactory();
factory.HostName="localhost";//RabbitMQ服务在本地运行
factory.UserName="guest";//用户名
factory.Password="guest";//密码
using(varconnection=factory.CreateConnection())
{
using(varchannel=connection.CreateModel())
{
//booldurable=true;//将消息标记为持久性
//channel.QueueDeclare("sljcgx:finish-queue",durable,false,false,null);//声明队列,队列不存在则创建该队列
//设置prefetchCount:1来告知RabbitMQ,在未收到消费端的消息确认时,不再分发消息,也就确保了当消费端处于忙碌状态时,不再分配任务。
channel.BasicQos(prefetchSize:0,prefetchCount:1,global:false);
varconsumer=newQueueingBasicConsumer(channel);
//false为手动确认,获取消息后需要添加channel.BasicAck(ea.DeliveryTag,false)确认
//true则为自动确认,不需要这行代码
channel.BasicConsume(queue,false,consumer);
while(true)
{
try
{
varea=(BasicDeliverEventArgs)consumer.Queue.Dequeue();//阻塞函数,获取队列中的消息
varbodyReceive=ea.Body;
stringmessage=Encoding.UTF8.GetString(bodyReceive);
Console.WriteLine(message);
channel.BasicAck(ea.DeliveryTag,false);//消息确认信号,确认后该消息将从队列里移除
}
catch(System.Exceptionex)
{
Console.WriteLine(ex.Message);
}
}
}
}
}
catch(System.Exceptionex)
{
Console.WriteLine(ex.ToString());
}
Console.ReadLine();
}
方法二:(通用)
privatestaticvoidConsumeInfo2(stringqueue)
{
try
{
varfactory=newConnectionFactory();
factory.HostName="localhost";//RabbitMQ服务在本地运行
factory.UserName="guest";//用户名
factory.Password="guest";//密码
varconnection=factory.CreateConnection();
varchannel=connection.CreateModel();
channel.QueueDeclare(queue,true,false,false,null);//声明队列,队列不存在则创建该队列
channel.BasicQos(0,1,false);//公平分发、同一时间只处理一个消息。
varconsumer=newEventingBasicConsumer(channel);//消费者(指定消息通道)该事件在接收到消息时触发
consumer.Received+=(sender,e)=>
{
byte[]body=e.Body.ToArray();//消息字节数组
stringmessage=Encoding.UTF8.GetString(body);//消息内容
Console.WriteLine(message);
channel.BasicAck(e.DeliveryTag,false);//手工确认
};
channel.BasicConsume(queue,false,consumer);//消费消息(在当前通道中监听queue队列,并进行消费)
Console.ReadLine();
connection.Close();
channel.Close();
}
catch(System.Exceptionex)
{
Console.WriteLine(ex.ToString());
}
}
到此这篇关于c#rabbitmq简单收发消息的示例代码的文章就介绍到这了,更多相关c#rabbitmq收发消息内容请搜索毛票票以前的文章或继续浏览下面的相关文章希望大家以后多多支持毛票票!
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。