Search in sources :

Example 1 with ConcurrentMetadataStore

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();
}
Also used : SimpleMetadataStore(org.springframework.integration.metadata.SimpleMetadataStore) CountDownLatch(java.util.concurrent.CountDownLatch) ConcurrentMetadataStore(org.springframework.integration.metadata.ConcurrentMetadataStore) IOException(java.io.IOException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) File(java.io.File) Test(org.junit.Test)

Example 2 with ConcurrentMetadataStore

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());
}
Also used : ExpressionEvaluatingMessageProcessor(org.springframework.integration.handler.ExpressionEvaluatingMessageProcessor) Message(org.springframework.messaging.Message) GenericMessage(org.springframework.messaging.support.GenericMessage) MessageHandler(org.springframework.messaging.MessageHandler) ProxyFactory(org.springframework.aop.framework.ProxyFactory) AtomicReference(java.util.concurrent.atomic.AtomicReference) SimpleMetadataStore(org.springframework.integration.metadata.SimpleMetadataStore) ConcurrentMetadataStore(org.springframework.integration.metadata.ConcurrentMetadataStore) MessageRejectedException(org.springframework.integration.MessageRejectedException) MetadataStoreSelector(org.springframework.integration.selector.MetadataStoreSelector) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) BeanFactory(org.springframework.beans.factory.BeanFactory) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)2 ConcurrentMetadataStore (org.springframework.integration.metadata.ConcurrentMetadataStore)2 SimpleMetadataStore (org.springframework.integration.metadata.SimpleMetadataStore)2 File (java.io.File)1 IOException (java.io.IOException)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 ProxyFactory (org.springframework.aop.framework.ProxyFactory)1 BeanFactory (org.springframework.beans.factory.BeanFactory)1 SpelExpressionParser (org.springframework.expression.spel.standard.SpelExpressionParser)1 MessageRejectedException (org.springframework.integration.MessageRejectedException)1 ExpressionEvaluatingMessageProcessor (org.springframework.integration.handler.ExpressionEvaluatingMessageProcessor)1 MetadataStoreSelector (org.springframework.integration.selector.MetadataStoreSelector)1 Message (org.springframework.messaging.Message)1 MessageHandler (org.springframework.messaging.MessageHandler)1 GenericMessage (org.springframework.messaging.support.GenericMessage)1