本文整理汇总了C#中RabbitMQ.Client.Events.EventingBasicConsumer类的典型用法代码示例。如果您正苦于以下问题:C# EventingBasicConsumer类的具体用法?C# EventingBasicConsumer怎么用?C# EventingBasicConsumer使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
EventingBasicConsumer类属于RabbitMQ.Client.Events命名空间,在下文中一共展示了EventingBasicConsumer类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: StartListening
public override void StartListening()
{
_rabbitConnection = Configuration.ConnectionFactory.CreateConnection();
_publishModel = _rabbitConnection.CreateModel();
_subscribeModel = _rabbitConnection.CreateModel();
_publishModel.ExchangeDeclare(Configuration.StampExchangeName, "x-stamp", durable: true);
_subscribeModel.ExchangeDeclare(Configuration.ExchangeName, ExchangeType.Fanout, durable: true);
_subscribeModel.QueueDeclare(Configuration.QueueName, durable: false, exclusive: false, autoDelete: true, arguments: new Dictionary<string, object>());
_subscribeModel.QueueBind(Configuration.QueueName, Configuration.ExchangeName, "");
var consumer = new EventingBasicConsumer(_subscribeModel);
consumer.Received += (sender, args) =>
{
try
{
OnMessage(new RabbitMqMessageWrapper
{
Bytes = args.Body,
Id = Convert.ToUInt64(args.BasicProperties.Headers["stamp"])
});
}
finally
{
_subscribeModel.BasicAck(args.DeliveryTag, multiple: false);
}
};
_subscribeModel.BasicConsume(Configuration.QueueName, noAck: false, consumer: consumer);
}
示例2: RabbitMqConsumer
public RabbitMqConsumer(Uri uri, string exchange, IEnumerable<string> routingKeys)
{
var connectionFactory = new ConnectionFactory
{
Uri = uri.ToString(),
};
_connection = connectionFactory.CreateConnection();
_model = _connection.CreateModel();
_model.ExchangeDeclare(exchange, ExchangeType.Topic, false);
var queueName = string.Format("test-{0}", Guid.NewGuid());
_model.QueueDeclare(
queueName,
false,
false,
true,
null);
foreach (var routingKey in routingKeys)
{
_model.QueueBind(
queueName,
exchange,
routingKey,
null);
}
_eventingBasicConsumer = new EventingBasicConsumer(_model);
_eventingBasicConsumer.Received += OnReceived;
_model.BasicConsume(queueName, false, _eventingBasicConsumer);
}
示例3: Consumer
public Consumer(string queueName, IDictionary<string, string> headers, bool matchAll = true)
{
var factory = new ConnectionFactory {HostName = "localhost"};
_connection = factory.CreateConnection();
_channel = _connection.CreateModel();
var args = new Dictionary<String, Object>();
if (headers != null)
{
args.Add("x-match", matchAll ? "all" : "any");
foreach (var item in headers)
{
args.Add(item.Key, item.Value);
}
}
_channel.ExchangeDeclare(ExchangeName, "headers", true);
_channel.QueueDeclare(queueName, true, false, false, args);
_channel.QueueBind(queueName, ExchangeName, string.Empty);
const bool nonTransactional = true;
_consumer = new EventingBasicConsumer();
_consumer.Received += ConsumerReceived;
_channel.BasicConsume(queueName, nonTransactional, _consumer);
}
示例4: Main
private static void Main(string[] args)
{
Console.WriteLine("----------Sad Receiver----------");
Console.WriteLine("Press [Enter] to exit.");
var factory = new ConnectionFactory() { HostName = "localhost" };
using (var connection = factory.CreateConnection())
using (var channel = connection.CreateModel())
{
channel.ExchangeDeclare(exchange: "Emotional_Messages",
type: "direct");
var queueName = channel.QueueDeclare().QueueName;
channel.QueueBind(queue: queueName,
exchange: "Emotional_Messages",
routingKey: "Sad");
Console.WriteLine("Waiting for messages...");
var consumer = new EventingBasicConsumer(channel);
consumer.Received += (model, ea) =>
{
var body = ea.Body;
var message = Encoding.UTF8.GetString(body);
Console.WriteLine(" Received {0}", message);
};
channel.BasicConsume(queue: queueName,
noAck: true,
consumer: consumer);
Console.WriteLine(" Press [enter] to exit.");
Console.ReadLine();
}
}
示例5: Main
public static void Main()
{
var factory = new ConnectionFactory() { HostName = "localhost" };
using(var connection = factory.CreateConnection())
using(var channel = connection.CreateModel())
{
channel.QueueDeclare(queue: "task_queue", durable: true, exclusive: false, autoDelete: false, arguments: null);
channel.BasicQos(prefetchSize: 0, prefetchCount: 1, global: false);
Console.WriteLine(" [*] Waiting for messages.");
var consumer = new EventingBasicConsumer(channel);
consumer.Received += (model, ea) =>
{
var body = ea.Body;
var message = Encoding.UTF8.GetString(body);
Console.WriteLine(" [x] Received {0}", message);
int dots = message.Split('.').Length - 1;
Thread.Sleep(dots * 1000);
Console.WriteLine(" [x] Done");
channel.BasicAck(deliveryTag: ea.DeliveryTag, multiple: false);
};
channel.BasicConsume(queue: "task_queue", noAck: false, consumer: consumer);
Console.WriteLine(" Press [enter] to exit.");
Console.ReadLine();
}
}
示例6: HandleQueueItems
public static void HandleQueueItems()
{
var factory = new ConnectionFactory { HostName = "localhost" };
using (var connection = factory.CreateConnection())
using (var channel = connection.CreateModel())
{
channel.QueueDeclare(queue: "VehicleQueue",
durable: false,
exclusive: false,
autoDelete: false,
arguments: null);
var consumer = new EventingBasicConsumer(channel);
consumer.Received += (model, ea) =>
{
var body = ea.Body;
var message = Encoding.UTF8.GetString(body);
Console.WriteLine(" [x] Received {0}", message);
PluginManager.HandleMessageForLoadedPlugins(new ExampleMessage { BodyText = message });
};
channel.BasicConsume(queue: "VehicleQueue",
noAck: true,
consumer: consumer);
Thread.Sleep(new TimeSpan(0, 0, 10));
}
}
示例7: Connect
private void Connect()
{
var cf = new ConnectionFactory();
_connection = cf.CreateConnection();
_channel = _connection.CreateModel();
_channel.ExchangeDeclare(_configurationService.WorkersExchange, ExchangeType.Direct);
// Listen for heartbeats
_channel.QueueDeclare(_configurationService.WorkerPingQueue, false, false, true, null);
_channel.QueueBind(_configurationService.WorkerPingQueue, _configurationService.WorkersExchange, _configurationService.WorkerPingQueue);
var heartbeatConsumer = new EventingBasicConsumer(_channel);
heartbeatConsumer.Received += OnReceivedHeartbeat;
heartbeatConsumer.ConsumerCancelled += OnConsumerCancelled;
heartbeatConsumer.Shutdown += OnShutdown;
_channel.BasicConsume(_configurationService.WorkerPingQueue, true, heartbeatConsumer);
// Listen or status changes
_channel.QueueDeclare(_configurationService.WorkerStatusQueue, false, false, true, null);
_channel.QueueBind(_configurationService.WorkerStatusQueue, _configurationService.WorkersExchange, _configurationService.WorkerStatusQueue);
var statusConsumer = new EventingBasicConsumer(_channel);
statusConsumer.Received += OnReceivedStatus;
statusConsumer.ConsumerCancelled += OnConsumerCancelled;
statusConsumer.Shutdown += OnShutdown;
_channel.BasicConsume(_configurationService.WorkerStatusQueue, false, statusConsumer);
}
示例8: Main
private static void Main(string[] args)
{
var factory = new ConnectionFactory() { Uri = "amqp://nopqbtng:[email protected]/nopqbtng" };
using (var connection = factory.CreateConnection())
using (var channel = connection.CreateModel())
{
channel.ExchangeDeclare(exchange: "OnAMission", type: "fanout");
var queueName = channel.QueueDeclare().QueueName;
channel.QueueBind(queue: queueName,
exchange: "OnAMission",
routingKey: "");
Console.WriteLine(" [*] Waiting for logs.");
var consumer = new EventingBasicConsumer(channel);
consumer.Received += (model, ea) =>
{
var body = ea.Body;
var message = Encoding.UTF8.GetString(body);
Console.WriteLine(" [x] {0}", message);
};
channel.BasicConsume(queue: queueName,
noAck: true,
consumer: consumer);
Console.WriteLine(" Press [enter] to exit.");
Console.ReadLine();
}
}
示例9: Main
private static void Main(string[] args)
{
Console.WriteLine("----------Receiver (ALL HAPPY MESSAGES)----------");
Console.WriteLine("Press [Enter] to exit.");
var factory = new ConnectionFactory() { HostName = "localhost" };
using (var connection = factory.CreateConnection())
using (var channel = connection.CreateModel())
{
channel.ExchangeDeclare(exchange: "topic_example", type: "topic");
var queueName = channel.QueueDeclare().QueueName;
channel.QueueBind(queue: queueName,
exchange: "topic_example",
routingKey: "Happy.*");
Console.WriteLine("Waiting for messages...");
var consumer = new EventingBasicConsumer(channel);
consumer.Received += (model, ea) =>
{
var body = ea.Body;
var message = Encoding.UTF8.GetString(body);
var routingKey = ea.RoutingKey;
Console.WriteLine(" Received '{0}':'{1}'",
routingKey,
message);
};
channel.BasicConsume(queue: queueName,
noAck: true,
consumer: consumer);
Console.ReadLine();
}
}
示例10: Main
static void Main(string[] args)
{
var factory = GetConnectionFactory();
using (var connection = factory.CreateConnection())
{
using (var channel = connection.CreateModel())
{
DeclareExchange(channel);
DeclareQ(channel, Constants.MASTER_CUSTOMER_QUEUE_NAME);
SetUpQoS(channel);
BindToQ(channel, Constants.MASTER_CUSTOMER_QUEUE_NAME, Constants.NEW_CUSTOMER_EVENT);
Console.WriteLine(" [CRM] Waiting for messages.");
var consumer = new EventingBasicConsumer(channel);
consumer.Received += (Model, ea) =>
{
var body = ea.Body;
var message = Encoding.UTF8.GetString(body);
var routingKey = ea.RoutingKey;
Console.WriteLine(" [>>>>>>>>>>] Received MESSAGE '{0}'", routingKey);
ProcessMessage(channel, message, routingKey);
channel.BasicAck(deliveryTag: ea.DeliveryTag, multiple: false);
};
channel.BasicConsume(queue: Constants.MASTER_CUSTOMER_QUEUE_NAME, noAck: false, consumer: consumer);
Console.WriteLine(" Press [enter] to exit.");
Console.ReadLine();
}
}
}
示例11: Rabbit
public Rabbit(string address)
{
cf = new ConnectionFactory();
//cf.Port = 35672;
cf.UserName = "eventHub";
cf.Password = "18daigaku75";
cf.Address = address;
conn = cf.CreateConnection();
IModel c = Connect();
conn.AutoClose = false;
UriSettings();
_address = address.Substring(0, address.IndexOf(":"));
ProcessQueues();
System.Timers.Timer aTimer = new System.Timers.Timer();
aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
aTimer.Interval = 60 * 1000;
aTimer.Enabled = true;
var consumer = new EventingBasicConsumer();
consumer.Received += (model, ea) =>
{
ProcessQueues();
Console.WriteLine("Received event");
};
c.BasicConsume("task_queue", false, null, consumer);
Console.ReadKey();
}
示例12: StartListening
public override void StartListening()
{
_rabbitConnection = _configuration.RabbitMqClusterMembers?.Count > 0
? _configuration.ConnectionFactory.CreateConnection(_configuration.RabbitMqClusterMembers)
: _configuration.ConnectionFactory.CreateConnection();
_publishModel = _rabbitConnection.CreateModel();
_receiveModel = _rabbitConnection.CreateModel();
_receiveModel.ExchangeDeclare(_configuration.ExchangeName, ExchangeType.Fanout, true);
var queue = _configuration.QueueName == null
? _receiveModel.QueueDeclare()
: _receiveModel.QueueDeclare(_configuration.QueueName, true, false, false,
new Dictionary<string, object>());
_receiveModel.QueueBind(queue.QueueName, _configuration.ExchangeName, "");
var consumer = new EventingBasicConsumer(_receiveModel);
consumer.Received += (sender, args) =>
{
var message = new RabbitMqMessageWrapper
{
Bytes = args.Body,
Id =
Convert.ToUInt64(Encoding.UTF8.GetString((byte[]) args.BasicProperties.Headers[MessageIdHeader]))
};
OnMessage(message);
};
_receiveModel.BasicConsume(queue.QueueName, true, consumer);
}
示例13: TestEventingConsumerRegistrationEvents
public void TestEventingConsumerRegistrationEvents()
{
string q = Model.QueueDeclare();
var registeredLatch = new ManualResetEvent(false);
object registeredSender = null;
var unregisteredLatch = new ManualResetEvent(false);
object unregisteredSender = null;
EventingBasicConsumer ec = new EventingBasicConsumer(Model);
ec.Registered += (s, args) =>
{
registeredSender = s;
registeredLatch.Set();
};
ec.Unregistered += (s, args) =>
{
unregisteredSender = s;
unregisteredLatch.Set();
};
string tag = Model.BasicConsume(q, false, ec);
Wait(registeredLatch);
Assert.IsNotNull(registeredSender);
Assert.AreEqual(ec, registeredSender);
Assert.AreEqual(Model, ((EventingBasicConsumer)registeredSender).Model);
Model.BasicCancel(tag);
Wait(unregisteredLatch);
Assert.IsNotNull(unregisteredSender);
Assert.AreEqual(ec, unregisteredSender);
Assert.AreEqual(Model, ((EventingBasicConsumer)unregisteredSender).Model);
}
示例14: Main
public static void Main()
{
var factory = new ConnectionFactory() { HostName = "localhost" };
using (var connection = factory.CreateConnection())
using (var channel = connection.CreateModel())
{// tutaj tez deklarujemy kolejke, ze wzgledu na to, iż możemy rozpoczac odbnieranie nim wysłannik wyśle wiadomość
//chcemy miec pewnośc, że kolejka istnieje
channel.QueueDeclare(queue: "hello",
durable: false,
exclusive: false,
autoDelete: false,
arguments: null);
//mowimy serwerowi by dostarczył nam wiadomośc z kolejki
var consumer = new EventingBasicConsumer(channel);
consumer.Received += (model, ea) =>
{
var body = ea.Body;
var message = Encoding.UTF8.GetString(body);
Console.WriteLine(" [x] Received {0}", message);
};
channel.BasicConsume(queue: "hello",
noAck: true,
consumer: consumer);
Console.WriteLine(" Press [enter] to exit.");
Console.ReadLine();
}
}
示例15: Subscribe
public void Subscribe(string queueName, EventHandler<BasicDeliverEventArgs> handler)
{
CreateQueue(queueName);
var consumer = new EventingBasicConsumer(_model);
consumer.Received += handler;
_model.BasicConsume(queueName, true, consumer);
}