Search in sources :

Example 1 with CachingSessionFactory

use of org.springframework.integration.file.remote.session.CachingSessionFactory in project spring-integration by spring-projects.

the class SessionFactoryTests method testConnectionLimit.

@Test
@Ignore
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(() -> {
            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());
}
Also used : CachingSessionFactory(org.springframework.integration.file.remote.session.CachingSessionFactory) Random(java.util.Random) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ExecutorService(java.util.concurrent.ExecutorService) MessagingException(org.springframework.messaging.MessagingException) Session(org.springframework.integration.file.remote.session.Session) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 2 with CachingSessionFactory

use of org.springframework.integration.file.remote.session.CachingSessionFactory in project spring-integration by spring-projects.

the class SftpOutboundTests method testSharedSessionCachedReset.

@Test
public void testSharedSessionCachedReset() throws Exception {
    JSch jsch = spy(new JSch());
    Constructor<com.jcraft.jsch.Session> ctor = com.jcraft.jsch.Session.class.getDeclaredConstructor(JSch.class, String.class, String.class, int.class);
    ctor.setAccessible(true);
    com.jcraft.jsch.Session jschSession1 = spy(ctor.newInstance(jsch, "foo", "host", 22));
    com.jcraft.jsch.Session jschSession2 = spy(ctor.newInstance(jsch, "foo", "host", 22));
    willAnswer(invocation -> {
        new DirectFieldAccessor(jschSession1).setPropertyValue("isConnected", true);
        return null;
    }).given(jschSession1).connect();
    willAnswer(invocation -> {
        new DirectFieldAccessor(jschSession2).setPropertyValue("isConnected", true);
        return null;
    }).given(jschSession2).connect();
    when(jsch.getSession("foo", "host", 22)).thenReturn(jschSession1, jschSession2);
    final ChannelSftp channel1 = spy(new ChannelSftp());
    doReturn("channel1").when(channel1).toString();
    final ChannelSftp channel2 = spy(new ChannelSftp());
    doReturn("channel2").when(channel2).toString();
    final ChannelSftp channel3 = spy(new ChannelSftp());
    doReturn("channel3").when(channel3).toString();
    final ChannelSftp channel4 = spy(new ChannelSftp());
    doReturn("channel4").when(channel4).toString();
    new DirectFieldAccessor(channel1).setPropertyValue("session", jschSession1);
    new DirectFieldAccessor(channel2).setPropertyValue("session", jschSession1);
    // Can't use when(session.open()) with a spy
    final AtomicInteger n = new AtomicInteger();
    doAnswer(invocation -> n.getAndIncrement() == 0 ? channel1 : channel2).when(jschSession1).openChannel("sftp");
    doAnswer(invocation -> n.getAndIncrement() < 3 ? channel3 : channel4).when(jschSession2).openChannel("sftp");
    DefaultSftpSessionFactory factory = new DefaultSftpSessionFactory(jsch, true);
    factory.setHost("host");
    factory.setUser("foo");
    factory.setPassword("bar");
    CachingSessionFactory<LsEntry> cachedFactory = new CachingSessionFactory<LsEntry>(factory);
    noopConnect(channel1);
    noopConnect(channel2);
    noopConnect(channel3);
    noopConnect(channel4);
    Session<LsEntry> s1 = cachedFactory.getSession();
    Session<LsEntry> s2 = cachedFactory.getSession();
    assertSame(jschSession1, TestUtils.getPropertyValue(s2, "targetSession.jschSession"));
    assertSame(channel1, TestUtils.getPropertyValue(s1, "targetSession.channel"));
    assertSame(channel2, TestUtils.getPropertyValue(s2, "targetSession.channel"));
    assertSame(TestUtils.getPropertyValue(s1, "targetSession.jschSession"), TestUtils.getPropertyValue(s2, "targetSession.jschSession"));
    s1.close();
    Session<LsEntry> s3 = cachedFactory.getSession();
    assertSame(TestUtils.getPropertyValue(s1, "targetSession"), TestUtils.getPropertyValue(s3, "targetSession"));
    assertSame(channel1, TestUtils.getPropertyValue(s3, "targetSession.channel"));
    s3.close();
    cachedFactory.resetCache();
    verify(jschSession1, never()).disconnect();
    s3 = cachedFactory.getSession();
    assertSame(jschSession2, TestUtils.getPropertyValue(s3, "targetSession.jschSession"));
    assertNotSame(TestUtils.getPropertyValue(s1, "targetSession"), TestUtils.getPropertyValue(s3, "targetSession"));
    assertSame(channel3, TestUtils.getPropertyValue(s3, "targetSession.channel"));
    s2.close();
    verify(jschSession1).disconnect();
    s2 = cachedFactory.getSession();
    assertSame(jschSession2, TestUtils.getPropertyValue(s2, "targetSession.jschSession"));
    assertNotSame(TestUtils.getPropertyValue(s3, "targetSession"), TestUtils.getPropertyValue(s2, "targetSession"));
    assertSame(channel4, TestUtils.getPropertyValue(s2, "targetSession.channel"));
    s2.close();
    s3.close();
    verify(jschSession2, never()).disconnect();
    cachedFactory.resetCache();
    verify(jschSession2).disconnect();
}
Also used : CachingSessionFactory(org.springframework.integration.file.remote.session.CachingSessionFactory) JSch(com.jcraft.jsch.JSch) DefaultSftpSessionFactory(org.springframework.integration.sftp.session.DefaultSftpSessionFactory) ChannelSftp(com.jcraft.jsch.ChannelSftp) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) LsEntry(com.jcraft.jsch.ChannelSftp.LsEntry) Session(org.springframework.integration.file.remote.session.Session) SftpSession(org.springframework.integration.sftp.session.SftpSession) Test(org.junit.Test)

