public EmailQueueConsumer(IConfigProvider configProvider, IQueueOperator queueOperator)
        {
            string connectUri = $"activemq:tcp://{configProvider.GetActiveMqHost()}:{configProvider.GetActiveMqPort()}";

            AutoResetEvent queueConnectedEvent = new AutoResetEvent(false);

            queueOperator.TryConnect(connectUri, configProvider.GetActiveMqUser(), configProvider.GetActiveMqPassword(),
                                     queueConnectedEvent, -1, 2000);

            if (queueConnectedEvent.WaitOne())
            {
                queueOperator.AddMessageListener("queue://Email", MessagesListener);
            }
            else
            {
                Cancel();
            }
        }
示例#2
0
        public IActionResult SendEmail([FromBody] SendEmailViewModel details)
        {
            try
            {
                var emailCsv = details.Email;

                string connectUri = $"activemq:tcp://{_configProvider.GetActiveMqHost()}:{_configProvider.GetActiveMqPort()}";

                var queueConnectedEvent = new AutoResetEvent(false);

                _queueOperator.TryConnect(connectUri, _configProvider.GetActiveMqUser(),
                                          _configProvider.GetActiveMqPassword(), queueConnectedEvent, 10000, 2000);

                if (queueConnectedEvent.WaitOne(10000))
                {
                    var propertyDict = new Dictionary <string, string>
                    {
                        { "email_recipients", emailCsv },
                        { "email_subject", "Forgotten Email" }
                    };
                    _queueOperator.ProduceMessage("Hello there!", propertyDict);

                    _queueOperator.Dispose();
                    return(Ok("Sent"));
                }
            }
            catch (Exception ex)
            {
                _queueOperator.Dispose();
                return(StatusCode(500, "Could not connect to ActiveMQ service." + ex));
            }
            return(StatusCode(500, "Could not connect to ActiveMQ service."));
        }