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

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

Introduction

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

Prototype

public CachingSessionFactory(SessionFactory<F> sessionFactory, int sessionCacheSize) 

Source Link

Document

Create a CachingSessionFactory with the specified session limit.

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();/*w ww  .  jav a 2s. com*/
    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();/*from   ww w  .  ja v a 2s. co 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();//from   www  .j  a  v  a  2 s.  co m
    cachingFactory.getSession();
    cachingFactory.getSession();
}

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

@Test
@Ignore/* w ww  .  jav a 2s .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());
}