Ejemplo n.º 1
0
        /// <summary>
        /// Get messages from a queue. (This is not an HTTP GET as it will alter the state of the queue.)
        /// </summary>
        /// <param name="virtualHost"></param>
        /// <param name="queueName"></param>
        /// <param name="count">ols the maximum number of messages to get. You may get fewer messages than this if the queue cannot immediately provide them.</param>
        /// <param name="requeue">determines whether the messages will be removed from the queue. If requeue is true they will be requeued - but their redelivered flag will be set.</param>
        /// <param name="encoding">must be either "auto" (in which case the payload will be returned as a string if it is valid UTF-8, and base64 encoded otherwise), or "base64" (in which case the payload will always be base64 encoded).</param>
        /// <returns></returns>
        public async Task <IEnumerable <QueueMessage> > GetQueueMessages(string virtualHost, string queueName,
                                                                         long count = Int64.MaxValue, bool requeue = true, PayloadEncoding encoding = PayloadEncoding.Auto)
        {
            var request = new GetQueueMessagesRequest
            {
                count    = count,
                requeue  = requeue,
                encoding = encoding.ToString("G").ToLower(),
            };

            string path = $"/api/queues/{virtualHost.Encode()}/{queueName.Encode()}/get";

            return(await DoCall <IEnumerable <QueueMessage> >(path, HttpMethod.Post, request));
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Publish a message to a given exchange.
        /// </summary>
        /// <param name="virtualHost"></param>
        /// <param name="exchangeName"></param>
        /// <param name="routingKey">binding key</param>
        /// <param name="payload">message data</param>
        /// <param name="payloadEncoding">The payload_encoding key should be either "string" (in which case the payload will be taken to be the UTF-8 encoding of the payload field) or "base64" (in which case the payload field is taken to be base64 encoded).</param>
        /// <param name="properties"></param>
        /// <returns>true if the message was sent to at least one queue.</returns>
        public async Task <bool> PublishMessage(
            string virtualHost, string exchangeName, string routingKey, dynamic payload,
            PayloadEncoding payloadEncoding = PayloadEncoding.String, Properties properties = null)
        {
            if (exchangeName == String.Empty)
            {
                throw new ArgumentException("Cannot send message using default exchange in HTTP API");
            }

            var request = new PublishMessageRequest
            {
                payload          = JsonConvert.SerializeObject(payload),
                routing_key      = routingKey,
                properties       = new Properties(),
                payload_encoding = payloadEncoding.ToString("G").ToLower()
            };

            string path     = $"/api/exchanges/{virtualHost.Encode()}/{exchangeName.Encode()}/publish";
            var    response = await DoCall <PublishMessageResponse>(path, HttpMethod.Post, request);

            return(response.Routed);
        }