Example 3 with CachingSessionFactory

use of org.springframework.integration.file.remote.session.CachingSessionFactory in project spring-integration by spring-projects.

the class SftpTestSupport method sessionFactory.

public static SessionFactory<LsEntry> sessionFactory() {
    DefaultSftpSessionFactory factory = new DefaultSftpSessionFactory(true);
    factory.setHost("localhost");
    factory.setPort(port);
    factory.setUser("foo");
    factory.setPassword("foo");
    factory.setAllowUnknownKeys(true);
    return new CachingSessionFactory<LsEntry>(factory);
}
Also used : CachingSessionFactory(org.springframework.integration.file.remote.session.CachingSessionFactory) DefaultSftpSessionFactory(org.springframework.integration.sftp.session.DefaultSftpSessionFactory)

Example 4 with CachingSessionFactory

use of org.springframework.integration.file.remote.session.CachingSessionFactory in project spring-integration by spring-projects.

the class RemoteFileTemplate method execute.

@SuppressWarnings("rawtypes")
@Override
public <T> T execute(SessionCallback<F, T> callback) {
    Session<F> session = null;
    boolean invokeScope = false;
    if (this.activeTemplateCallbacks.get() > 0) {
        session = this.contextSessions.get();
    }
    try {
        if (session == null) {
            session = this.sessionFactory.getSession();
        } else {
            invokeScope = true;
        }
        return callback.doInSession(session);
    } catch (Exception e) {
        if (session instanceof CachingSessionFactory<?>.CachedSession) {
            ((CachingSessionFactory.CachedSession) session).dirty();
        }
        if (e instanceof MessagingException) {
            throw (MessagingException) e;
        }
        throw new MessagingException("Failed to execute on session", e);
    } finally {
        if (!invokeScope) {
            try {
                session.close();
            } catch (Exception ignored) {
                if (this.logger.isDebugEnabled()) {
                    this.logger.debug("failed to close Session", ignored);
                }
            }
        }
    }
}
Also used : CachingSessionFactory(org.springframework.integration.file.remote.session.CachingSessionFactory) MessagingException(org.springframework.messaging.MessagingException) MessagingException(org.springframework.messaging.MessagingException) IOException(java.io.IOException) BeansException(org.springframework.beans.BeansException) FileNotFoundException(java.io.FileNotFoundException) MessageDeliveryException(org.springframework.messaging.MessageDeliveryException)

