Search in sources :

Example 6 with SimpleMetadataStore

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

the class WatchServiceDirectoryScannerTests method testWatchServiceDirectoryScanner.

@Test
public void testWatchServiceDirectoryScanner() throws Exception {
    FileReadingMessageSource fileReadingMessageSource = new FileReadingMessageSource();
    fileReadingMessageSource.setDirectory(folder.getRoot());
    fileReadingMessageSource.setUseWatchService(true);
    fileReadingMessageSource.setWatchEvents(FileReadingMessageSource.WatchEventType.CREATE, FileReadingMessageSource.WatchEventType.MODIFY, FileReadingMessageSource.WatchEventType.DELETE);
    fileReadingMessageSource.setBeanFactory(mock(BeanFactory.class));
    final CountDownLatch removeFileLatch = new CountDownLatch(1);
    FileSystemPersistentAcceptOnceFileListFilter filter = new FileSystemPersistentAcceptOnceFileListFilter(new SimpleMetadataStore(), "test") {

        @Override
        public boolean remove(File fileToRemove) {
            removeFileLatch.countDown();
            return super.remove(fileToRemove);
        }
    };
    fileReadingMessageSource.setFilter(filter);
    fileReadingMessageSource.afterPropertiesSet();
    fileReadingMessageSource.start();
    DirectoryScanner scanner = fileReadingMessageSource.getScanner();
    assertThat(scanner.getClass().getName(), containsString("FileReadingMessageSource$WatchServiceDirectoryScanner"));
    List<File> files = scanner.listFiles(folder.getRoot());
    assertEquals(3, files.size());
    assertTrue(files.contains(top1));
    assertTrue(files.contains(foo1));
    assertTrue(files.contains(bar1));
    fileReadingMessageSource.start();
    File top2 = this.folder.newFile();
    File foo2 = File.createTempFile("foo", ".txt", this.foo);
    File bar2 = File.createTempFile("bar", ".txt", this.bar);
    File baz = new File(this.foo, "baz");
    baz.mkdir();
    File baz1 = File.createTempFile("baz", ".txt", baz);
    files = scanner.listFiles(folder.getRoot());
    int n = 0;
    Set<File> accum = new HashSet<File>(files);
    while (n++ < 300 && accum.size() != 4) {
        Thread.sleep(100);
        files = scanner.listFiles(folder.getRoot());
        accum.addAll(files);
    }
    assertEquals(4, accum.size());
    assertTrue(accum.contains(top2));
    assertTrue(accum.contains(foo2));
    assertTrue(accum.contains(bar2));
    assertTrue(accum.contains(baz1));
    /*See AbstractWatchKey#signalEvent source code:
			if(var5 >= 512) {
				var1 = StandardWatchEventKinds.OVERFLOW;
			}
		*/
    fileReadingMessageSource.start();
    List<File> filesForOverflow = new ArrayList<File>(600);
    for (int i = 0; i < 600; i++) {
        filesForOverflow.add(this.folder.newFile("" + i));
    }
    n = 0;
    while (n++ < 300 && accum.size() < 604) {
        Thread.sleep(100);
        files = scanner.listFiles(folder.getRoot());
        accum.addAll(files);
    }
    assertEquals(604, accum.size());
    for (File fileForOverFlow : filesForOverflow) {
        accum.contains(fileForOverFlow);
    }
    File baz2 = File.createTempFile("baz2", ".txt", baz);
    n = 0;
    while (n++ < 300 && accum.size() < 605) {
        Thread.sleep(100);
        files = scanner.listFiles(folder.getRoot());
        accum.addAll(files);
    }
    assertTrue(accum.contains(baz2));
    File baz2Copy = new File(baz2.getAbsolutePath());
    baz2Copy.setLastModified(baz2.lastModified() + 100000);
    n = 0;
    files.clear();
    while (n++ < 300 && files.size() < 1) {
        Thread.sleep(100);
        files = scanner.listFiles(folder.getRoot());
        accum.addAll(files);
    }
    assertEquals(1, files.size());
    assertTrue(files.contains(baz2));
    baz2.delete();
    n = 0;
    while (n++ < 300 && removeFileLatch.getCount() > 0) {
        Thread.sleep(100);
        scanner.listFiles(folder.getRoot());
    }
    assertTrue(removeFileLatch.await(10, TimeUnit.SECONDS));
    File baz3 = File.createTempFile("baz3", ".txt", baz);
    n = 0;
    Message<File> fileMessage = null;
    while (n++ < 300 && (fileMessage = fileReadingMessageSource.receive()) == null) {
        Thread.sleep(100);
    }
    assertNotNull(fileMessage);
    assertEquals(baz3, fileMessage.getPayload());
    assertThat(fileMessage.getHeaders().get(FileHeaders.RELATIVE_PATH, String.class), startsWith(TestUtils.applySystemFileSeparator("foo/baz/")));
    fileReadingMessageSource.stop();
}
Also used : ArrayList(java.util.ArrayList) SimpleMetadataStore(org.springframework.integration.metadata.SimpleMetadataStore) Matchers.containsString(org.hamcrest.Matchers.containsString) CountDownLatch(java.util.concurrent.CountDownLatch) FileSystemPersistentAcceptOnceFileListFilter(org.springframework.integration.file.filters.FileSystemPersistentAcceptOnceFileListFilter) BeanFactory(org.springframework.beans.factory.BeanFactory) File(java.io.File) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 7 with SimpleMetadataStore

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

