Search in sources :

Example 1 with LRUCache

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;
}
Also used : LRUCache(org.apache.camel.util.LRUCache) Producer(org.apache.camel.Producer) Endpoint(org.apache.camel.Endpoint)

Example 2 with LRUCache

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;
}
Also used : LRUCache(org.apache.camel.util.LRUCache) PollingConsumer(org.apache.camel.PollingConsumer) Endpoint(org.apache.camel.Endpoint)

Example 3 with LRUCache

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());
    }
}
Also used : LRUCache(org.apache.camel.util.LRUCache) StringDeserializer(org.apache.kafka.common.serialization.StringDeserializer) Properties(java.util.Properties) CountDownLatch(java.util.concurrent.CountDownLatch) StringSerializer(org.apache.kafka.common.serialization.StringSerializer)

Aggregations

LRUCache (org.apache.camel.util.LRUCache)3 Endpoint (org.apache.camel.Endpoint)2 Properties (java.util.Properties)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 PollingConsumer (org.apache.camel.PollingConsumer)1 Producer (org.apache.camel.Producer)1 StringDeserializer (org.apache.kafka.common.serialization.StringDeserializer)1 StringSerializer (org.apache.kafka.common.serialization.StringSerializer)1