Example usage for org.springframework.integration.file.remote.session Session close

List of usage examples for org.springframework.integration.file.remote.session Session close

Introduction

In this page you can find the example usage for org.springframework.integration.file.remote.session Session close.

Prototype

@Override
    void close();

Source Link

Usage

From source file:org.springframework.integration.file.remote.session.CachingSessionFactory.java

/**
 * Create a CachingSessionFactory with the specified session limit. By default, if
 * no sessions are available in the cache, and the size limit has been reached,
 * calling threads will block until a session is available.
 * @see #setSessionWaitTimeout(long)// w  w  w.j a v  a  2s.co  m
 * @see #setPoolSize(int)
 * @param sessionFactory the underlying session factory.
 * @param sessionCacheSize the maximum cache size.
 */
public CachingSessionFactory(SessionFactory<F> sessionFactory, int sessionCacheSize) {
    this.sessionFactory = sessionFactory;
    this.pool = new SimplePool<Session<F>>(sessionCacheSize, new SimplePool.PoolItemCallback<Session<F>>() {
        public Session<F> createForPool() {
            return CachingSessionFactory.this.sessionFactory.getSession();
        }

        public boolean isStale(Session<F> session) {
            return !session.isOpen();
        }

        public void removedFromPool(Session<F> session) {
            session.close();
        }
    });
}

From source file:org.springframework.integration.file.remote.synchronizer.AbstractInboundFileSynchronizer.java

public void synchronizeToLocalDirectory(File localDirectory) {
    Session<F> session = null;
    try {/*from ww  w  .j  a  va2  s . com*/
        session = this.sessionFactory.getSession();
        Assert.state(session != null, "failed to acquire a Session");
        F[] files = session.list(this.remoteDirectory);
        if (!ObjectUtils.isEmpty(files)) {
            Collection<F> filteredFiles = this.filterFiles(files);
            for (F file : filteredFiles) {
                if (file != null) {
                    this.copyFileToLocalDirectory(this.remoteDirectory, file, localDirectory, session);
                }
            }
        }
    } catch (IOException e) {
        throw new MessagingException("Problem occurred while synchronizing remote to local directory", e);
    } finally {
        if (session != null) {
            try {
                session.close();
            } catch (Exception ignored) {
                if (logger.isDebugEnabled()) {
                    logger.debug("failed to close Session", ignored);
                }
            }
        }
    }
}

From source file:org.springframework.integration.ftp.outbound.FtpServerOutboundTests.java

@Test
@SuppressWarnings("unchecked")
public void testMGETOnNullDir() throws IOException {
    Session<FTPFile> session = ftpSessionFactory.getSession();
    ((FTPClient) session.getClientInstance()).changeWorkingDirectory("ftpSource");
    session.close();

    this.inboundMGet.send(new GenericMessage<Object>(""));
    Message<?> result = this.output.receive(1000);
    assertNotNull(result);/*from   w  ww.  ja  v a 2  s .c  om*/
    List<File> localFiles = (List<File>) result.getPayload();

    assertThat(localFiles.size(), Matchers.greaterThan(0));

    for (File file : localFiles) {
        assertThat(file.getName(), isOneOf(" localTarget1.txt", "localTarget2.txt"));
        assertThat(file.getName(), not(containsString("null")));
    }
}

From source file:org.springframework.integration.ftp.outbound.FtpServerOutboundTests.java

@Test
public void testInt3100RawGET() throws Exception {
    Session<?> session = this.ftpSessionFactory.getSession();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    FileCopyUtils.copy(session.readRaw("ftpSource/ ftpSource1.txt"), baos);
    assertTrue(session.finalizeRaw());//from w w w .j  av a 2  s  . c o  m
    assertEquals("source1", new String(baos.toByteArray()));

    baos = new ByteArrayOutputStream();
    FileCopyUtils.copy(session.readRaw("ftpSource/ftpSource2.txt"), baos);
    assertTrue(session.finalizeRaw());
    assertEquals("source2", new String(baos.toByteArray()));

    session.close();
}

From source file:org.springframework.integration.ftp.outbound.FtpServerOutboundTests.java

private Session<FTPFile> spyOnSession() {
    Session<FTPFile> session = spy(this.ftpSessionFactory.getSession());
    session.close();
    @SuppressWarnings("unchecked")
    BlockingQueue<Session<FTPFile>> cache = TestUtils.getPropertyValue(ftpSessionFactory, "pool.available",
            BlockingQueue.class);
    assertNotNull(cache.poll());/* w  ww.  j a v  a 2  s . c o  m*/
    cache.offer(session);
    @SuppressWarnings("unchecked")
    Set<Session<FTPFile>> allocated = TestUtils.getPropertyValue(ftpSessionFactory, "pool.allocated",
            Set.class);
    allocated.clear();
    allocated.add(session);
    return session;
}

From source file:org.springframework.integration.ftp.outbound.FtpServerOutboundTests.java

