메시지를 수신 할 구독자가없는 것과 관련하여 "영구적 인"및 "영구 모드"가 재부팅과 관련이있는 것 같습니다.구독자가없는 RabbitMQ 큐
구독자가없는 경우 RabbitMQ에서 메시지를 대기열에 보관하고 싶습니다. 가입자가 온라인 상태가되면 해당 가입자가 메시지를 수신해야합니다. RabbitMQ로 가능합니까?
코드 샘플 :
서버 :
namespace RabbitEg
{
class Program
{
private const string EXCHANGE_NAME = "helloworld";
static void Main(string[] args)
{
ConnectionFactory cnFactory = new RabbitMQ.Client.ConnectionFactory() { HostName = "localhost" };
using (IConnection cn = cnFactory.CreateConnection())
{
using (IModel channel = cn.CreateModel())
{
//channel.ExchangeDelete(EXCHANGE_NAME);
channel.ExchangeDeclare(EXCHANGE_NAME, "direct", true);
//channel.BasicReturn += new BasicReturnEventHandler(channel_BasicReturn);
for (int i = 0; i < 100; i++)
{
byte[] payLoad = Encoding.ASCII.GetBytes("hello world _ " + i);
IBasicProperties channelProps = channel.CreateBasicProperties();
channelProps.SetPersistent(true);
channel.BasicPublish(EXCHANGE_NAME, "routekey_helloworld", false, false, channelProps, payLoad);
Console.WriteLine("Sent Message " + i);
System.Threading.Thread.Sleep(25);
}
Console.ReadLine();
}
}
}
}
}
클라이언트 :
namespace RabbitListener
{
class Program
{
private const string EXCHANGE_NAME = "helloworld";
static void Main(string[] args)
{
ConnectionFactory cnFactory = new ConnectionFactory() { HostName = "localhost" };
using (IConnection cn = cnFactory.CreateConnection())
{
using (IModel channel = cn.CreateModel())
{
channel.ExchangeDeclare(EXCHANGE_NAME, "direct", true);
string queueName = channel.QueueDeclare("myQueue", true, false, false, null);
channel.QueueBind(queueName, EXCHANGE_NAME, "routekey_helloworld");
Console.WriteLine("Waiting for messages");
QueueingBasicConsumer consumer = new QueueingBasicConsumer(channel);
channel.BasicConsume(queueName, true, consumer);
while (true)
{
BasicDeliverEventArgs e = (BasicDeliverEventArgs)consumer.Queue.Dequeue();
Console.WriteLine(Encoding.ASCII.GetString(e.Body));
}
}
}
}
}
}
고마워, 나는 이것을 시도했다. 그러나 듣는 클라이언트가 없다면 메시지를 지속하지 않는다. 질문에 첨부 된 코드 샘플. –
좋은 코드입니다. 두 가지 문제점 : 1) 서버는 대기열을 선언해야합니다. 두 번 선언하는 것은 문제가되지 않으며 좋은 방법입니다. 2) queueDeclare()는 익명의 비 영구적 인 대기열을 제공합니다. 당신은 queueDeclare ("myQueue", true, false, false, null)를 원한다. – scvalex
또한 질문을 편집 한 방식으로 달성하려는 내용을 이해하기 어렵습니다. – scvalex