publish(String body) { var config = new ProducerConfig { BootstrapServers = "localhost:9092" }; Console.WriteLine("Before Publish:" + Timestamp.DateTimeToUnixTimestampMs(DateTime.UtcNow)); string somestring = body;//JsonSerializer.Serialize(some); using (var p = new ProducerBuilder <string, string>(config).Build()) { try { var headers = new Headers(); headers.Add("CID", new byte[] { 154 }); headers.Add("Partition", new byte[] { 1 }); var dr = await p.ProduceAsync("reply-some1", new Message <string, string> { Key = "uid1", Value = somestring, Headers = headers }); Console.WriteLine("After Publish:" + Timestamp.DateTimeToUnixTimestampMs(DateTime.UtcNow)); } catch (ProduceException <Null, string> e) { Console.WriteLine($"Delivery failed: {e.Error.Reason}"); } } }
public void Conversion() { // check is to millisecond accuracy. var ts = new DateTime(2012, 5, 6, 12, 4, 3, 220, DateTimeKind.Utc); var unixTime = Timestamp.DateTimeToUnixTimestampMs(ts); var ts2 = Timestamp.UnixTimestampMsToDateTime(unixTime); Assert.Equal(ts, ts2); Assert.Equal(ts2.Kind, DateTimeKind.Utc); }
public void Rounding() { // check is to millisecond accuracy, rounding down the value var dateTimeAfterEpoch = new DateTime(2012, 5, 6, 12, 4, 3, 220, DateTimeKind.Utc); var dateTimeBeforeEpoch = new DateTime(1950, 5, 6, 12, 4, 3, 220, DateTimeKind.Utc); foreach (var datetime in new[] { dateTimeAfterEpoch, dateTimeBeforeEpoch }) { var unixTime1 = Timestamp.DateTimeToUnixTimestampMs(datetime.AddTicks(1)); var unixTime2 = Timestamp.DateTimeToUnixTimestampMs(datetime.AddTicks(TimeSpan.TicksPerMillisecond - 1)); var unixTime3 = Timestamp.DateTimeToUnixTimestampMs(datetime.AddTicks(TimeSpan.TicksPerMillisecond)); var unixTime4 = Timestamp.DateTimeToUnixTimestampMs(datetime.AddTicks(-1)); var expectedUnixTime = Timestamp.DateTimeToUnixTimestampMs(datetime); Assert.Equal(expectedUnixTime, unixTime1); Assert.Equal(expectedUnixTime, unixTime2); Assert.Equal(expectedUnixTime + 1, unixTime3); Assert.Equal(expectedUnixTime - 1, unixTime4); } }
public Message(string topic, int partition, long offset, string key, T val, DateTime creationTime, Error error) : base(topic, partition, offset, key, val, new Timestamp(Timestamp.DateTimeToUnixTimestampMs(creationTime), TimestampType.NotAvailable), new Confluent.Kafka.Error(error)) { }
public void UnixTimeEpoch() { Assert.Equal(0, Timestamp.DateTimeToUnixTimestampMs(Timestamp.UnixTimeEpoch)); Assert.Equal(DateTimeKind.Utc, Timestamp.UnixTimeEpoch.Kind); }
public async Task <JsonElement?> postTransactionAsync([FromBody] JsonElement body) { //The reply topic name string topicName = "reply-some1"; //Topic creation with AdminClient Console.WriteLine("Create Topic Before:" + Timestamp.DateTimeToUnixTimestampMs(DateTime.UtcNow)); await createTopic(topicName); Console.WriteLine("Create Topic After:" + Timestamp.DateTimeToUnixTimestampMs(DateTime.UtcNow)); //Publish to the topic await publish(body.ToString()); //Consumer config to read from Reply topic var conf = new ConsumerConfig { GroupId = "some", BootstrapServers = "localhost:9092", AutoOffsetReset = AutoOffsetReset.Earliest, //EnableAutoCommit = false, SessionTimeoutMs = 6000, QueuedMinMessages = 1000000 }; //Read from the reply topic var consumer = new ConsumerBuilder <Ignore, string>(conf).Build(); Console.WriteLine("kafka subscribe Topic: {0}, at: {1}", "transactions", DateTime.Now); Console.WriteLine("Before Subscribe:" + Timestamp.DateTimeToUnixTimestampMs(DateTime.UtcNow)); consumer.Assign(new List <TopicPartitionOffset>() { new TopicPartitionOffset(topicName, 0, 0) }); CancellationTokenSource cts = new CancellationTokenSource(); cts.CancelAfter(10000); try { bool flag = true; while (flag) { Console.WriteLine("Cosume message at: {0}", DateTime.Now); var consumedMessage = consumer.Consume(cts.Token); consumer.Commit(consumedMessage); flag = false; Console.WriteLine("After Consume:" + Timestamp.DateTimeToUnixTimestampMs(DateTime.UtcNow)); } } catch (Exception e) { Console.WriteLine("Failed While Consuming topic transaction-posted Aggregator Request and Exception: {0} at {1}", e, DateTime.Now); Console.WriteLine("Failed While Consumiong Aggregator Request: " + e.Message); //consumer.Close(); } //Once the message is read, use Admin client to delete the randomly generate topic Console.WriteLine("Delete Topic Before:" + Timestamp.DateTimeToUnixTimestampMs(DateTime.UtcNow)); await deleteTopic(topicName); Console.WriteLine("Delete Topic After:" + Timestamp.DateTimeToUnixTimestampMs(DateTime.UtcNow)); return(body); }