|
1 | | -using System; |
2 | | -using System.Collections.Generic; |
3 | | -using System.Text; |
| 1 | +using Akka.Actor; |
| 2 | +using Akka.Event; |
| 3 | +using AkkaDotModule.Kafka; |
| 4 | +using AkkaDotModule.Models; |
| 5 | +using Confluent.Kafka; |
| 6 | +using System; |
| 7 | +using System.Threading; |
4 | 8 |
|
5 | 9 | namespace AkkaDotModule.ActorUtils.Confluent |
6 | 10 | { |
7 | | - //TODO : 준비중....소비자는 생산자보다 조금더 난이도가 있습니다. |
8 | | - class ConsumerActor |
| 11 | + public class ConsumerStart |
9 | 12 | { |
| 13 | + }; |
| 14 | + |
| 15 | + public class ConsumerPull |
| 16 | + { |
| 17 | + }; |
| 18 | + |
| 19 | + public class ConsumerStop |
| 20 | + { |
| 21 | + }; |
| 22 | + |
| 23 | + public class ConsumerActor : ReceiveActor |
| 24 | + { |
| 25 | + private readonly string topic; |
| 26 | + |
| 27 | + private readonly ConsumerConfig consumerConfig; |
| 28 | + |
| 29 | + private readonly CancellationTokenSource cancellationTokenSource; |
| 30 | + |
| 31 | + private readonly IConsumer<Ignore, string> consumer; |
| 32 | + |
| 33 | + private readonly IActorRef workActor; |
| 34 | + |
| 35 | + private readonly ILoggingAdapter logger = Context.GetLogger(); |
| 36 | + |
| 37 | + public ConsumerActor(ConsumerAkkaOption consumerAkkaOption) |
| 38 | + { |
| 39 | + topic = consumerAkkaOption.Topics; |
| 40 | + cancellationTokenSource = new CancellationTokenSource(); |
| 41 | + workActor = consumerAkkaOption.RelayActor; |
| 42 | + |
| 43 | + if (consumerAkkaOption.SecurityOption != null) |
| 44 | + { |
| 45 | + consumerConfig = new ConsumerConfig() |
| 46 | + { |
| 47 | + GroupId = consumerAkkaOption.KafkaGroupId, |
| 48 | + BootstrapServers = consumerAkkaOption.BootstrapServers, |
| 49 | + AutoOffsetReset = consumerAkkaOption.AutoOffsetReset, |
| 50 | + //For Security |
| 51 | + SecurityProtocol = consumerAkkaOption.SecurityOption.SecurityProtocol, |
| 52 | + SaslMechanism = consumerAkkaOption.SecurityOption.SaslMechanism, |
| 53 | + SaslUsername = consumerAkkaOption.SecurityOption.SaslUsername, |
| 54 | + SaslPassword = consumerAkkaOption.SecurityOption.SaslPassword, |
| 55 | + SslCaLocation = consumerAkkaOption.SecurityOption.SslCaLocation, |
| 56 | + }; |
| 57 | + } |
| 58 | + else |
| 59 | + { |
| 60 | + consumerConfig = new ConsumerConfig() |
| 61 | + { |
| 62 | + GroupId = consumerAkkaOption.KafkaGroupId, |
| 63 | + BootstrapServers = consumerAkkaOption.BootstrapServers, |
| 64 | + AutoOffsetReset = consumerAkkaOption.AutoOffsetReset, |
| 65 | + }; |
| 66 | + } |
| 67 | + |
| 68 | + consumer = new ConsumerBuilder<Ignore, string>(consumerConfig).Build(); |
| 69 | + |
| 70 | + ReceiveAsync<ConsumerStart>(async msg => |
| 71 | + { |
| 72 | + IActorRef selfActor = this.Self; |
| 73 | + consumer.Subscribe(topic); |
| 74 | + |
| 75 | + var cr = consumer.Consume(cancellationTokenSource.Token); |
| 76 | + selfActor.Tell(new KafkaTextMessage() |
| 77 | + { |
| 78 | + Topic = cr.Topic, |
| 79 | + Message = cr.Message.Value |
| 80 | + }); |
| 81 | + }); |
| 82 | + |
| 83 | + ReceiveAsync<ConsumerPull>(async msg => |
| 84 | + { |
| 85 | + IActorRef selfActor = this.Self; |
| 86 | + var cr = consumer.Consume(cancellationTokenSource.Token); |
| 87 | + selfActor.Tell(new KafkaTextMessage() |
| 88 | + { |
| 89 | + Topic = cr.Topic, |
| 90 | + Message = cr.Message.Value |
| 91 | + }); |
| 92 | + }); |
| 93 | + |
| 94 | + ReceiveAsync<KafkaTextMessage>(async msg => |
| 95 | + { |
| 96 | + IActorRef selfActor = this.Self; |
| 97 | + //이 액터는 카프카 메시지소비만 담당하며 |
| 98 | + //소비된 메시지는 작업 액터에게 전달한다. |
| 99 | + string logText = $"Consumed message '{msg.Message}' Topic: '{msg.Topic}'."; |
| 100 | + logger.Debug(logText); |
| 101 | + |
| 102 | + if (workActor != null) |
| 103 | + { |
| 104 | + workActor.Tell(msg); |
| 105 | + } |
| 106 | + selfActor.Tell(new ConsumerPull()); |
| 107 | + }); |
| 108 | + } |
| 109 | + |
| 110 | + protected override void PostStop() |
| 111 | + { |
| 112 | + Console.WriteLine("try down KafkaConsumer....."); |
| 113 | + cancellationTokenSource.Cancel(); |
| 114 | + } |
| 115 | + |
10 | 116 | } |
11 | 117 | } |
0 commit comments