private static void Client() { Channel channel = new Channel("10.154.0.2:55001", ChannelCredentials.Insecure); var client = new Replica.ReplicaClient(channel); var reply = client.CopyData(new CopyRequest { Topic = "test1" }); Console.WriteLine("Copied: " + reply.Message); channel.ShutdownAsync().Wait(); Console.WriteLine("Press any key to exit..."); Console.ReadKey(); }
private static void ConsumerCmd(string topic, CancellationToken cancelToken) { string brokes = "localhost:9092"; //string topic = "test2"; Channel channel = new Channel("10.154.0.2:55001", ChannelCredentials.Insecure); var client = new Replica.ReplicaClient(channel); //Console.WriteLine("Copied: " + reply.Message); var config = new Dictionary <string, object> { { "bootstrap.servers", brokes }, { "group.id", "csharp-consumer" }, { "enable.auto.commit", true }, // this is the default { "auto.commit.interval.ms", 5000 }, //{ "statistics.interval.ms", 60000 }, { "session.timeout.ms", 6000 }, { "auto.offset.reset", "smallest" } }; var consumer = new Consumer <byte[], byte[]>(config, new ByteArrayDeserializer(), new ByteArrayDeserializer()); consumer.OnMessage += (_, msg) => { while (true) { try { var reply = client.CopyData(new CopyRequest { Topic = msg.Topic, Key = ByteString.CopyFrom(msg.Key ?? new byte[0]), Value = ByteString.CopyFrom(msg.Value ?? new byte[0]), Partition = msg.Partition, Offset = msg.Offset, }); if (!reply.Success) { throw new Exception(reply.Message); } break; } catch (Exception e) { Console.WriteLine(e); Thread.Sleep(3000); } } // Console.WriteLine($"Topic: {msg.Topic} Partition: {msg.Partition} Offset: {msg.Offset} {msg.Value}"); }; consumer.OnPartitionEOF += (_, end) => Console.WriteLine($"Reached end of topic {end.Topic} partition {end.Partition}, next message will be at offset {end.Offset}"); consumer.OnError += (_, error) => Console.WriteLine($"Error: {error}"); // Raised on deserialization errors or when a consumed message has an error != NoError. consumer.OnConsumeError += (_, msg) => Console.WriteLine($"Error consuming from topic/partition/offset {msg.Topic}/{msg.Partition}/{msg.Offset}: {msg.Error}"); // consumer.OnOffsetsCommitted += (_, commit) // => // { // Console.WriteLine( // commit.Error // ? $"Failed to commit offsets: {commit.Error}" // : $"Successfully committed offsets:]"); // foreach (var offset in commit.Offsets) // { // Console.WriteLine($" {offset}"); // } // }; //var s = new object(); // Raised when the consumer is assigned a new set of partitions. consumer.OnPartitionsAssigned += (_, partitions) => { //lock (s) { var x = Interlocked.Increment(ref p); Console.WriteLine($"Assigned partitions({x}): [{string.Join(", ", partitions)}], member id: {consumer.MemberId}"); } // If you don't add a handler to the OnPartitionsAssigned event, // the below .Assign call happens automatically. If you do, you // must call .Assign explicitly in order for the consumer to // start consuming messages. consumer.Assign(partitions); }; consumer.OnPartitionsRevoked += (_, partitions) => { //lock (s) { var x = Interlocked.Decrement(ref p); Console.WriteLine($"Revoked partitions({x}): [{string.Join(", ", partitions)}]"); } // If you don't add a handler to the OnPartitionsRevoked event, // the below .Unassign call happens automatically. If you do, // you must call .Unassign explicitly in order for the consumer // to stop consuming messages from it's previously assigned // partitions. consumer.Unassign(); }; // consumer.OnStatistics += (_, json) => Console.WriteLine($"Statistics: {json}"); consumer.Subscribe(topic); Console.WriteLine($"Subscribed to: [{string.Join(", ", consumer.Subscription)}]"); Task.Run(() => { while (!cancelToken.IsCancellationRequested) { consumer.Poll(TimeSpan.FromMilliseconds(100)); } Console.WriteLine("Unsub.."); consumer.Unsubscribe(); Console.WriteLine("Unsub..done"); }); cancelToken.WaitHandle.WaitOne(); Console.WriteLine("Spin.."); var spin = SpinWait.SpinUntil(() => Interlocked.CompareExchange(ref p, 0, 0) == 0, TimeSpan.FromSeconds(300)); if (!spin) { Console.WriteLine("Warning: Did not unassigned in time!"); } else { Console.WriteLine("All unassigned in time."); } channel.ShutdownAsync().Wait(); }