Beispiel #1
0
        public async Task Publish <T>(string publishedLanguageEntity, T message, long sequence)
        {
            var exchange = await _messageBus.ExchangeDeclareAsync(NamingConventions.ExchangeNamingConvention(_boundedContextName, publishedLanguageEntity),
                                                                  ExchangeType.Topic);

            await Publish(exchange, "", message, sequence);
        }
Beispiel #2
0
        public async Task <IDisposable> AddSubscription <TEvent>(string sourceBoundedContextName,
                                                                 string publishedLanguageEntity)
            where TEvent : class
        {
            var queue = await _messageBus.QueueDeclareAsync(NamingConventions.QueueNamingConvention(_boundedContextName,
                                                                                                    sourceBoundedContextName, publishedLanguageEntity, _subscriberId));

            await _messageBus.BindAsync(
                new Exchange(
                    NamingConventions.ExchangeNamingConvention(sourceBoundedContextName, publishedLanguageEntity)),
                queue, "");

            return(_messageBus.Consume(queue, async(bytes, properties, info) =>
            {
                var msg = Encoding.UTF8.GetString(bytes);

                Console.WriteLine(msg);
                var validationResult = await _schemaValidator.IsValid <TEvent>(msg);
                if (validationResult.IsValid)
                {
                    var envelope = System.Text.Json.JsonSerializer.Deserialize <Envelope <TEvent> >(msg);
                    var props = new MessageProperties();
                    properties.CopyTo(props);
                    await _eventDispatcher.HandleEvent(envelope, props);
                }
                else
                {
                    throw new Exception($"Schema is invalid, errors: {string.Join(", ", validationResult.Errors)}");
                }
            }));
        }
        public async Task Publish <T>(string publishedLanguageEntity, T message, long sequence)
        {
            var envelope = new Envelope <T>(_boundedContextName, message, sequence);
            var msgBytes = System.Text.Json.JsonSerializer.SerializeToUtf8Bytes(envelope);

            var topic = _session.GetTopic($"topic://{NamingConventions.Topic(_boundedContextName, publishedLanguageEntity)}");

            //var msg = _producer.CreateBytesMessage(msgBytes);
            var msg = _producer.CreateTextMessage(System.Text.Json.JsonSerializer.Serialize(envelope));

            _producer.Send(topic, msg, MsgDeliveryMode.Persistent, MsgPriority.Normal, TimeSpan.FromMinutes(5));
        }
Beispiel #4
0
        public async Task <IDisposable> AddSubscription <TEvent>(string sourceBoundedContextName, string publishedLanguageEntity)
            where TEvent : class
        {
            var topic    = _session.GetTopic($"topic://{NamingConventions.Topic(sourceBoundedContextName, publishedLanguageEntity)}");
            var consumer = _session.CreateConsumer(topic);

            consumer.Listener += async msg => await OnMessage <TEvent>(msg);

            if (!_conn.IsStarted)
            {
                _conn.Start();
            }
            return(consumer);
        }