static async Task Main(string[] args) { var client = PulsarClient.Builder().Build(); /* * If Multiple Consumer process have same "subscriptionName", only one of them will get message * Can use Guid.NewGuid().ToString() to ensure each consumer process have unique subscriptionName */ var consumerOptions = new ConsumerOptions(subscriptionName: "DemoSub", topic: "persistent://public/default/mytopic"); var consumer = client.CreateConsumer(consumerOptions); await foreach (var message in consumer.Messages()) { Console.WriteLine($"Received: {Encoding.UTF8.GetString(message.Data.ToArray())}"); //await consumer.Acknowledge(message); } //var readerOptions = new ReaderOptions(MessageId.Latest, "persistent://public/default/mytopic"); //var reader = client.CreateReader(readerOptions); //await foreach (var message2 in reader.Messages()) //{ // Console.WriteLine($"Reader Received: {Encoding.UTF8.GetString(message2.Data.ToArray())}"); //} }
private static async Task Main(string[] args) { const string myTopic = "persistent://public/default/mytopic"; await using var client = PulsarClient.Builder().Build(); //Connecting to pulsar://localhost:6650 var consumer = client.NewConsumer() .SubscriptionName("MySubscription") .Topic(myTopic) .Create(); var monitoring = Monitor(consumer); var cts = new CancellationTokenSource(); var consuming = ConsumeMessages(consumer, cts.Token); Console.WriteLine("Press a key to exit"); _ = Console.ReadKey(); cts.Cancel(); await consuming.ConfigureAwait(false); await consumer.DisposeAsync().ConfigureAwait(false); await monitoring.ConfigureAwait(false); }
public async Task SimpleProduceConsume_WhenSendingMessagesToProducer_ThenReceiveMessagesFromConsumer() { //Arrange await using var client = PulsarClient.Builder().ServiceUrl(_pulsarService.GetBrokerUri()).Build(); string topicName = $"simple-produce-consume{Guid.NewGuid():N}"; const string content = "test-message"; //Act await using var producer = client.NewProducer(Schema.String) .Topic(topicName) .Create(); await using var consumer = client.NewConsumer(Schema.String) .Topic(topicName) .SubscriptionName("test-sub") .InitialPosition(SubscriptionInitialPosition.Earliest) .Create(); await producer.Send(content); _testOutputHelper.WriteLine($"Sent a message: {content}"); //Assert (await consumer.Receive()).Value().Should().Be(content); }
static async Task Main(string[] args) { const string myTopic = "persistent://public/default/mytopic"; await using var client = PulsarClient.Builder().Build(); //Connecting to pulsar://localhost:6650 var reader = client.NewReader() .StartMessageId(MessageId.Earliest) .Topic(myTopic) .Create(); var monitoring = Monitor(reader); var cts = new CancellationTokenSource(); var reading = ReadMessages(reader, cts.Token); Console.WriteLine("Press a key to exit"); _ = Console.ReadKey(); cts.Cancel(); await reading; await reader.DisposeAsync(); await monitoring; }
protected override async Task ExecuteAsync(CancellationToken stoppingToken) { NeoService.Notify += NeoService_Notify; var neo = new MainService().StartUpNode(); _decision = new Decision(neo); await using var client = PulsarClient.Builder().Build(); _producer = client.NewProducer() .Topic("persistent://public/default/smartcontract") .Create(); _consumer = client.NewConsumer() .SubscriptionName("persistent://public/default/decision") .Topic("Decision") .Create(); while (!stoppingToken.IsCancellationRequested) { _logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now); await foreach (var message in _consumer.Messages()) { Console.WriteLine("Received: " + Encoding.UTF8.GetString(message.Data.ToArray())); await _consumer.Acknowledge(message); } await Task.Delay(1000, stoppingToken); } }
public async Task RoundRobinPartition_WhenSendMessages_ThenGetMessagesFromPartitionsInOrder() { //Arrange await using var client = PulsarClient.Builder().ServiceUrl(_pulsarService.GetBrokerUri()).Build(); string topicName = $"round-robin-partitioned-{Guid.NewGuid():N}"; const string content = "test-message"; const int partitions = 3; var consumers = new List <IConsumer <string> >(); await _pulsarService.CreatePartitionedTopic($"persistent/public/default/{topicName}", partitions); //Act await using var producer = client.NewProducer(Schema.String) .Topic(topicName) .Create(); await producer.StateChangedTo(ProducerState.Connected); for (var i = 0; i < partitions; ++i) { consumers.Add(client.NewConsumer(Schema.String) .Topic($"{topicName}-partition-{i}") .SubscriptionName("test-sub") .InitialPosition(SubscriptionInitialPosition.Earliest) .Create()); await producer.Send($"{content}-{i}"); _testOutputHelper.WriteLine($"Sent a message to consumer [{i}]"); } //Assert for (var i = 0; i < partitions; ++i) { (await consumers[i].Receive()).Value().Should().Be($"{content}-{i}"); } }
public async Task <MessageId> ProduceAsync(string topic, string payload) { await using var client = PulsarClient.Builder() .ServiceUrl(new Uri(_pulsarConfig.ServiceUrl)) .Build(); await using var producer = client.NewProducer(Schema.String) .StateChangedHandler(HandleStateChange) .Topic(topic) .Create(); return(await producer.Send(payload)); }
static async Task Main(string[] args) { var client = PulsarClient.Builder().Build(); var producerOptions = new ProducerOptions("persistent://public/default/mytopic"); var producer = client.CreateProducer(producerOptions); while (true) { Console.Write("Please Enter Message: "); var message = Console.ReadLine(); var data = Encoding.UTF8.GetBytes(message); var result = await producer.Send(data); } }
public async Task SinglePartition_WhenSendMessages_ThenGetMessagesFromSinglePartition() { //Arrange await using var client = PulsarClient.Builder().ServiceUrl(_pulsarService.GetBrokerUri()).Build(); string topicName = $"single-partitioned-{Guid.NewGuid():N}"; const string content = "test-message"; const int partitions = 3; const int msgCount = 3; var consumers = new List <IConsumer <string> >(); await _pulsarService.CreatePartitionedTopic($"persistent/public/default/{topicName}", partitions); //Act for (var i = 0; i < partitions; ++i) { await using var producer = client.NewProducer(Schema.String) .Topic(topicName) .MessageRouter(new SinglePartitionRouter(i)) .Create(); for (var msgIndex = 0; msgIndex < msgCount; ++msgIndex) { await producer.Send($"{content}-{i}-{msgIndex}"); _testOutputHelper.WriteLine($"Sent a message: {content}-{i}-{msgIndex}"); } } for (var i = 0; i < partitions; ++i) { consumers.Add(client.NewConsumer(Schema.String) .Topic($"{topicName}-partition-{i}") .SubscriptionName("test-sub") .InitialPosition(SubscriptionInitialPosition.Earliest) .Create()); } var msg = await consumers[1].GetLastMessageId(); //Assert for (var i = 0; i < partitions; ++i) { for (var msgIndex = 0; msgIndex < msgCount; ++msgIndex) { (await consumers[i].Receive()).Value().Should().Be($"{content}-{i}-{msgIndex}"); } } }
static async Task Main() { await using var client = PulsarClient.Builder() .ServiceUrl(new Uri("pulsar://pulsar:6650")) .Build(); await using var consumer = client.NewConsumer(Schema.String) .StateChangedHandler(HandleStateChange) .SubscriptionName("test-sub") .SubscriptionType(SubscriptionType.Shared) .Topic("persistent://public/testevents/packagedelivery") .Create(); await foreach (var message in consumer.Messages()) { Console.WriteLine(message.Value()); await consumer.Acknowledge(message); } }
public AEPMsgConsumerService(ILoggerFactory loggerFactory) { Logger = loggerFactory.CreateLogger <AEPMsgConsumerService>(); //自己填写 string topic = "test"; //自己填写 string tenantId = ""; //自己填写 string token = ""; var pulsarClient = PulsarClient.Builder().ServiceUrl(new Uri($"pulsar+ssl://msgpush.ctwing.cn:16651")) .AuthenticateUsingToken(token) .Build(); pulsarConsumer = pulsarClient.NewConsumer() .SubscriptionName(tenantId) .Topic($"{pulsarMqTenant}/{tenantId}/{topic}") .InitialPosition(SubscriptionInitialPosition.Earliest) .SubscriptionType(SubscriptionType.Shared) .Create(); }
private static async Task Main() { const string myTopic = "persistent://public/default/mytopic"; var cts = new CancellationTokenSource(); Console.CancelKeyPress += (sender, args) => { cts.Cancel(); args.Cancel = true; }; await using var client = PulsarClient.Builder().Build(); //Connecting to pulsar://localhost:6650 await using var producer = client.NewProducer(Schema.String) .StateChangedHandler(Monitor) .Topic(myTopic) .Create(); Console.WriteLine("Press Ctrl+C to exit"); await ProduceMessages(producer, cts.Token); }