Search in sources :

Example 11 with SimpleMetadataStore

use of org.springframework.integration.metadata.SimpleMetadataStore in project spring-integration by spring-projects.

the class FeedEntryMessageSource method onInit.

@Override
protected void onInit() throws Exception {
    if (this.metadataStore == null) {
        // first try to look for a 'messageStore' in the context
        BeanFactory beanFactory = this.getBeanFactory();
        if (beanFactory != null) {
            this.metadataStore = IntegrationContextUtils.getMetadataStore(beanFactory);
        }
        // if no 'messageStore' in context, fall back to in-memory Map-based default
        if (this.metadataStore == null) {
            this.metadataStore = new SimpleMetadataStore();
        }
    }
    String lastTimeValue = this.metadataStore.get(this.metadataKey);
    if (StringUtils.hasText(lastTimeValue)) {
        this.lastTime = Long.parseLong(lastTimeValue);
    }
    this.initialized = true;
}
Also used : BeanFactory(org.springframework.beans.factory.BeanFactory) SimpleMetadataStore(org.springframework.integration.metadata.SimpleMetadataStore)

Example 12 with SimpleMetadataStore

use of org.springframework.integration.metadata.SimpleMetadataStore 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)

Example 13 with SimpleMetadataStore

use of org.springframework.integration.metadata.SimpleMetadataStore in project spring-integration by spring-projects.

the class PersistentAcceptOnceFileListFilterTests method testRollback.

@Override
@Test
public void testRollback() {
    AbstractPersistentAcceptOnceFileListFilter<String> filter = new AbstractPersistentAcceptOnceFileListFilter<String>(new SimpleMetadataStore(), "rollback:") {

        @Override
        protected long modified(String file) {
            return 0;
        }

        @Override
        protected String fileName(String file) {
            return file;
        }
    };
    doTestRollback(filter);
}
Also used : SimpleMetadataStore(org.springframework.integration.metadata.SimpleMetadataStore) Test(org.junit.Test)

Example 14 with SimpleMetadataStore

use of org.springframework.integration.metadata.SimpleMetadataStore in project spring-integration by spring-projects.

the class AbstractInboundFileSynchronizingMessageSource method afterPropertiesSet.

@Override
public void afterPropertiesSet() throws Exception {
    super.afterPropertiesSet();
    Assert.notNull(this.localDirectory, "localDirectory must not be null");
    try {
        if (!this.localDirectory.exists()) {
            if (this.autoCreateLocalDirectory) {
                if (logger.isDebugEnabled()) {
                    logger.debug("The '" + this.localDirectory + "' directory doesn't exist; Will create.");
                }
                this.localDirectory.mkdirs();
            } else {
                throw new FileNotFoundException(this.localDirectory.getName());
            }
        }
        this.fileSource.setDirectory(this.localDirectory);
        if (this.localFileListFilter == null) {
            this.localFileListFilter = new FileSystemPersistentAcceptOnceFileListFilter(new SimpleMetadataStore(), getComponentName());
        }
        FileListFilter<File> filter = buildFilter();
        if (this.scannerExplicitlySet) {
            Assert.state(!this.fileSource.isUseWatchService(), "'useWatchService' and 'scanner' are mutually exclusive.");
            this.fileSource.getScanner().setFilter(filter);
        } else if (!this.fileSource.isUseWatchService()) {
            DirectoryScanner directoryScanner = new DefaultDirectoryScanner();
            directoryScanner.setFilter(filter);
            this.fileSource.setScanner(directoryScanner);
        } else {
            this.fileSource.setFilter(filter);
        }
        if (this.getBeanFactory() != null) {
            this.fileSource.setBeanFactory(this.getBeanFactory());
        }
        this.fileSource.afterPropertiesSet();
        this.synchronizer.afterPropertiesSet();
    } catch (RuntimeException e) {
        throw e;
    } catch (Exception e) {
        throw new BeanInitializationException("Failure during initialization of MessageSource for: " + this.getClass(), e);
    }
}
Also used : BeanInitializationException(org.springframework.beans.factory.BeanInitializationException) FileSystemPersistentAcceptOnceFileListFilter(org.springframework.integration.file.filters.FileSystemPersistentAcceptOnceFileListFilter) DirectoryScanner(org.springframework.integration.file.DirectoryScanner) DefaultDirectoryScanner(org.springframework.integration.file.DefaultDirectoryScanner) FileNotFoundException(java.io.FileNotFoundException) SimpleMetadataStore(org.springframework.integration.metadata.SimpleMetadataStore) File(java.io.File) DefaultDirectoryScanner(org.springframework.integration.file.DefaultDirectoryScanner) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) BeanInitializationException(org.springframework.beans.factory.BeanInitializationException)

