Esempio n. 1
0
        public void Publicar(ObjetoPersonalisado obj)
        {
            var factory = new ConnectionFactory()
            {
                HostName = "localhost"
            };

            using (var connection = factory.CreateConnection())
                using (var channel = connection.CreateModel())
                {
                    channel.ConfirmSelect();
                    channel.BasicAcks  += Evento_Confirmacao;
                    channel.BasicNacks += Evento_NaoConfirmacao;

                    channel.QueueDeclare(queue: "order",
                                         durable: false,
                                         exclusive: false,
                                         autoDelete: false,
                                         arguments: null);

                    var json = Newtonsoft.Json.JsonConvert.SerializeObject(obj);

                    var body = Encoding.UTF8.GetBytes(json);

                    channel.BasicPublish(exchange: "",
                                         routingKey: "order",
                                         basicProperties: null,
                                         body: body);

                    Console.WriteLine("Mensagem enviada!");
                }
        }
Esempio n. 2
0
        static void Main(string[] args)
        {
            try
            {
                var msgEnviar = new ObjetoPersonalisado {
                    Id = Guid.NewGuid(), Mensagem = "Ola" + new Random().Next(1, 8).ToString()
                };

                new Publicador().Publicar(msgEnviar);

                new Receptor().Consumir();

                Console.WriteLine(" Press [enter] to exit.");
                Console.ReadLine();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }