C# 클라이언트 사용
C# 클라이언트 사용 (C# client use)
Pulsar C# 클라이언트(DotPulsar)로 프로듀서, 컨슈머, 리더를 만드는 실용적인 예시를 살펴볼게요. 특히 각 개체의 상태를 모니터링하는 방법을 보여드릴 텐데, DotPulsar는 상태 변경을 구독하는 방식이라 연결 상태를 파악하기 좋아요.
출처: 문서
본문
이 섹션은 Pulsar C# 클라이언트 사용을 시작하기 위한 실용적인 예시를 소개해요.
프로듀서 만들기 (Create a producer)
이 섹션은 프로듀서를 만드는 방법을 설명해요.
- 빌더를 사용해 프로듀서 만들기:
using DotPulsar;
using DotPulsar.Extensions;
var producer = client.NewProducer()
.Topic("persistent://public/default/mytopic")
.Create();
- 빌더를 사용하지 않고 프로듀서 만들기:
using DotPulsar;
var options = new ProducerOptions<byte[]>("persistent://public/default/mytopic", Schema.ByteArray);
var producer = client.CreateProducer(options);
모니터링 (Monitor)
이 예시는 프로듀서의 상태를 모니터링하는 방법을 보여줘요.
private static async ValueTask Monitor(IProducer producer, CancellationToken cancellationToken)
{
var state = ProducerState.Disconnected;
while (!cancellationToken.IsCancellationRequested)
{
state = (await producer.StateChangedFrom(state, cancellationToken)).ProducerState;
var stateMessage = state switch
{
ProducerState.Connected => $"The producer is connected",
ProducerState.Disconnected => $"The producer is disconnected",
ProducerState.Closed => $"The producer has closed",
ProducerState.Faulted => $"The producer has faulted",
ProducerState.PartiallyConnected => $"The producer is partially connected.",
_ => $"The producer has an unknown state '{state}'"
};
Console.WriteLine(stateMessage);
if (producer.IsFinalState(state))
return;
}
}
프로듀서에 사용 가능한 상태는 다음 표와 같아요.
| 상태 | 설명 |
|---|---|
| Closed | 프로듀서 또는 Pulsar 클라이언트가 폐기(dispose)됐어요. |
| Connected | 모든 것이 정상이에요. |
| Disconnected | 연결이 끊어졌고 재연결을 시도 중이에요. |
| Faulted | 복구할 수 없는 에러가 발생했어요. |
| PartiallyConnected | 하위 프로듀서(sub-producers) 중 일부가 연결이 끊어져 있어요. |
컨슈머 만들기 (Create a consumer)
이 섹션은 컨슈머를 만드는 방법을 설명해요.
- 빌더를 사용해 컨슈머 만들기:
using DotPulsar;
using DotPulsar.Extensions;
var consumer = client.NewConsumer()
.SubscriptionName("MySubscription")
.Topic("persistent://public/default/mytopic")
.Create();
- 빌더를 사용하지 않고 컨슈머 만들기:
using DotPulsar;
var options = new ConsumerOptions<byte[]>("MySubscription", "persistent://public/default/mytopic", Schema.ByteArray);
var consumer = client.CreateConsumer(options);
모니터링 (Monitor)
이 예시는 컨슈머의 상태를 모니터링하는 방법을 보여줘요.
private static async ValueTask Monitor(IConsumer consumer, CancellationToken cancellationToken)
{
var state = ConsumerState.Disconnected;
while (!cancellationToken.IsCancellationRequested)
{
state = (await consumer.StateChangedFrom(state, cancellationToken)).ConsumerState;
var stateMessage = state switch
{
ConsumerState.Active => "The consumer is active",
ConsumerState.Inactive => "The consumer is inactive",
ConsumerState.Disconnected => "The consumer is disconnected",
ConsumerState.Closed => "The consumer has closed",
ConsumerState.ReachedEndOfTopic => "The consumer has reached end of topic",
ConsumerState.Faulted => "The consumer has faulted",
ConsumerState.Unsubscribed => "The consumer is unsubscribed.",
_ => $"The consumer has an unknown state '{state}'"
};
Console.WriteLine(stateMessage);
if (consumer.IsFinalState(state))
return;
}
}
컨슈머에 사용 가능한 상태는 다음 표와 같아요.
| 상태 | 설명 |
|---|---|
| Active | 모든 것이 정상이에요. |
| Inactive | 모든 것이 정상이에요. 서브스크립션 타입이 Failover이고 당신이 활성 컨슈머가 아닌 경우예요. |
| Closed | 컨슈머 또는 Pulsar 클라이언트가 폐기됐어요. |
| Disconnected | 연결이 끊어졌고 재연결을 시도 중이에요. |
| Faulted | 복구할 수 없는 에러가 발생했어요. |
| ReachedEndOfTopic | 더 이상 전달되는 메시지가 없어요. |
| Unsubscribed | 컨슈머가 구독을 해제했어요. |
리더 만들기 (Create a reader)
이 섹션은 리더를 만드는 방법을 설명해요.
- 빌더를 사용해 리더 만들기:
using DotPulsar;
using DotPulsar.Extensions;
var reader = client.NewReader()
.StartMessageId(MessageId.Earliest)
.Topic("persistent://public/default/mytopic")
.Create();
- 빌더를 사용하지 않고 리더 만들기:
using DotPulsar;
var options = new ReaderOptions<byte[]>(MessageId.Earliest, "persistent://public/default/mytopic", Schema.ByteArray);
var reader = client.CreateReader(options);
모니터링 (Monitor)
이 예시는 리더의 상태를 모니터링하는 방법을 보여줘요.
private static async ValueTask Monitor(IReader reader, CancellationToken cancellationToken)
{
var state = ReaderState.Disconnected;
while (!cancellationToken.IsCancellationRequested)
{
state = (await reader.StateChangedFrom(state, cancellationToken)).ReaderState;
var stateMessage = state switch
{
ReaderState.Connected => "The reader is connected",
ReaderState.Disconnected => "The reader is disconnected",
ReaderState.Closed => "The reader has closed",
ReaderState.ReachedEndOfTopic => "The reader has reached end of topic",
ReaderState.Faulted => "The reader has faulted",
_ => $"The reader has an unknown state '{state}'"
};
Console.WriteLine(stateMessage);
if (reader.IsFinalState(state))
return;
}
}
리더에 사용 가능한 상태는 다음 표와 같아요.
| 상태 | 설명 |
|---|---|
| Closed | 리더 또는 Pulsar 클라이언트가 폐기됐어요. |
| Connected | 모든 것이 정상이에요. |
| Disconnected | 연결이 끊어졌고 재연결을 시도 중이에요. |
| Faulted | 복구할 수 없는 에러가 발생했어요. |
| ReachedEndOfTopic | 더 이상 전달되는 메시지가 없어요. |
더 알아보기 (Learn more)
- C# 클라이언트 라이브러리 설정 — C# 클라이언트를 설치하는 방법을 알아봐요.
- C# 클라이언트 초기화 — PulsarClient를 만드는 방법을 살펴봐요.
- C# 클라이언트 — C# 클라이언트 개요를 확인해요.
- 프로듀서 사용하기 — 프로듀서 개념을 자세히 알아봐요.
- 컨슈머 사용하기 — 컨슈머 개념을 자세히 알아봐요.