Example 15 with SimpleMetadataStore

use of org.springframework.integration.metadata.SimpleMetadataStore in project spring-integration by spring-projects.

the class FtpPersistentAcceptOnceFileListFilterTests method testRollback.

@Test
public void testRollback() throws Exception {
    FtpPersistentAcceptOnceFileListFilter filter = new FtpPersistentAcceptOnceFileListFilter(new SimpleMetadataStore(), "rollback:");
    FTPFile ftpFile1 = new FTPFile();
    ftpFile1.setName("foo");
    ftpFile1.setTimestamp(Calendar.getInstance());
    FTPFile ftpFile2 = new FTPFile();
    ftpFile2.setName("bar");
    ftpFile2.setTimestamp(Calendar.getInstance());
    FTPFile ftpFile3 = new FTPFile();
    ftpFile3.setName("baz");
    ftpFile3.setTimestamp(Calendar.getInstance());
    FTPFile[] files = new FTPFile[] { ftpFile1, ftpFile2, ftpFile3 };
    List<FTPFile> passed = filter.filterFiles(files);
    assertTrue(Arrays.equals(files, passed.toArray()));
    List<FTPFile> now = filter.filterFiles(files);
    assertEquals(0, now.size());
    filter.rollback(passed.get(1), passed);
    now = filter.filterFiles(files);
    assertEquals(2, now.size());
    assertEquals("bar", now.get(0).getName());
    assertEquals("baz", now.get(1).getName());
    now = filter.filterFiles(files);
    assertEquals(0, now.size());
    filter.close();
}
Also used : FTPFile(org.apache.commons.net.ftp.FTPFile) SimpleMetadataStore(org.springframework.integration.metadata.SimpleMetadataStore) Test(org.junit.Test)

Aggregations

SimpleMetadataStore (org.springframework.integration.metadata.SimpleMetadataStore)16 Test (org.junit.Test)10 File (java.io.File)5 BeanFactory (org.springframework.beans.factory.BeanFactory)4 CompositeFileListFilter (org.springframework.integration.file.filters.CompositeFileListFilter)4 LsEntry (com.jcraft.jsch.ChannelSftp.LsEntry)3 FTPFile (org.apache.commons.net.ftp.FTPFile)3 ChannelSftp (com.jcraft.jsch.ChannelSftp)2 SftpATTRS (com.jcraft.jsch.SftpATTRS)2 IOException (java.io.IOException)2 Constructor (java.lang.reflect.Constructor)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 FileSystemPersistentAcceptOnceFileListFilter (org.springframework.integration.file.filters.FileSystemPersistentAcceptOnceFileListFilter)2 FtpPersistentAcceptOnceFileListFilter (org.springframework.integration.ftp.filters.FtpPersistentAcceptOnceFileListFilter)2 ConcurrentMetadataStore (org.springframework.integration.metadata.ConcurrentMetadataStore)2 SftpPersistentAcceptOnceFileListFilter (org.springframework.integration.sftp.filters.SftpPersistentAcceptOnceFileListFilter)2 Closeable (java.io.Closeable)1 FileNotFoundException (java.io.FileNotFoundException)1