Ejemplo n.º 1
0
        /// <summary>
        /// Called when a message is published.  Determines if there is a registration associated with
        /// the message being published.  If there is a registration for an associated Queue or Topic,
        /// the message will be delivered.
        /// </summary>
        /// <param name="message">The message being dispatched.</param>
        /// <param name="cancellationToken">Cancellation token.</param>
        /// <returns></returns>
        public override async Task PublishMessageAsync(IMessage message, CancellationToken cancellationToken)
        {
            Type messageType = message.GetType();

            if (!_publisherModule.HasHostItem(messageType))
            {
                return;
            }

            // Get information associated with the message being published:
            IHostItem       hostItem   = _publisherModule.GetHostItem(messageType);
            ISenderHostItem senderItem = (ISenderHostItem)hostItem;

            Session session = await _connectionModule.CreateSenderSession(hostItem.HostName);

            SenderLink senderLink = null;

            try
            {
                // Create the sender link used to send messages:
                senderLink = new SenderLink(session, Guid.NewGuid().ToString(), senderItem.Name);

                // Serialize the message based on its associated content-type and delegate to the corresponding
                // host sender item to create the AMQP message with the message properties correctly set.
                object  body        = SerializeMessage(hostItem, message);
                Message amqpMessage = senderItem.CreateMessage(message, body);

                // Send message to the defined host item (i.e. Queue/Topic).
                await senderLink.SendAsync(amqpMessage);
            }
            finally
            {
                await session.CloseAsync();

                if (senderLink != null)
                {
                    await senderLink.CloseAsync();
                }
            }
        }
Ejemplo n.º 2
0
 public object SerializeMessage(IHostItem hostItem, IMessage message)
 {
     return(_serialization.Serialize(message, hostItem.ContentType, hostItem.ContentEncoding));
 }