public static void Publish <T>(string topicName, T data) { var channel = RabbitMqFactory.GetChannel(topicName); var es = new RabbitMqEventStream( typeof(T).Name, JsonConvert.SerializeObject(data), TimeStampHelper.ToUtcTimeStamp(DateTime.Now)); var body = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(es)); var prop = channel.CreateBasicProperties(); prop.Expiration = ConfigHelper.RmqExpiration; channel.BasicPublish( $"Exchange-{ExchangeType.Direct}-{topicName}", string.Empty, prop, body); }
public void Register() { this.queueNames.ToList().ForEach(topicName => { var channel = RabbitMqFactory.GetChannel(topicName); //// generate queue var queueName = channel.QueueDeclare($"{this.queueId}-{topicName}", false, false, false, null).QueueName; //// bind queue to exchange channel.QueueBind(queueName, $"Exchange-{ExchangeType.Direct}-{topicName}", string.Empty, null); var consumer = new EventingBasicConsumer(channel); consumer.Received += (model, ea) => { var @event = JsonConvert.DeserializeObject <RabbitMqEventStream>(Encoding.UTF8.GetString(ea.Body)); if (this.dispatcher.DispatchMessage(@event)) { channel.BasicAck(ea.DeliveryTag, true); } }; var consumerTag = channel.BasicConsume(queueName, false, $"{Environment.MachineName}", false, false, null, consumer); }); }