/// <summary> /// Emits messages to a stream. There's no limit to the number of messages in a request, but the total size of a message or request must be 1 MiB or less. /// The service calculates the partition ID from the message key and stores messages that share a key on the same partition. /// If a message does not contain a key or if the key is null, the service generates a message key for you. /// The partition ID cannot be passed as a parameter. /// /// </summary> /// <param name="request">The request object containing the details to send. Required.</param> /// <param name="retryConfiguration">The retry configuration that will be used by to send this request. Optional.</param> /// <param name="cancellationToken">The cancellation token to cancel this operation. Optional.</param> /// <returns>A response object containing details about the completed operation</returns> /// <example>Click <a href="https://docs.cloud.oracle.com/en-us/iaas/tools/dot-net-examples/latest/streaming/PutMessages.cs.html">here</a> to see an example of how to use PutMessages API.</example> public async Task <PutMessagesResponse> PutMessages(PutMessagesRequest request, RetryConfiguration retryConfiguration = null, CancellationToken cancellationToken = default) { logger.Trace("Called putMessages"); Uri uri = new Uri(this.restClient.GetEndpoint(), System.IO.Path.Combine(basePathWithoutHost, "/streams/{streamId}/messages".Trim('/'))); HttpMethod method = new HttpMethod("POST"); HttpRequestMessage requestMessage = Converter.ToHttpRequestMessage(uri, method, request); requestMessage.Headers.Add("Accept", "application/json"); requestMessage.Properties.Add(Oci.Common.Http.Signing.SigningStrategy.SIGNING_STRATEGY_PROPERTY_NAME_KEY, Oci.Common.Http.Signing.SigningStrategy.EXCLUDE_BODY); GenericRetrier retryingClient = Retrier.GetPreferredRetrier(retryConfiguration, this.retryConfiguration); HttpResponseMessage responseMessage; try { if (retryingClient != null) { responseMessage = await retryingClient.MakeRetryingCall(this.restClient.HttpSend, requestMessage, cancellationToken).ConfigureAwait(false); } else { responseMessage = await this.restClient.HttpSend(requestMessage).ConfigureAwait(false); } this.restClient.CheckHttpResponseMessage(requestMessage, responseMessage); return(Converter.FromHttpResponseMessage <PutMessagesResponse>(responseMessage)); } catch (Exception e) { logger.Error($"PutMessages failed with error: {e.Message}"); throw; } }
private static async Task PublishExampleMessages(StreamClient streamClient, string streamId) { // build up a putRequest and publish some messages to the stream List <PutMessagesDetailsEntry> messages = new List <PutMessagesDetailsEntry>(); for (int i = 0; i < 100; i++) { PutMessagesDetailsEntry detailsEntry = new PutMessagesDetailsEntry { Key = Encoding.UTF8.GetBytes($"messagekey-{i}"), Value = Encoding.UTF8.GetBytes($"messageValue-{i}") }; messages.Add(detailsEntry); } logger.Info($"Publishing {messages.Count} messages to stream {streamId}"); PutMessagesDetails messagesDetails = new PutMessagesDetails { Messages = messages }; PutMessagesRequest putRequest = new PutMessagesRequest { StreamId = streamId, PutMessagesDetails = messagesDetails }; PutMessagesResponse putResponse = await streamClient.PutMessages(putRequest); // the putResponse can contain some useful metadata for handling failures foreach (PutMessagesResultEntry entry in putResponse.PutMessagesResult.Entries) { if (entry.Error != null) { logger.Info($"Error({entry.Error}): {entry.ErrorMessage}"); } else { logger.Info($"Published message to partition {entry.Partition}, offset {entry.Offset}"); } } }
protected override void ProcessRecord() { base.ProcessRecord(); PutMessagesRequest request; try { request = new PutMessagesRequest { StreamId = StreamId, PutMessagesDetails = PutMessagesDetails, OpcRequestId = OpcRequestId }; response = client.PutMessages(request).GetAwaiter().GetResult(); WriteOutput(response, response.PutMessagesResult); FinishProcessing(response); } catch (Exception ex) { TerminatingErrorDuringExecution(ex); } }