the class FtpInboundChannelAdapterSpec method composeFilters.

@SuppressWarnings("unchecked")
private CompositeFileListFilter<FTPFile> composeFilters(FileListFilter<FTPFile> fileListFilter) {
    CompositeFileListFilter<FTPFile> compositeFileListFilter = new CompositeFileListFilter<>();
    compositeFileListFilter.addFilters(fileListFilter, new FtpPersistentAcceptOnceFileListFilter(new SimpleMetadataStore(), "ftpMessageSource"));
    return compositeFileListFilter;
}
Also used : CompositeFileListFilter(org.springframework.integration.file.filters.CompositeFileListFilter) FTPFile(org.apache.commons.net.ftp.FTPFile) SimpleMetadataStore(org.springframework.integration.metadata.SimpleMetadataStore) FtpPersistentAcceptOnceFileListFilter(org.springframework.integration.ftp.filters.FtpPersistentAcceptOnceFileListFilter)

Example 8 with SimpleMetadataStore

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

the class SftpPersistentAcceptOnceFileListFilterTests method testKeyUsingFileName.

@Test
public void testKeyUsingFileName() throws Exception {
    SftpPersistentAcceptOnceFileListFilter filter = new SftpPersistentAcceptOnceFileListFilter(new SimpleMetadataStore(), "rollback:");
    ChannelSftp channel = new ChannelSftp();
    SftpATTRS attrs = mock(SftpATTRS.class);
    @SuppressWarnings("unchecked") Constructor<LsEntry> ctor = (Constructor<LsEntry>) LsEntry.class.getDeclaredConstructors()[0];
    ctor.setAccessible(true);
    LsEntry sftpFile1 = ctor.newInstance(channel, "foo", "same", attrs);
    LsEntry sftpFile2 = ctor.newInstance(channel, "bar", "same", attrs);
    LsEntry[] files = new LsEntry[] { sftpFile1, sftpFile2 };
    List<LsEntry> now = filter.filterFiles(files);
    assertEquals(2, now.size());
    assertEquals("foo", now.get(0).getFilename());
    assertEquals("bar", now.get(1).getFilename());
    filter.close();
}
Also used : ChannelSftp(com.jcraft.jsch.ChannelSftp) Constructor(java.lang.reflect.Constructor) SftpATTRS(com.jcraft.jsch.SftpATTRS) SimpleMetadataStore(org.springframework.integration.metadata.SimpleMetadataStore) LsEntry(com.jcraft.jsch.ChannelSftp.LsEntry) Test(org.junit.Test)

Example 9 with SimpleMetadataStore

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

the class SftpPersistentAcceptOnceFileListFilterTests method testRollback.

@Test
public void testRollback() throws Exception {
    SftpPersistentAcceptOnceFileListFilter filter = new SftpPersistentAcceptOnceFileListFilter(new SimpleMetadataStore(), "rollback:");
    ChannelSftp channel = new ChannelSftp();
    SftpATTRS attrs = mock(SftpATTRS.class);
    @SuppressWarnings("unchecked") Constructor<LsEntry> ctor = (Constructor<LsEntry>) LsEntry.class.getDeclaredConstructors()[0];
    ctor.setAccessible(true);
    LsEntry sftpFile1 = ctor.newInstance(channel, "foo", "foo", attrs);
    LsEntry sftpFile2 = ctor.newInstance(channel, "bar", "bar", attrs);
    LsEntry ftpFile3 = ctor.newInstance(channel, "baz", "baz", attrs);
    LsEntry[] files = new LsEntry[] { sftpFile1, sftpFile2, ftpFile3 };
    List<LsEntry> passed = filter.filterFiles(files);
    assertTrue(Arrays.equals(files, passed.toArray()));
    List<LsEntry> 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).getFilename());
    assertEquals("baz", now.get(1).getFilename());
    now = filter.filterFiles(files);
    assertEquals(0, now.size());
    filter.close();
}
Also used : ChannelSftp(com.jcraft.jsch.ChannelSftp) Constructor(java.lang.reflect.Constructor) SftpATTRS(com.jcraft.jsch.SftpATTRS) SimpleMetadataStore(org.springframework.integration.metadata.SimpleMetadataStore) LsEntry(com.jcraft.jsch.ChannelSftp.LsEntry) Test(org.junit.Test)

Example 10 with SimpleMetadataStore

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

the class SftpStreamingInboundChannelAdapterSpec method composeFilters.

@SuppressWarnings("unchecked")
private CompositeFileListFilter<LsEntry> composeFilters(FileListFilter<LsEntry> fileListFilter) {
    CompositeFileListFilter<LsEntry> compositeFileListFilter = new CompositeFileListFilter<>();
    compositeFileListFilter.addFilters(fileListFilter, new SftpPersistentAcceptOnceFileListFilter(new SimpleMetadataStore(), "sftpStreamingMessageSource"));
    return compositeFileListFilter;
}
Also used : CompositeFileListFilter(org.springframework.integration.file.filters.CompositeFileListFilter) SimpleMetadataStore(org.springframework.integration.metadata.SimpleMetadataStore) LsEntry(com.jcraft.jsch.ChannelSftp.LsEntry) SftpPersistentAcceptOnceFileListFilter(org.springframework.integration.sftp.filters.SftpPersistentAcceptOnceFileListFilter)

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