static void Main(string[] args) { // init connection to activeMQ IConnectionFactory factory = new ConnectionFactory("tcp://localhost:61616"); ((ConnectionFactory)factory).PrefetchPolicy.SetAll(0); IConnection connection = factory.CreateConnection(); connection.Start(); ISession session = connection.CreateSession(); IDestination requestDestination = session.GetQueue(HashFinderConfig.RequestQueueName); IMessageConsumer consumer = session.CreateConsumer(requestDestination); IDestination responseDestination = session.GetQueue(HashFinderConfig.ResponseQueueName); IMessageProducer producer = session.CreateProducer(responseDestination); // ----------------------------------------------------------------------------------------------- // read worker-messages IMessage message; while ((message = consumer.Receive(TimeSpan.FromMinutes(1))) != null) { // map message body to our strongly typed message type var objectMessage = message as IObjectMessage; if (objectMessage?.Body is RequestMessage mapMessage) { // debug output Console.WriteLine(mapMessage.PinToCalculate); // analyze pin if (PinUtil.GetMD5Hash(mapMessage.PinToCalculate) == mapMessage.ResultHash) { // pin found! Console.WriteLine( $"yeaaah found the hash {mapMessage.PinToCalculate} - {mapMessage.ResultHash}"); producer.Send(producer.CreateObjectMessage( new ResponseMessage() { ResultPin = mapMessage.PinToCalculate, ResultHash = mapMessage.ResultHash })); break; } } } while (consumer.Receive(TimeSpan.FromSeconds(1)) != null) { } Console.WriteLine("... processing finished"); Console.WriteLine("Press enter to continue..."); Console.ReadLine(); }
static void Main(string[] args) { // connect to activeMQ IConnectionFactory factory = new ConnectionFactory(HashFinderConfig.ActiveMQEndpoint); IConnection connection = factory.CreateConnection(); connection.Start(); ISession session = connection.CreateSession(); IDestination requestDestination = session.GetQueue(HashFinderConfig.RequestQueueName); IMessageProducer producer = session.CreateProducer(requestDestination); IDestination responseDestination = session.GetQueue(HashFinderConfig.ResponseQueueName); IMessageConsumer consumer = session.CreateConsumer(responseDestination); // ----------------------------------------------------------------------------------------------- // handle UI input and calculate Hash Console.Write("Please enter your pin: "); var pin = Console.ReadLine(); var hashedPin = PinUtil.GetMD5Hash(pin); Console.WriteLine(); Console.WriteLine("your hash is: " + hashedPin); // ----------------------------------------------------------------------------------------------- var itemFound = false; Task t = Task.Run(() => { // create messages for workers for (int i = 0; i <= 9999 && !itemFound; i++) { var objectMessage = producer.CreateObjectMessage( new RequestMessage() { PinToCalculate = i.ToString("0000"), ResultHash = hashedPin, }); producer.Send(objectMessage); } }); // ----------------------------------------------------------------------------------------------- // read worker-responses IMessage message; if ((message = consumer.Receive(TimeSpan.FromMinutes(1))) != null) { itemFound = true; // map message body to our strongly typed message type var objectMessage = message as IObjectMessage; var mapMessage = objectMessage?.Body as ResponseMessage; Console.WriteLine($"pin is: {mapMessage.ResultPin} ({mapMessage.ResultHash})"); } Task.WaitAll(new[] { t }); // final UI candy Console.WriteLine("... processing finished"); Console.WriteLine("Press enter to continue..."); Console.ReadLine(); }