public void Send(dynamic message) { try { /*Main entry point to the RabbitMQ .NET AMQP client*/ ConnectionFactory factory = new ConnectionFactory() { HostName = GggUtilities.HostName }; /*Create a connection to one of the endpoints provided by the IEndpointResolver * returned by the EndpointResolverFactory. By default the configured * hostname and port are used.*/ using (IConnection connection = factory.CreateConnection()) { // Create and return a fresh channel, session, and model. using (IModel channel = connection.CreateModel()) { /*Declaring a queue is idempotent - it will only be created * if it doesn't exist already.*/ channel.QueueDeclare(queue: QueueName, durable: IsDurable, exclusive: Exclusive, autoDelete: AutoDelete, arguments: Arguments); IBasicProperties basicProperties = null; if (IsDurable) { /*At this point we're sure that the task_queue queue won't be lost even * if RabbitMQ restarts. Now we need to mark our messages as persistent - * by setting IBasicProperties.SetPersistent to true.*/ basicProperties = channel.CreateBasicProperties(); basicProperties.Persistent = true; } /*The message content is a byte array, so you can encode * whatever you like there.*/ byte[] body = GggUtilities.DynamicToByteArray(message); /*BasicPublish(this IModel model, string exchange, * string routingKey, * IBasicProperties basicProperties, byte[] body)*/ channel.BasicPublish(exchange: Exchange, routingKey: RoutingKey, basicProperties: basicProperties, body: body); string messageString = message.ToString(); GggUtilities.WriteLog(this, "Exchange: " + Exchange + " " + "Queue: " + QueueName + " " + "RoutingKey: " + RoutingKey); GggUtilities.WriteLog(this, "Sent: " + messageString); } } } catch (Exception e) { GggUtilities.WriteLog(this, e); throw; } }
public void DeclareQueueTest() { string declareQueue = GggRabbitMq.DeclareQueue(); GggUtilities.WriteLog(this, "declareQueue: " + declareQueue); GggUtilities.ShowLog(); }
public void Receive() { try { ConnectionFactory factory = new ConnectionFactory() { HostName = GggUtilities.HostName }; // consumer.Received event not firing when channel.BasicConsume is called // https://groups.google.com/forum/#!topic/rabbitmq-users/l0GQ4w3sYEU IConnection connection = factory.CreateConnection(); IModel channel = connection.CreateModel(); Channel = channel; /*Declaring a queue is idempotent - it will only be created * if it doesn't exist already.*/ channel.QueueDeclare(queue: QueueName, durable: IsDurable, exclusive: Exclusive, autoDelete: AutoDelete, arguments: Arguments); // https://www.rabbitmq.com/tutorials/tutorial-two-dotnet.html /*we can use the basicQos method with the prefetchCount = 1 setting. * This tells RabbitMQ not to give more than one message to a worker at a time. * Or, in other words, don't dispatch a new message to a worker until it has processed * and acknowledged the previous one. Instead, it will dispatch it to the next * worker that is not still busy.*/ if (BasicQosEnabled) { channel.BasicQos(prefetchSize: 0, prefetchCount: 1, global: false); } /*Experimental class exposing an IBasicConsumer's * methods as separate events.*/ EventingBasicConsumer consumer = new EventingBasicConsumer(channel); consumer.Received += OnConsumerOnReceived; /*Start a Basic content-class consumer*/ /*Manual message acknowledgments are turned on by default. * In previous examples we explicitly turned them off by setting the autoAck * ("automatic acknowledgement mode") parameter to true. It's time to remove * this flag and manually send a proper acknowledgment from the worker, * once we're done with a task.*/ string logMessage = Name + " " + "AutoAck is " + AutoAck; GggUtilities.WriteLog(this, logMessage); channel.BasicConsume(queue: QueueName, autoAck: AutoAck, consumer: consumer); } catch (Exception e) { GggUtilities.WriteLog(this, e); throw; } }
private void OnConsumerOnReceived(object model, BasicDeliverEventArgs ea) { byte[] body = ea.Body; string message = Encoding.UTF8.GetString(body); // https://www.rabbitmq.com/tutorials/tutorial-two-dotnet.html if (AutoAck == false) { /*Using this code we can be sure that even if you kill a worker using * CTRL+C while it was processing a message, nothing will be lost. Soon after * the worker dies all unacknowledged messages will be redelivered.*/ Channel.BasicAck(deliveryTag: ea.DeliveryTag, multiple: false); GggUtilities.WriteLog(this, "OnConsumerOnReceived.Channel.BasicAck.deliveryTag: " + ea.DeliveryTag); } string logMessage = Name + " " + "Received: " + message; GggUtilities.WriteLog(this, "Queue: " + QueueName); GggUtilities.WriteLog(this, logMessage); }
public void PublishReadExchangeTest() { // timers double seconds = 3; double waitTime = TimeSpan.FromSeconds(seconds).TotalMilliseconds; // sent MessageSender sender = new MessageSender(GggUtilities.ExchangeLogs, GggUtilities.QueueHello); sender.SendMulti(5); // wait GggUtilities.WriteLog(this, "Waiting {0} seconds before receiver.".Replace("{0}", seconds.ToString())); System.Threading.Thread.Sleep((int)waitTime); // receiver MessageReceiver receiver = new MessageReceiver("receiver1", GggUtilities.QueueHello); receiver.Receive(); // wait GggUtilities.WriteLog(this, "Waiting {0} seconds for thread to finish.".Replace("{0}", seconds.ToString())); System.Threading.Thread.Sleep((int)waitTime); GggUtilities.ShowLog(); }
public void MessageDurabilityTest() { // timers double seconds = 3; double waitTime = TimeSpan.FromSeconds(seconds).TotalMilliseconds; // sent MessageSender sender = new MessageSender(GggUtilities.QueueDurable, null, autoDelete: false, exclusive: false, isDurable: true, routingKey: GggUtilities.QueueDurable); sender.SendMulti(10); // wait GggUtilities.WriteLog(this, "Waiting {0} seconds before receiver.".Replace("{0}", seconds.ToString())); System.Threading.Thread.Sleep((int)waitTime); // receiver MessageReceiver receiver = new MessageReceiver("receiver1", GggUtilities.QueueDurable, null, autoDelete: false, exclusive: false, isDurable: true); receiver.Receive(); // wait GggUtilities.WriteLog(this, "Waiting {0} seconds for thread to finish.".Replace("{0}", seconds.ToString())); System.Threading.Thread.Sleep((int)waitTime); GggUtilities.ShowLog(); }