Example usage for org.springframework.integration.file.remote.session CachingSessionFactory getSession

List of usage examples for org.springframework.integration.file.remote.session CachingSessionFactory getSession

Introduction

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

Prototype

@Override
public Session<F> getSession() 

Source Link

Document

Get a session from the pool (or block if none available).

Usage

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();//from  ww w  . j  a v  a2s . c  om
    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();//  www.  ja  v  a2s  . c  o  m
    Session s2 = cachingFactory.getSession();
    s2.close();
    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(expected = MessagingException.class) // timeout expire
public void testSessionWaitExpire() 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);

    cachingFactory.setSessionWaitTimeout(3000);

    cachingFactory.getSession();
    cachingFactory.getSession();//from  w  w  w.  ja  v a2 s .c o m
    cachingFactory.getSession();
}

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

@Test
@Ignore/*from   ww  w  .ja v  a2  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());
}