You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
How do I write a pub/sub that uses the redis component?
using Dapr.PluggableComponents.Components;using Dapr.PluggableComponents.Components.PubSub;using Dapr.Proto.Components.V1;using Microsoft.Extensions.Logging;namespace Test.Dapr.WebApp3.Components;internalsealedclassRedisQueuesPubSub(ILogger<RedisQueuesPubSub> logger):IPubSub{privatereadonlyILogger<RedisQueuesPubSub>_logger=logger??thrownew ArgumentNullException(nameof(logger));privatestring?redisHost;privatestring?redisPassword;privateTimeSpanpollInterval= TimeSpan.FromSeconds(5);privateintmaxMessages=5;// Called to initialize the component with its configured metadata...public Task InitAsync(MetadataRequestrequest,CancellationTokencancellationToken=default){
_logger.LogInformation("Init request");this.redisHost = request.Properties["redisHost"];this.redisPassword = request.Properties["redisPassword"];if(request.Properties.TryGetValue("pollIntervalSeconds",outvar pollIntervalString)){this.pollInterval = TimeSpan.FromSeconds(int.Parse(pollIntervalString));}if(request.Properties.TryGetValue("maxMessages",outvar maxMessagesString)){this.maxMessages =int.Parse(maxMessagesString);}return Task.CompletedTask;}// Send the message to the "topic"...public Task PublishAsync(PubSubPublishRequestrequest,CancellationTokencancellationToken=default){
_logger.LogInformation("Publish request");thrownew NotImplementedException();}// Until canceled, check the topic for messages and deliver them to the Dapr runtime...public Task PullMessagesAsync(PubSubPullMessagesTopictopic,MessageDeliveryHandler<string?,PubSubPullMessagesResponse>deliveryHandler,CancellationTokencancellationToken=default){
_logger.LogInformation($"Pull messages request for topic {topic.Name}");// Poll the topic until canceled...while(!cancellationToken.IsCancellationRequested){varmessages=// Poll topic for messages...
foreach (var message in messages){// Deliver the message to the Dapr runtime...await deliveryHandler(new PubSubPullMessagesResponse(topicName){// Set the message content...},// Callback invoked when application acknowledges the message...async errorMessage =>{// An empty message indicates the application successfully processed the message...if(string.IsNullOrWhiteSpace(errorMessage)){// Delete the message from the topic...}});}// Wait for the next poll (or cancellation)...await Task.Delay(this.pollInterval, cancellationToken);}}}
The redis component is deployed in a local docker environment, how do I implement the specific code, looking forward to your guidance!
The text was updated successfully, but these errors were encountered:
About redis pub/sub
The redis component is deployed in a local docker environment, how do I implement the specific code, looking forward to your guidance!
The text was updated successfully, but these errors were encountered: