Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ public abstract class GenericFileEndpoint<T> extends ScheduledPollEndpoint imple
+ "org.apache.camel.spi.IdempotentRepository. The in-progress repository is used to account the current in "
+ "progress files being consumed. By default a memory based repository is used.")
protected IdempotentRepository inProgressRepository
= MemoryIdempotentRepository.memoryIdempotentRepository(DEFAULT_IN_PROGRESS_CACHE_SIZE);
= MemoryIdempotentRepository.memoryIdempotentRepositoryInsertionOrder(DEFAULT_IN_PROGRESS_CACHE_SIZE);
@UriParam(label = "consumer,advanced", description = "When consuming, a local work directory can be used to "
+ "store the remote file content directly in local files, to avoid loading the content into memory. This "
+ "is beneficial, if you consume a very big remote file and thus can conserve memory.")
Expand Down Expand Up @@ -1502,7 +1502,7 @@ public IdempotentRepository getInProgressRepository() {

/**
* A pluggable in-progress repository org.apache.camel.spi.IdempotentRepository. The in-progress repository is used
* to account the current in progress files being consumed. By default a memory based repository is used.
* to account the current in progress files being consumed. By default, a memory based repository is used.
*/
public void setInProgressRepository(IdempotentRepository inProgressRepository) {
this.inProgressRepository = inProgressRepository;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
*/
package org.apache.camel.support.processor.idempotent;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

Expand Down Expand Up @@ -77,6 +79,21 @@ public static IdempotentRepository memoryIdempotentRepository(int cacheSize) {
return answer;
}

/**
* Creates a new memory based repository using a {@link java.util.LinkedHashMap} as its store, with the given
* maximum capacity. When a new entry is added and the store has reached its maximum capacity, the oldest entry is
* removed.
*/
public static IdempotentRepository memoryIdempotentRepositoryInsertionOrder(int cacheSize) {
LinkedHashMap<String, Object> map = new LinkedHashMap<>() {
@Override
protected boolean removeEldestEntry(Entry<String, Object> eldest) {
return size() > cacheSize;
}
};
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This factory method passes the map to the MemoryIdempotentRepository(Map) constructor, which does not set the cacheSize field. As a result, getMaxCacheSize() (a JMX-exposed @ManagedAttribute) will return 0.

Consider setting it:

public static IdempotentRepository memoryIdempotentRepositoryInsertionOrder(int cacheSize) {
    LinkedHashMap<String, Object> map = new LinkedHashMap<>() {
        @Override
        protected boolean removeEldestEntry(Entry<String, Object> eldest) {
            return size() > cacheSize;
        }
    };
    MemoryIdempotentRepository answer = new MemoryIdempotentRepository(map);
    answer.setCacheSize(cacheSize);
    return answer;
}

return memoryIdempotentRepository(map);
}

/**
* Creates a new memory based repository using the given {@link Map} to use to store the processed message ids.
* <p/>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.camel.support;

import java.io.IOException;

import org.apache.camel.spi.IdempotentRepository;
import org.apache.camel.support.processor.idempotent.MemoryIdempotentRepository;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

class MemoryIdempotentRepositoryTest {

@Test
void repositoryEvictsOldestEntryWhenRepositoryIsFull() throws IOException {
final int cacheSize = 5;
final int entriesNotFittingInRepository = 4;
try (IdempotentRepository repository = MemoryIdempotentRepository.memoryIdempotentRepositoryInsertionOrder(
cacheSize)) {

for (int i = 0; i < cacheSize + entriesNotFittingInRepository; i++) {
repository.add(String.valueOf(i));
}

for (int i = entriesNotFittingInRepository; i < cacheSize + entriesNotFittingInRepository; i++) {
assertTrue(repository.contains(String.valueOf(i)), "Repository should contain entry " + i);
}
for (int i = 0; i < cacheSize - entriesNotFittingInRepository; i++) {
assertFalse(repository.contains(String.valueOf(i)), "Repository should not contain entry " + i);
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The loop condition i < cacheSize - entriesNotFittingInRepository equals i < 1, so this only checks that entry "0" was evicted. It should verify all 4 evicted entries (0, 1, 2, 3) are absent.

Suggested change
}
for (int i = 0; i < entriesNotFittingInRepository; i++) {

}
}
}