use of org.springframework.integration.metadata.ConcurrentMetadataStore in project spring-integration by spring-projects.
the class PersistentAcceptOnceFileListFilterTests method testFileSystem.
@Test
public void testFileSystem() throws Exception {
final AtomicBoolean suspend = new AtomicBoolean();
final CountDownLatch latch1 = new CountDownLatch(1);
final CountDownLatch latch2 = new CountDownLatch(1);
ConcurrentMetadataStore store = new SimpleMetadataStore() {
@Override
public boolean replace(String key, String oldValue, String newValue) {
if (suspend.get()) {
latch2.countDown();
try {
latch1.await(10, TimeUnit.SECONDS);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
return super.replace(key, oldValue, newValue);
}
};
final FileSystemPersistentAcceptOnceFileListFilter filter = new FileSystemPersistentAcceptOnceFileListFilter(store, "foo:");
final File file = File.createTempFile("foo", ".txt");
assertEquals(1, filter.filterFiles(new File[] { file }).size());
String ts = store.get("foo:" + file.getAbsolutePath());
assertEquals(String.valueOf(file.lastModified()), ts);
assertEquals(0, filter.filterFiles(new File[] { file }).size());
file.setLastModified(file.lastModified() + 5000L);
assertEquals(1, filter.filterFiles(new File[] { file }).size());
ts = store.get("foo:" + file.getAbsolutePath());
assertEquals(String.valueOf(file.lastModified()), ts);
assertEquals(0, filter.filterFiles(new File[] { file }).size());
suspend.set(true);
file.setLastModified(file.lastModified() + 5000L);
Future<Integer> result = Executors.newSingleThreadExecutor().submit(new Callable<Integer>() {
@Override
public Integer call() throws Exception {
return filter.filterFiles(new File[] { file }).size();
}
});
assertTrue(latch2.await(10, TimeUnit.SECONDS));
store.put("foo:" + file.getAbsolutePath(), "43");
latch1.countDown();
Integer theResult = result.get(10, TimeUnit.SECONDS);
// lost the race, key changed
assertEquals(Integer.valueOf(0), theResult);
file.delete();
filter.close();
}
use of org.springframework.integration.metadata.ConcurrentMetadataStore in project spring-integration by spring-projects.
the class IdempotentReceiverTests method testIdempotentReceiverInterceptor.
@Test
public void testIdempotentReceiverInterceptor() {
ConcurrentMetadataStore store = new SimpleMetadataStore();
ExpressionEvaluatingMessageProcessor<String> idempotentKeyStrategy = new ExpressionEvaluatingMessageProcessor<>(new SpelExpressionParser().parseExpression("payload"));
BeanFactory beanFactory = Mockito.mock(BeanFactory.class);
idempotentKeyStrategy.setBeanFactory(beanFactory);
IdempotentReceiverInterceptor idempotentReceiverInterceptor = new IdempotentReceiverInterceptor(new MetadataStoreSelector(idempotentKeyStrategy, store));
idempotentReceiverInterceptor.setThrowExceptionOnRejection(true);
AtomicReference<Message<?>> handled = new AtomicReference<>();
MessageHandler idempotentReceiver = handled::set;
ProxyFactory proxyFactory = new ProxyFactory(idempotentReceiver);
proxyFactory.addAdvice(idempotentReceiverInterceptor);
idempotentReceiver = (MessageHandler) proxyFactory.getProxy();
idempotentReceiver.handleMessage(new GenericMessage<>("foo"));
assertEquals(1, TestUtils.getPropertyValue(store, "metadata", Map.class).size());
assertNotNull(store.get("foo"));
try {
idempotentReceiver.handleMessage(new GenericMessage<>("foo"));
fail("MessageRejectedException expected");
} catch (Exception e) {
assertThat(e, instanceOf(MessageRejectedException.class));
}
idempotentReceiverInterceptor.setThrowExceptionOnRejection(false);
idempotentReceiver.handleMessage(new GenericMessage<>("foo"));
assertTrue(handled.get().getHeaders().get(IntegrationMessageHeaderAccessor.DUPLICATE_MESSAGE, Boolean.class));
assertEquals(1, TestUtils.getPropertyValue(store, "metadata", Map.class).size());
}
Aggregations