/// <summary> /// Places an event on the message queue /// </summary> /// <param name="route">The topic/route of the event (ex. user.created)</param> /// <param name="eventdata">The event details</param> public void RaiseEvent(string route, Monosoft.Common.DTO.EventDTO eventdata) { Console.WriteLine("Event: " + route); byte[] bsonMessage = System.Text.Encoding.UTF8.GetBytes(Newtonsoft.Json.JsonConvert.SerializeObject(eventdata)); var factory = new ConnectionFactory() { HostName = MicroServiceConfig.Rabbit_hostname(), UserName = MicroServiceConfig.Rabbit_username(), Password = MicroServiceConfig.Rabbit_password() }; using (var connection = factory.CreateConnection()) { using (var channel = connection.CreateModel()) { var properties = channel.CreateBasicProperties(); properties.Persistent = true; channel.ExchangeDeclare(exchange: "ms.events", type: "topic"); channel.BasicPublish( exchange: "ms.events", routingKey: route, basicProperties: properties, body: bsonMessage); } } }
/// <summary> /// Initializes a new instance of the <see cref="RequestClient"/> class. /// </summary> public RequestClient() { ConnectionFactory factory = new ConnectionFactory() { HostName = MicroServiceConfig.Rabbit_hostname(), UserName = MicroServiceConfig.Rabbit_username(), Password = MicroServiceConfig.Rabbit_password() }; factory.AutomaticRecoveryEnabled = true; factory.NetworkRecoveryInterval = TimeSpan.FromSeconds(10); this.connection = factory.CreateConnection(); this.channel = this.connection.CreateModel(); this.channel.ExchangeDeclare(exchange: "ms.request", type: "topic"); this.replyQueueName = this.channel.QueueDeclare().QueueName; this.consumer = new EventingBasicConsumer(this.channel); this.props = this.channel.CreateBasicProperties(); var correlationId = Guid.NewGuid().ToString(); this.props.CorrelationId = correlationId; this.props.ReplyTo = this.replyQueueName; this.consumer.Received += (model, ea) => { var body = ea.Body; if (ea.BasicProperties.CorrelationId == correlationId) { this.respQueue.Add(body); } }; }
/// <summary> /// Initializes a new instance of the <see cref="EventClient"/> class. /// </summary> public EventClient() { var factory = new ConnectionFactory() { HostName = MicroServiceConfig.Rabbit_hostname(), UserName = MicroServiceConfig.Rabbit_username(), Password = MicroServiceConfig.Rabbit_password() }; factory.AutomaticRecoveryEnabled = true; factory.NetworkRecoveryInterval = TimeSpan.FromSeconds(10); this.connection = factory.CreateConnection(); this.channel = this.connection.CreateModel(); this.channel.ExchangeDeclare(exchange: "ms.event", type: "topic"); this.consumer = new EventingBasicConsumer(this.channel); }
public static void Main(string[] args) { var factory = new ConnectionFactory() { HostName = MicroServiceConfig.Rabbit_hostname(), UserName = MicroServiceConfig.Rabbit_username(), Password = MicroServiceConfig.Rabbit_password() }; factory.AutomaticRecoveryEnabled = true; factory.NetworkRecoveryInterval = TimeSpan.FromSeconds(10); using (var connection = factory.CreateConnection()) { Console.WriteLine(DateTime.Now.ToLongDateString() + " " + DateTime.Now.ToLongTimeString() + ": Connected"); using (var channel = connection.CreateModel()) { channel.ExchangeDeclare(exchange: "ms.events", type: "topic"); channel.QueueDeclare(queue: "ms.queue.authapi", durable: true, exclusive: false, autoDelete: false, arguments: null); channel.QueueBind("ms.queue.authapi", "ms.events", "#"); channel.BasicQos(0, 1, false); var consumer = new EventingBasicConsumer(channel); consumer.Received += (model, ea) => { var body = ea.Body; try { MessageFlow.HandleEvent("authapi", ea, authApiHub.HandleMessage); channel.BasicAck(ea.DeliveryTag, false); } catch (Exception ex) { System.Threading.Thread.Sleep(1000); //try again in a second... channel.BasicNack(ea.DeliveryTag, false, true); authApiHub.Log($"Event exception: {Monosoft.Common.Utils.ExceptionHelper.GetExceptionAsReportText(ex)}"); } }; channel.BasicConsume("ms.queue.authapi", false, consumer); CreateWebHostBuilder(args).Build().Run(); } } }
/// <summary> /// Initializes a new instance of the <see cref="RequestServer"/> class. /// Starts the request server, so that it will consume messages from the request (ms.request) exchange /// </summary> /// <param name="settings">List of topic/routes that are consumed by this server</param> public RequestServer(List <MessageQueueConfiguration> settings, double?automaticShutdownSeconds = null) { DateTime shutdownTime = automaticShutdownSeconds.HasValue ? DateTime.Now.AddSeconds(automaticShutdownSeconds.Value) : DateTime.Now.AddYears(99); settings.Add(new RequestConfiguration("diagnostics", "diagnostics.seteventsettings", null)); Console.WriteLine(DateTime.Now.ToLongDateString() + " " + DateTime.Now.ToLongTimeString() + ": Connecting to: " + MicroServiceConfig.Rabbit_hostname()); var factory = new ConnectionFactory() { HostName = MicroServiceConfig.Rabbit_hostname(), UserName = MicroServiceConfig.Rabbit_username(), Password = MicroServiceConfig.Rabbit_password() }; factory.AutomaticRecoveryEnabled = true; factory.NetworkRecoveryInterval = TimeSpan.FromSeconds(10); using (var connection = factory.CreateConnection()) { var channel = connection.CreateModel(); { channel.BasicQos(0, 1, false); foreach (var setting in settings) { Console.WriteLine(DateTime.Now.ToLongDateString() + " " + DateTime.Now.ToLongTimeString() + ": " + setting.QueueName + " connected"); channel.ExchangeDeclare(exchange: setting.ExchangeName, type: "topic"); channel.QueueDeclare(queue: "ms.queue." + setting.QueueName, durable: true, exclusive: false, autoDelete: false, arguments: null); channel.QueueBind("ms.queue." + setting.QueueName, setting.ExchangeName, setting.RoutingKey); var consumer = new EventingBasicConsumer(channel); consumer.Received += (model, ea) => { Console.WriteLine(DateTime.Now.ToLongDateString() + " " + DateTime.Now.ToLongTimeString() + ": " + setting.QueueName + " message received"); System.Threading.Thread.Sleep(1); // just for a bit, to make sure we dont block everything.. byte[] response = null; var body = ea.Body; var props = ea.BasicProperties; var replyProps = channel.CreateBasicProperties(); replyProps.CorrelationId = props.CorrelationId; try { if (setting is EventConfiguration) { MessageFlow.HandleEvent(setting.QueueName, ea, (setting as EventConfiguration).Handler); } if (setting is RequestConfiguration) { response = MessageFlow.HandleMessage(setting.QueueName, ea, (setting as RequestConfiguration).Handler); } } catch (Exception ex) { string reporttxt = Monosoft.Common.Utils.ExceptionHelper.GetExceptionAsReportText(ex); Common.MessageQueue.Diagnostics.Instance.LogEvent("Catestrophic failure", reporttxt, Common.DTO.Severity.Information, Guid.Empty); } finally { channel.BasicAck(ea.DeliveryTag, false); Console.WriteLine(DateTime.Now.ToLongDateString() + " " + DateTime.Now.ToLongTimeString() + ": ACK"); if (props != null && props.ReplyTo != null) { channel.BasicPublish( exchange: string.Empty, routingKey: props.ReplyTo, basicProperties: replyProps, body: response); Console.WriteLine(DateTime.Now.ToLongDateString() + " " + DateTime.Now.ToLongTimeString() + ": RPC responded"); } } }; channel.BasicConsume("ms.queue." + setting.QueueName, false, consumer); } } while (DateTime.Now < shutdownTime) { System.Threading.Thread.Sleep(2500); Console.Write("."); } } }
public static void Main(string[] args) { var factory = new ConnectionFactory() { HostName = MicroServiceConfig.Rabbit_hostname(), UserName = MicroServiceConfig.Rabbit_username(), Password = MicroServiceConfig.Rabbit_password() }; factory.AutomaticRecoveryEnabled = true; factory.NetworkRecoveryInterval = TimeSpan.FromSeconds(10); using (var connection = factory.CreateConnection()) { Console.WriteLine(DateTime.Now.ToLongDateString() + " " + DateTime.Now.ToLongTimeString() + ": Connected"); using (var channel = connection.CreateModel()) { channel.ExchangeDeclare(exchange: "ms.events", type: "topic"); channel.QueueDeclare(queue: "ms.queue.clusterapi", durable: true, exclusive: false, autoDelete: false, arguments: null); channel.QueueBind("ms.queue.clusterapi", "ms.events", "#"); channel.BasicQos(0, 1, false); var consumer = new EventingBasicConsumer(channel); consumer.Received += (model, ea) => { var body = ea.Body; try { MessageFlow.HandleEvent("clusterapi", ea, clusterApiHub.HandleMessage); } catch (Exception ex) { Console.WriteLine("ex: " + ex.Message); } finally { channel.BasicAck(ea.DeliveryTag, false); } }; channel.BasicConsume("ms.queue.clusterapi", false, consumer); CreateWebHostBuilder(args).Build().Run(); } using (var channel = connection.CreateModel()) { channel.ExchangeDeclare(exchange: "ms.request", type: "topic");//ms.request? eller ms.auth.request? channel.QueueDeclare(queue: "ms.queue.forwardauthapi", durable: true, exclusive: false, autoDelete: false, arguments: null); channel.QueueBind("ms.queue.forwardauthapi", "ms.request", "token.#"); channel.QueueBind("ms.queue.forwardauthapi", "ms.request", "servicestore.#"); channel.BasicQos(0, 1, false); var consumer = new EventingBasicConsumer(channel); consumer.Received += (model, ea) => { byte[] response = null; var body = ea.Body; var props = ea.BasicProperties; var replyProps = channel.CreateBasicProperties(); replyProps.CorrelationId = props.CorrelationId; try { response = MessageFlow.HandleMessage("clusterapi", ea, Program.FowardMessage /*send via signalR*/); } catch (Exception ex) { Console.WriteLine("ex: " + ex.Message); } finally { channel.BasicAck(ea.DeliveryTag, false); if (props != null && props.ReplyTo != null) { channel.BasicPublish( exchange: string.Empty, routingKey: props.ReplyTo, basicProperties: replyProps, body: response); Console.WriteLine(DateTime.Now.ToLongDateString() + " " + DateTime.Now.ToLongTimeString() + ": RPC responded"); } } }; channel.BasicConsume("ms.queue.forwardauthapi", false, consumer); CreateWebHostBuilder(args).Build().Run(); } } }