Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            Console.SetOut(new PrefixedWriter());

            var database = new List <API.DemoMessage>();

            var connection = new BrokerConnection();

            connection.Open();
            Console.WriteLine("Connected");

            connection.SubscribeRead();
            connection.SubscribeWrite();
            Console.WriteLine("Subscribed to \'demo.read.single\' and \'demo.write.single\'");

            connection.WriteToDatabaseEvent += (o, e) =>
            {
                var match = database.FirstOrDefault(message => message.Id == e.Message.Id);
                if (match == null)
                {
                    database.Add(e.Message);
                    Console.WriteLine("Added a new message to database: Id: [{0}], description: {1}", e.Message.Id, e.Message.Description);
                }
                else
                {
                    match.Description = e.Message.Description;
                    match.Updated     = e.Message.Updated;
                    Console.WriteLine("Updated message: Id: [{0}], description: {1}, updated: {2}", e.Message.Id, e.Message.Description, e.Message.Updated.ToDateTime());
                }
            };
            connection.ReadFromDatabaseEvent += (o, e) =>
            {
                Console.WriteLine("Got read from database request for ID: [{0}], with reply queue: [{1}]", e.Message.Id, e.ReplyQueue);
                var match = database.FirstOrDefault(message => message.Id == e.Message.Id);
                if (match == null)
                {
                    Console.WriteLine("No match found in database for ID: [{0}], aborting request", e.Message.Id);
                    return;
                }
                connection.PublishRead(e.ReplyQueue, match);
                Console.WriteLine("Match found, message written to queue: [{0}],\n" +
                                  "Id: [{1}], description: [{2}], updated: [{3}]", e.ReplyQueue, match.Id, match.Description, match.Updated);
            };

            Console.ReadKey();

            connection.Close();
            Console.WriteLine("Disconnected");
        }
Ejemplo n.º 2
0
        public static async Task Main(string[] args)
        {
            Console.SetOut(new PrefixedWriter());

            var connection = new BrokerConnection();

            connection.Open();
            Console.WriteLine("Connected");
            // Subscribe to any requests received
            connection.Subscribe();
            Console.WriteLine("Subscribed to to anonymous queue");
            // Produce couple of messages to queue
            var msg1 = new API.DemoMessage
            {
                Id          = 10,
                Description = "First message",
                Updated     = Timestamp.FromDateTime(DateTime.Now.ToUniversalTime())
            };
            var msg2 = new API.DemoMessage
            {
                Id          = 20,
                Description = "Second message",
                Updated     = Timestamp.FromDateTime(DateTime.Now.ToUniversalTime())
            };

            connection.Produce(msg1);
            connection.Produce(msg2);
            Console.WriteLine("Produced 2 messages to queue: \'demo.write.single\'");
            // Ask for some message from queue
            try
            {
                Console.WriteLine("Sent request for queue: \'demo.read.single\' with id: 10");
                var received1 = await TimeoutAfter(Requests.RequestMessage(connection, 10), TimeSpan.FromSeconds(2));

                Console.WriteLine("Received message: ID: [{0}], Description: [{1}], Updated: [{2}]", received1.Id, received1.Description, received1.Updated);
            }
            catch (TimeoutException e)
            {
                Console.WriteLine(e.Message);
            }
            connection.Close();
            Console.WriteLine("Disconnected");
        }