首页 > Windows开发 > 详细

c# 控制台程序编写RabbitMQ 生产者

时间:2021-08-24 20:36:43      阅读:12      评论:0      收藏:0      [点我收藏+]

using RabbitMQ.Client;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace .RabbitMQWorker
{
class Program
{
static string hostName = string.Empty;
static string userName = string.Empty;
static string password = string.Empty;
static string queueName = string.Empty;

static void Main(string[] args)
{
hostName = ConfigurationManager.AppSettings.Get("HostName");
userName = ConfigurationManager.AppSettings.Get("UserName");
password = ConfigurationManager.AppSettings.Get("Password");
queueName = ConfigurationManager.AppSettings.Get("QueueName");

var factory = new ConnectionFactory() { HostName = hostName, UserName = userName, Password = password };
using (var connection = factory.CreateConnection())
using (var channel = connection.CreateModel())
{
channel.QueueDeclare(queue: queueName,
durable: true,
exclusive: false,
autoDelete: false,
arguments: null);

Console.WriteLine(" Press message to publish.input exit to stop.");

string message = Console.ReadLine();

while (message.ToLower() != "exit")
{
var body = Encoding.UTF8.GetBytes(message);

var properties = channel.CreateBasicProperties();
properties.Persistent = true;

channel.BasicPublish(exchange: "",
routingKey: queueName,
basicProperties: properties,
body: body);
Console.WriteLine(" [x] Sent {0}", message);

message = Console.ReadLine();
}
}

Console.WriteLine(" Press [enter] to exit.");
Console.ReadLine();
}
}
}

c# 控制台程序编写RabbitMQ 生产者

原文:https://www.cnblogs.com/TallkingIsEasying/p/15181438.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!