This is an automated email from the ASF dual-hosted git repository.
blankensteiner pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/pulsar-dotpulsar.git
The following commit(s) were added to refs/heads/master by this push:
new f7fcf2d Adding exception handlers to the samples
f7fcf2d is described below
commit f7fcf2d8f2b4df889e43d4e0d2fe62bbc42bcad4
Author: Daniel Blankensteiner <[email protected]>
AuthorDate: Mon Jan 6 15:44:52 2025 +0100
Adding exception handlers to the samples
---
samples/Consuming/Program.cs | 18 ++++++++++--------
samples/Producing/Program.cs | 18 ++++++++++--------
samples/Reading/Program.cs | 18 ++++++++++--------
samples/SendChannel/Program.cs | 16 +++++++++-------
4 files changed, 39 insertions(+), 31 deletions(-)
diff --git a/samples/Consuming/Program.cs b/samples/Consuming/Program.cs
index 18a1c83..649b3c4 100644
--- a/samples/Consuming/Program.cs
+++ b/samples/Consuming/Program.cs
@@ -24,10 +24,13 @@ Console.CancelKeyPress += (sender, args) =>
args.Cancel = true;
};
-await using var client = PulsarClient.Builder().Build(); // Connecting to
pulsar://localhost:6650
+await using var client = PulsarClient
+ .Builder()
+ .ExceptionHandler(ExceptionHandler)
+ .Build(); // Connecting to pulsar://localhost:6650
await using var consumer = client.NewConsumer(Schema.String)
- .StateChangedHandler(Monitor)
+ .StateChangedHandler(StateChangedHandler)
.SubscriptionName("MySubscription")
.Topic("persistent://public/default/mytopic")
.Create();
@@ -49,9 +52,8 @@ async Task ConsumeMessages(IConsumer<string> consumer,
CancellationToken cancell
catch (OperationCanceledException) { }
}
-void Monitor(ConsumerStateChanged stateChanged)
-{
- var topic = stateChanged.Consumer.Topic;
- var state = stateChanged.ConsumerState;
- Console.WriteLine($"The consumer for topic '{topic}' changed state to
'{state}'");
-}
+void ExceptionHandler(ExceptionContext context) =>
+ Console.WriteLine($"The PulsarClient got an exception:
{context.Exception}");
+
+void StateChangedHandler(ConsumerStateChanged stateChanged) =>
+ Console.WriteLine($"The consumer for topic '{stateChanged.Consumer.Topic}'
changed state to '{stateChanged.ConsumerState}'");
diff --git a/samples/Producing/Program.cs b/samples/Producing/Program.cs
index 7339b77..425bea3 100644
--- a/samples/Producing/Program.cs
+++ b/samples/Producing/Program.cs
@@ -24,10 +24,13 @@ Console.CancelKeyPress += (sender, args) =>
args.Cancel = true;
};
-await using var client = PulsarClient.Builder().Build(); // Connecting to
pulsar://localhost:6650
+await using var client = PulsarClient
+ .Builder()
+ .ExceptionHandler(ExceptionHandler)
+ .Build(); // Connecting to pulsar://localhost:6650
await using var producer = client.NewProducer(Schema.String)
- .StateChangedHandler(Monitor)
+ .StateChangedHandler(StateChangedHandler)
.Topic("persistent://public/default/mytopic")
.Create();
@@ -53,9 +56,8 @@ async Task ProduceMessages(IProducer<string> producer,
CancellationToken cancell
{ }
}
-void Monitor(ProducerStateChanged stateChanged)
-{
- var topic = stateChanged.Producer.Topic;
- var state = stateChanged.ProducerState;
- Console.WriteLine($"The producer for topic '{topic}' changed state to
'{state}'");
-}
+void ExceptionHandler(ExceptionContext context) =>
+ Console.WriteLine($"The PulsarClient got an exception:
{context.Exception}");
+
+void StateChangedHandler(ProducerStateChanged stateChanged) =>
+ Console.WriteLine($"The producer for topic '{stateChanged.Producer.Topic}'
changed state to '{stateChanged.ProducerState}'");
diff --git a/samples/Reading/Program.cs b/samples/Reading/Program.cs
index e73f3a3..cb37650 100644
--- a/samples/Reading/Program.cs
+++ b/samples/Reading/Program.cs
@@ -24,11 +24,14 @@ Console.CancelKeyPress += (sender, args) =>
args.Cancel = true;
};
-await using var client = PulsarClient.Builder().Build(); // Connecting to
pulsar://localhost:6650
+await using var client = PulsarClient
+ .Builder()
+ .ExceptionHandler(ExceptionHandler)
+ .Build(); // Connecting to pulsar://localhost:6650
await using var reader = client.NewReader(Schema.String)
.StartMessageId(MessageId.Earliest)
- .StateChangedHandler(Monitor)
+ .StateChangedHandler(StateChangedHandler)
.Topic("persistent://public/default/mytopic")
.Create();
@@ -48,9 +51,8 @@ async Task ReadMessages(IReader<string> reader,
CancellationToken cancellationTo
catch (OperationCanceledException) { }
}
-void Monitor(ReaderStateChanged stateChanged)
-{
- var topic = stateChanged.Reader.Topic;
- var state = stateChanged.ReaderState;
- Console.WriteLine($"The reader for topic '{topic}' changed state to
'{state}'");
-}
+void ExceptionHandler(ExceptionContext context) =>
+ Console.WriteLine($"The PulsarClient got an exception:
{context.Exception}");
+
+void StateChangedHandler(ReaderStateChanged stateChanged) =>
+ Console.WriteLine($"The reader for topic '{stateChanged.Reader.Topic}'
changed state to '{stateChanged.ReaderState}'");
diff --git a/samples/SendChannel/Program.cs b/samples/SendChannel/Program.cs
index f498366..c27d3dd 100644
--- a/samples/SendChannel/Program.cs
+++ b/samples/SendChannel/Program.cs
@@ -24,7 +24,10 @@ Console.CancelKeyPress += (sender, args) =>
args.Cancel = true;
};
-await using var client = PulsarClient.Builder().Build(); // Connecting to
pulsar://localhost:6650
+await using var client = PulsarClient
+ .Builder()
+ .ExceptionHandler(ExceptionHandler)
+ .Build(); // Connecting to pulsar://localhost:6650
await using var producer = client.NewProducer(Schema.String)
.StateChangedHandler(Monitor)
@@ -63,9 +66,8 @@ async Task ProduceMessages(ISendChannel<string> sendChannel,
int messages, Cance
{ }
}
-void Monitor(ProducerStateChanged stateChanged)
-{
- var topic = stateChanged.Producer.Topic;
- var state = stateChanged.ProducerState;
- Console.WriteLine($"The producer for topic '{topic}' changed state to
'{state}'");
-}
+void ExceptionHandler(ExceptionContext context) =>
+ Console.WriteLine($"The PulsarClient got an exception:
{context.Exception}");
+
+void Monitor(ProducerStateChanged stateChanged) =>
+ Console.WriteLine($"The producer for topic '{stateChanged.Producer.Topic}'
changed state to '{stateChanged.ProducerState}'");