@Test
@SuppressWarnings("unchecked")
public void testLsForNullDir() throws IOException {
    Session<FTPFile> session = ftpSessionFactory.getSession();
    ((FTPClient) session.getClientInstance()).changeWorkingDirectory("ftpSource");
    session.close();

    this.inboundLs.send(new GenericMessage<String>("foo"));
    Message<?> receive = this.output.receive(10000);
    assertNotNull(receive);/*from ww w. ja v  a 2s . co  m*/
    assertThat(receive.getPayload(), instanceOf(List.class));
    List<String> files = (List<String>) receive.getPayload();
    assertEquals(2, files.size());
    assertThat(files, containsInAnyOrder(" ftpSource1.txt", "ftpSource2.txt"));

    FTPFile[] ftpFiles = ftpSessionFactory.getSession().list(null);
    for (FTPFile ftpFile : ftpFiles) {
        if (!ftpFile.isDirectory()) {
            assertTrue(files.contains(ftpFile.getName()));
        }
    }
}

From source file:org.springframework.integration.ftp.outbound.FtpServerOutboundTests.java

@Test
public void testInboundChannelAdapterWithNullDir() throws IOException {
    Session<FTPFile> session = ftpSessionFactory.getSession();
    ((FTPClient) session.getClientInstance()).changeWorkingDirectory("ftpSource");
    session.close();
    this.ftpInbound.start();

    Message<?> message = this.output.receive(10000);
    assertNotNull(message);/*from w  w  w .j  a v  a2 s.co m*/
    assertThat(message.getPayload(), instanceOf(File.class));
    assertEquals(" ftpSource1.txt", ((File) message.getPayload()).getName());

    message = this.output.receive(10000);
    assertNotNull(message);
    assertThat(message.getPayload(), instanceOf(File.class));
    assertEquals("ftpSource2.txt", ((File) message.getPayload()).getName());

    assertNull(this.output.receive(10));

    this.ftpInbound.stop();
}

From source file:org.springframework.integration.ftp.session.SessionFactoryTests.java

@Test
public void testStaleConnection() throws Exception {
    SessionFactory sessionFactory = Mockito.mock(SessionFactory.class);
    Session sessionA = Mockito.mock(Session.class);
    Session sessionB = Mockito.mock(Session.class);
    Mockito.when(sessionA.isOpen()).thenReturn(true);
    Mockito.when(sessionB.isOpen()).thenReturn(false);

    Mockito.when(sessionFactory.getSession()).thenReturn(sessionA);
    Mockito.when(sessionFactory.getSession()).thenReturn(sessionB);

    CachingSessionFactory cachingFactory = new CachingSessionFactory(sessionFactory, 2);

    Session firstSession = cachingFactory.getSession();
    Session secondSession = cachingFactory.getSession();
    secondSession.close();
    Session nonStaleSession = cachingFactory.getSession();
    assertEquals(TestUtils.getPropertyValue(firstSession, "targetSession"),
            TestUtils.getPropertyValue(nonStaleSession, "targetSession"));
}

From source file:org.springframework.integration.ftp.session.SessionFactoryTests.java

@Test
public void testSameSessionFromThePool() throws Exception {
    SessionFactory sessionFactory = Mockito.mock(SessionFactory.class);
    Session session = Mockito.mock(Session.class);
    Mockito.when(sessionFactory.getSession()).thenReturn(session);

    CachingSessionFactory cachingFactory = new CachingSessionFactory(sessionFactory, 2);

    Session s1 = cachingFactory.getSession();
    s1.close();
    Session s2 = cachingFactory.getSession();
    s2.close();/*from   ww  w. j a  v a 2 s.  c  o m*/
    assertEquals(TestUtils.getPropertyValue(s1, "targetSession"),
            TestUtils.getPropertyValue(s2, "targetSession"));
    Mockito.verify(sessionFactory, Mockito.times(2)).getSession();
}

From source file:org.springframework.integration.ftp.session.SessionFactoryTests.java

@Test
@Ignore//from ww  w .ja va2 s. c o m
public void testConnectionLimit() throws Exception {
    ExecutorService executor = Executors.newCachedThreadPool();
    DefaultFtpSessionFactory sessionFactory = new DefaultFtpSessionFactory();
    sessionFactory.setHost("192.168.28.143");
    sessionFactory.setPassword("password");
    sessionFactory.setUsername("user");
    final CachingSessionFactory factory = new CachingSessionFactory(sessionFactory, 2);

    final Random random = new Random();
    final AtomicInteger failures = new AtomicInteger();
    for (int i = 0; i < 30; i++) {
        executor.execute(new Runnable() {
            public void run() {
                try {
                    Session session = factory.getSession();
                    Thread.sleep(random.nextInt(5000));
                    session.close();
                } catch (Exception e) {
                    e.printStackTrace();
                    failures.incrementAndGet();
                }
            }
        });
    }
    executor.shutdown();
    executor.awaitTermination(10000, TimeUnit.SECONDS);

    assertEquals(0, failures.get());
}