static async Task UsePulsarClient(string topic, CancellationToken cancel) { var client = new PulsarClientBuilder() .ServiceUrl("pulsar://pulsar:6650") .Build(); var producer = await client.NewProducer() .Topic(topic) .CreateAsync(); var consumer = await client.NewConsumer() .Topic(topic) .SubscriptionName("sub") .SubscribeAsync(); var messageId = await producer.SendAsync(Encoding.UTF8.GetBytes($"Sent from C# at '{DateTime.Now}'")); Console.WriteLine($"MessageId is: '{messageId}'"); var message = await consumer.ReceiveAsync(); Console.WriteLine($"Received: {Encoding.UTF8.GetString(message.Data)}"); await consumer.AcknowledgeAsync(message.MessageId); }
static async Task Main(string[] args) { var subscription = "pulsar-dead-letter-tester"; var topic = $"public/default/{Guid.NewGuid()}"; var deadLetterTopic = $"{topic}-{subscription}-DLQ"; await WaitForPulsar(TimeSpan.FromSeconds(30)); // SUCCESSFUL // await CreateTopic(topic); // UNSUCCESSFUL await CreateTopic(topic, 2); string serviceUrl = "pulsar://pulsar:6650"; PulsarClient client = new PulsarClientBuilder().ServiceUrl(serviceUrl) .Build(); var producer = await client.NewProducer().Topic(topic) .CreateAsync(); var consumer = await client.NewConsumer().Topic(topic).ConsumerName("test").SubscriptionName(subscription) .NegativeAckRedeliveryDelay(TimeSpan.FromSeconds(2)) .DeadLetterPolicy(new DeadLetterPolicy(maxRetry)) .SubscriptionType(SubscriptionType.Shared) .SubscribeAsync(); var deadLetterConsumer = await client.NewConsumer().Topic(deadLetterTopic).ConsumerName("test").SubscriptionName(subscription) .SubscriptionType(SubscriptionType.Shared) .SubscribeAsync(); await producer.SendAsync(System.Text.Encoding.UTF8.GetBytes(Guid.NewGuid().ToString())); var consumerTask = ConsumeTopic(consumer); var deadLetterConsumerTask = ConsumeDeadLetterTopic(deadLetterConsumer); Task.WaitAny(new[] { consumerTask, deadLetterConsumerTask }); if (consumerTask.IsFaulted) { throw consumerTask.Exception; } if (deadLetterConsumerTask.IsCompletedSuccessfully) { Console.WriteLine("Successfully moved to dead letter queue!"); return; } if (!consumerTask.IsCompleted) { throw new Exception("No new messages but no dead letter messages either."); } }
public PulsarClient RentClient() { lock (this) { if (_client == null) { var builder = new PulsarClientBuilder().ServiceUrl(_options.ServiceUrl); if (_options.TlsOptions != null) { builder.EnableTls(_options.TlsOptions.UseTls); builder.EnableTlsHostnameVerification(_options.TlsOptions.TlsHostnameVerificationEnable); builder.AllowTlsInsecureConnection(_options.TlsOptions.TlsAllowInsecureConnection); builder.TlsTrustCertificate(_options.TlsOptions.TlsTrustCertificate); builder.Authentication(_options.TlsOptions.Authentication); builder.TlsProtocols(_options.TlsOptions.TlsProtocols); } _client = builder.BuildAsync().Result; } return(_client); } }