Example 5 with CachingSessionFactory

use of org.springframework.integration.file.remote.session.CachingSessionFactory in project spring-integration by spring-projects.

the class SftpOutboundGatewayParserTests method testGateway2.

@Test
public void testGateway2() throws Exception {
    SftpOutboundGateway gateway = TestUtils.getPropertyValue(gateway2, "handler", SftpOutboundGateway.class);
    assertEquals("X", TestUtils.getPropertyValue(gateway, "remoteFileTemplate.remoteFileSeparator"));
    assertNotNull(TestUtils.getPropertyValue(gateway, "remoteFileTemplate.sessionFactory"));
    assertTrue(TestUtils.getPropertyValue(gateway, "remoteFileTemplate.sessionFactory") instanceof CachingSessionFactory);
    assertNotNull(TestUtils.getPropertyValue(gateway, "outputChannel"));
    assertEquals("local-test-dir", TestUtils.getPropertyValue(gateway, "localDirectoryExpression.literalValue"));
    assertFalse((Boolean) TestUtils.getPropertyValue(gateway, "autoCreateLocalDirectory"));
    assertEquals(Command.GET, TestUtils.getPropertyValue(gateway, "command"));
    assertFalse(TestUtils.getPropertyValue(gateway, "requiresReply", Boolean.class));
    @SuppressWarnings("unchecked") Set<String> options = TestUtils.getPropertyValue(gateway, "options", Set.class);
    assertTrue(options.contains(Option.PRESERVE_TIMESTAMP));
    // INT-3129
    assertNotNull(TestUtils.getPropertyValue(gateway, "localFilenameGeneratorExpression"));
    final AtomicReference<Method> genMethod = new AtomicReference<Method>();
    ReflectionUtils.doWithMethods(SftpOutboundGateway.class, method -> {
        method.setAccessible(true);
        genMethod.set(method);
    }, method -> "generateLocalFileName".equals(method.getName()));
    assertEquals("FOO.afoo", genMethod.get().invoke(gateway, new GenericMessage<String>(""), "foo"));
    assertThat(TestUtils.getPropertyValue(gateway, "mputFilter"), Matchers.instanceOf(SimplePatternFileListFilter.class));
}
Also used : SftpOutboundGateway(org.springframework.integration.sftp.gateway.SftpOutboundGateway) GenericMessage(org.springframework.messaging.support.GenericMessage) CachingSessionFactory(org.springframework.integration.file.remote.session.CachingSessionFactory) AtomicReference(java.util.concurrent.atomic.AtomicReference) Method(java.lang.reflect.Method) SimplePatternFileListFilter(org.springframework.integration.file.filters.SimplePatternFileListFilter) Test(org.junit.Test)

Aggregations

CachingSessionFactory (org.springframework.integration.file.remote.session.CachingSessionFactory)9 Test (org.junit.Test)6 Session (org.springframework.integration.file.remote.session.Session)5 SessionFactory (org.springframework.integration.file.remote.session.SessionFactory)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 DefaultSftpSessionFactory (org.springframework.integration.sftp.session.DefaultSftpSessionFactory)2 MessagingException (org.springframework.messaging.MessagingException)2 ChannelSftp (com.jcraft.jsch.ChannelSftp)1 LsEntry (com.jcraft.jsch.ChannelSftp.LsEntry)1 JSch (com.jcraft.jsch.JSch)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 Method (java.lang.reflect.Method)1 Random (java.util.Random)1 ExecutorService (java.util.concurrent.ExecutorService)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 Ignore (org.junit.Ignore)1 BeansException (org.springframework.beans.BeansException)1 DirectFieldAccessor (org.springframework.beans.DirectFieldAccessor)1 SimplePatternFileListFilter (org.springframework.integration.file.filters.SimplePatternFileListFilter)1