use of org.apache.camel.util.LRUCache in project camel by apache.
the class ProducerCache method getCapacity.
/**
* Gets the maximum cache size (capacity).
* <p/>
* Will return <tt>-1</tt> if it cannot determine this if a custom cache was used.
*
* @return the capacity
*/
public int getCapacity() {
int capacity = -1;
if (producers instanceof LRUCache) {
LRUCache<String, Producer> cache = (LRUCache<String, Producer>) producers;
capacity = cache.getMaxCacheSize();
}
return capacity;
}
use of org.apache.camel.util.LRUCache in project camel by apache.
the class ConsumerCache method getCapacity.
/**
* Gets the maximum cache size (capacity).
* <p/>
* Will return <tt>-1</tt> if it cannot determine this if a custom cache was used.
*
* @return the capacity
*/
public int getCapacity() {
int capacity = -1;
if (consumers instanceof LRUCache) {
LRUCache<String, PollingConsumer> cache = (LRUCache<String, PollingConsumer>) consumers;
capacity = cache.getMaxCacheSize();
}
return capacity;
}
use of org.apache.camel.util.LRUCache in project camel by apache.
the class KafkaIdempotentRepository method doStart.
@Override
protected void doStart() throws Exception {
ObjectHelper.notNull(camelContext, "camelContext");
StringHelper.notEmpty(topic, "topic");
this.cache = Collections.synchronizedMap(new LRUCache<>(maxCacheSize));
if (consumerConfig == null) {
consumerConfig = new Properties();
StringHelper.notEmpty(bootstrapServers, "bootstrapServers");
consumerConfig.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers);
}
if (producerConfig == null) {
producerConfig = new Properties();
StringHelper.notEmpty(bootstrapServers, "bootstrapServers");
producerConfig.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers);
}
ObjectHelper.notNull(consumerConfig, "consumerConfig");
ObjectHelper.notNull(producerConfig, "producerConfig");
// each consumer instance must have control over its own offset, so assign a groupID at random
String groupId = UUID.randomUUID().toString();
log.debug("Creating consumer with {}[{}]", ConsumerConfig.GROUP_ID_CONFIG, groupId);
consumerConfig.put(ConsumerConfig.GROUP_ID_CONFIG, groupId);
consumerConfig.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG, Boolean.TRUE.toString());
consumerConfig.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName());
consumerConfig.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName());
consumer = new KafkaConsumer<>(consumerConfig);
producerConfig.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
producerConfig.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
// set up the producer to remove all batching on send, we want all sends to be fully synchronous
producerConfig.putIfAbsent(ProducerConfig.ACKS_CONFIG, "1");
producerConfig.putIfAbsent(ProducerConfig.BATCH_SIZE_CONFIG, "0");
producer = new KafkaProducer<>(producerConfig);
cacheReadyLatch = new CountDownLatch(1);
topicPoller = new TopicPoller(consumer, cacheReadyLatch, pollDurationMs);
// warm up the cache
executorService = camelContext.getExecutorServiceManager().newSingleThreadExecutor(this, "KafkaIdempotentRepository");
executorService.submit(topicPoller);
log.info("Warming up cache from topic {}", topic);
try {
if (cacheReadyLatch.await(30, TimeUnit.SECONDS)) {
log.info("Cache OK");
} else {
log.warn("Timeout waiting for cache warm-up from topic {}. Proceeding anyway. " + "Duplicate records may not be detected.", topic);
}
} catch (InterruptedException e) {
log.warn("Interrupted while warming up cache. This exception is ignored.", e.getMessage());
}
}
Aggregations