io.github.aneudeveloper.func.delay.wait.WaitTimeHandler Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of func-delay Show documentation
Show all versions of func-delay Show documentation
Delayed return of func messages.
The newest version!
/**
* Copyright 2022 aneu
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the * "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
package io.github.aneudeveloper.func.delay.wait;
import java.time.Duration;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.CountDownLatch;
import org.apache.kafka.clients.consumer.ConsumerGroupMetadata;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.apache.kafka.clients.consumer.OffsetAndMetadata;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.common.TopicPartition;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import io.github.aneudeveloper.func.delay.TopicSelector;
public class WaitTimeHandler implements Runnable {
private static final Logger LOG = LoggerFactory.getLogger(WaitTimeHandler.class);
private TopicSelector.WaitTopic waitInterval;
private KafkaProducer kafkaProducer;
private KafkaConsumer consumer;
private TopicSelector topicSelector;
private CountDownLatch countDownLatch;
private ConsumerGroupMetadata consumerGroupMetadata;
private Date lastPollDate;
public WaitTimeHandler(Properties aCommonKafkaProducerProperties, String transactioIdPrefix,
TopicSelector.WaitTopic waitInterval, TopicSelector topicSelector, Properties aCommonConsumerProperties,
String consumerGroupPrefix) {
this.waitInterval = waitInterval;
this.topicSelector = topicSelector;
this.countDownLatch = new CountDownLatch(1);
LOG.info("Create WaitTimeHandler with waitInterval timeDefinition={} topicName={} waitTime={}",
waitInterval.getTimeDefinition(), waitInterval.getTopicName(), waitInterval.getWaitTime());
this.consumerGroupMetadata = new ConsumerGroupMetadata(consumerGroupPrefix + waitInterval.getTimeDefinition());
Properties consumerProperties = new Properties();
consumerProperties.putAll((Map, ?>) aCommonConsumerProperties);
consumerProperties.put("group.id", consumerGroupMetadata.groupId());
LOG.info("Adding group.id={}", consumerGroupMetadata.groupId());
int waitTimeMillisForTopic = waitInterval.getWaitTime().intValue();
int maxToWaitMillis = waitTimeMillisForTopic + 300000;
if (waitTimeMillisForTopic >= 10800000) {
maxToWaitMillis = waitTimeMillisForTopic + 900000;
} else if (waitTimeMillisForTopic >= 43200000) {
maxToWaitMillis = waitTimeMillisForTopic + 1800000;
}
consumerProperties.put("max.poll.interval.ms", maxToWaitMillis);
consumerProperties.put("connections.max.idle.ms", maxToWaitMillis);
consumerProperties.put("request.timeout.ms", maxToWaitMillis);
consumerProperties.put("fetch.min.bytes", Integer.MAX_VALUE);
consumerProperties.put("fetch.max.wait.ms", waitTimeMillisForTopic);
this.consumer = new KafkaConsumer(consumerProperties);
LOG.info("Subscribe to topic {}", waitInterval.getTopicName());
for (Map.Entry