当前位置: 首页 > 知识库问答 >
问题:

Spring集成DSL

翟志新
2023-03-14
@Configuration
public class UploaderSftpConnectionFactoryBuilder {

@Value("${report-uploader.sftp-factory.host}")
private String host = null;
@Value("${report-uploader.sftp-factory.port}")
private Integer port = null;
@Value("classpath:${report-uploader.sftp-factory.privateKey}")
private Resource privateKey = null;
@Value("${report-uploader.sftp-factory.privateKeyPassphrase}")
private String privateKeyPassphrase = null;
@Value("${report-uploader.sftp-factory.maxConnections}")
private String maxConnections = null;
@Value("${report-uploader.sftp-factory.user}")
private String user = null;
@Value("${report-uploader.sftp-factory.poolSize}")
private Integer poolSize = null;
@Value("${report-uploader.sftp-factory.sessionWaitTimeout}")
private Long sessionWaitTimeout = null;

//@Bean(name = "cachedSftpSessionFactory")
public DefaultSftpSessionFactory getSftpSessionFactory() {
    DefaultSftpSessionFactory defaultSftpSessionFactory = new DefaultSftpSessionFactory();

    Optional.ofNullable(this.getHost()).ifPresent(value -> defaultSftpSessionFactory.setHost(value));
    Optional.ofNullable(this.getPort()).ifPresent(value -> defaultSftpSessionFactory.setPort(port));
    Optional.ofNullable(this.getPrivateKey()).ifPresent(
            value -> defaultSftpSessionFactory.setPrivateKey(privateKey));
    Optional.ofNullable(this.getPrivateKeyPassphrase()).ifPresent(
            value -> defaultSftpSessionFactory.setPrivateKeyPassphrase(value));
    Optional.ofNullable(this.getUser()).ifPresent(value -> defaultSftpSessionFactory.setUser(value));

    return defaultSftpSessionFactory;
}

@Bean(name = "cachedSftpSessionFactory")
public CachingSessionFactory<LsEntry> getCachedSftpSessionFactory() {
    CachingSessionFactory<LsEntry> cachedFtpSessionFactory = new CachingSessionFactory<LsEntry>(
            getSftpSessionFactory());
    cachedFtpSessionFactory.setPoolSize(poolSize);
    cachedFtpSessionFactory.setSessionWaitTimeout(sessionWaitTimeout);
    return cachedFtpSessionFactory;
}

public String getHost() {
    return host;
}

public void setHost(String host) {
    this.host = host;
}

public Resource getPrivateKey() {
    return privateKey;
}

public void setPrivateKey(Resource privateKey) {
    this.privateKey = privateKey;
}

public String getPrivateKeyPassphrase() {
    return privateKeyPassphrase;
}

public void setPrivateKeyPassphrase(String privateKeyPassphrase) {
    this.privateKeyPassphrase = privateKeyPassphrase;
}

public String getMaxConnections() {
    return maxConnections;
}

public void setMaxConnections(String maxConnections) {
    this.maxConnections = maxConnections;
}

public Integer getPort() {
    return port;
}

public void setPort(Integer port) {
    this.port = port;
}

public String getUser() {
    return user;
}

public void setUser(String user) {
    this.user = user;
}
@Configuration
public class TestContextConfiguration {
@Configuration
@Import({ FakeSftpServer.class, JmxAutoConfiguration.class, IntegrationAutoConfiguration.class,
        UploaderSftpConnectionFactoryBuilder.class })
@IntegrationComponentScan
public static class ContextConfiguration {

    @Value("${report-uploader.reportingServer.fileEncoding}")
    private String fileEncoding;

    @Autowired
    private FakeSftpServer fakeSftpServer;

    @Autowired
    @Qualifier("cachedSftpSessionFactory")
    //CachingSessionFactory<LsEntry> cachedSftpSessionFactory;
    DefaultSftpSessionFactory cachedSftpSessionFactory;

    @Bean
    public IntegrationFlow sftpOutboundFlow() {
        return IntegrationFlows
                .from("toSftpChannel")
                .handle(Sftp.outboundAdapter(this.cachedSftpSessionFactory, FileExistsMode.REPLACE)
                        .charset(Charset.forName(fileEncoding)).remoteFileSeparator("\\")
                        .remoteDirectory(this.fakeSftpServer.getTargetSftpDirectory().getPath())
                        .fileNameExpression("payload.getName()").autoCreateDirectory(true)
                        .useTemporaryFileName(true).temporaryFileSuffix(".tranferring")
                        ).get();
    }

    @Bean
    public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
        return new PropertySourcesPlaceholderConfigurer();
    }

}
@Configuration("fakeSftpServer")
public class FakeSftpServer implements InitializingBean, DisposableBean {

    private final SshServer server = SshServer.setUpDefaultServer();

    private final int port = SocketUtils.findAvailableTcpPort();

    private final TemporaryFolder sftpFolder;

    private final TemporaryFolder localFolder;

    private volatile File sftpRootFolder;

    private volatile File sourceSftpDirectory;

    private volatile File targetSftpDirectory;

    private volatile File sourceLocalDirectory;

    private volatile File targetLocalDirectory;

    public FakeSftpServer() {
            this.sftpFolder = new TemporaryFolder() {

                    @Override
                    public void create() throws IOException {
                            super.create();
                            sftpRootFolder = this.newFolder("sftpTest");
                            targetSftpDirectory = new File(sftpRootFolder, "sftpTarget");
                            targetSftpDirectory.mkdir();
                    }
            };

            this.localFolder = new TemporaryFolder() {

                    @Override
                    public void create() throws IOException {
                            super.create();
                            File rootFolder = this.newFolder("sftpTest");
                            sourceLocalDirectory = new File(rootFolder, "localSource");
                            sourceLocalDirectory.mkdirs();
                            File file = new File(sourceLocalDirectory, "localSource1.txt");
                            file.createNewFile();
                            file = new File(sourceLocalDirectory, "localSource2.txt");
                            file.createNewFile();

                            File subSourceLocalDirectory = new File(sourceLocalDirectory, "subLocalSource");
                            subSourceLocalDirectory.mkdir();
                            file = new File(subSourceLocalDirectory, "subLocalSource1.txt");
                            file.createNewFile();

                    }
            };
    }

    @Override
    public void afterPropertiesSet() throws Exception {
            this.sftpFolder.create();
            this.localFolder.create();

            this.server.setPasswordAuthenticator((username, password, session) -> true);
            this.server.setPort(this.port);
            this.server.setKeyPairProvider(new SimpleGeneratorHostKeyProvider(new File("src/test/resources/hostkey.ser")));
            this.server.setSubsystemFactories(Arrays.<NamedFactory<Command>>asList(new SftpSubsystemFactory()));
            this.server.setFileSystemFactory(new VirtualFileSystemFactory(sftpRootFolder.toPath()));
            this.server.start();
    }

    @Override
    public void destroy() throws Exception {
            this.server.stop();
            this.sftpFolder.delete();
            this.localFolder.delete();
    }

    public File getSourceLocalDirectory() {
            return this.sourceLocalDirectory;
    }

    public File getTargetLocalDirectory() {
            return this.targetLocalDirectory;
    }

    public String getTargetLocalDirectoryName() {
            return this.targetLocalDirectory.getAbsolutePath() + File.separator;
    }

    public File getTargetSftpDirectory() {
            return this.targetSftpDirectory;
    }

    public void recursiveDelete(File file) {
            File[] files = file.listFiles();
            if (files != null) {
                    for (File each : files) {
                            recursiveDelete(each);
                    }
            }
            if (!(file.equals(this.targetSftpDirectory) || file.equals(this.targetLocalDirectory))) {
                    file.delete();
            }
    }
}
@ContextConfiguration(classes = { TestContextConfiguration.class }, initializers = {ConfigFileApplicationContextInitializer.class})
@RunWith(SpringJUnit4ClassRunner.class)
@DirtiesContext
public class TestConnectionFactory {

@Autowired
@Qualifier("cachedSftpSessionFactory")
CachingSessionFactory<LsEntry> cachedSftpSessionFactory;
//DefaultSftpSessionFactory cachedSftpSessionFactory;

@Autowired
FakeSftpServer fakeSftpServer;

@Autowired
@Qualifier("toSftpChannel")
private MessageChannel toSftpChannel;

@After
public void setupRemoteFileServers() {
        this.fakeSftpServer.recursiveDelete(this.fakeSftpServer.getSourceLocalDirectory());
        this.fakeSftpServer.recursiveDelete(this.fakeSftpServer.getTargetSftpDirectory());
}

@Test
public void testSftpOutboundFlow() {
        boolean status = this.toSftpChannel.send(MessageBuilder.withPayload(new File(this.fakeSftpServer.getSourceLocalDirectory() + "\\" + "localSource1.txt")).build());

        RemoteFileTemplate<ChannelSftp.LsEntry> template = new RemoteFileTemplate<>(this.cachedSftpSessionFactory);
        ChannelSftp.LsEntry[] files = template.execute(session ->
                        session.list(this.fakeSftpServer.getTargetSftpDirectory() + "\\" + "localSource1.txt"));
        assertEquals(1, files.length);
        //assertEquals(3, files[0].getAttrs().getSize());
    }
}
org.springframework.messaging.MessagingException: ; nested exception is org.springframework.messaging.MessagingException: Failed to execute on session; nested exception is java.lang.IllegalStateException: failed to create SFTP Session
at org.springframework.integration.dispatcher.AbstractDispatcher.wrapExceptionIfNecessary(AbstractDispatcher.java:133)
at org.springframework.integration.dispatcher.AbstractDispatcher.tryOptimizedDispatch(AbstractDispatcher.java:120)
at org.springframework.integration.dispatcher.UnicastingDispatcher.doDispatch(UnicastingDispatcher.java:101)
at org.springframework.integration.dispatcher.UnicastingDispatcher.dispatch(UnicastingDispatcher.java:97)
at org.springframework.integration.channel.AbstractSubscribableChannel.doSend(AbstractSubscribableChannel.java:77)
at org.springframework.integration.channel.AbstractMessageChannel.send(AbstractMessageChannel.java:286)
at org.springframework.integration.channel.AbstractMessageChannel.send(AbstractMessageChannel.java:245)
at com.sapient.lufthansa.reportuploader.config.TestConnectionFactory.testSftpOutboundFlow(TestConnectionFactory.java:66)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:73)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:82)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:73)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:224)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:83)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:68)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:163)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: org.springframework.messaging.MessagingException: Failed to execute on session; nested exception is java.lang.IllegalStateException: failed to create SFTP Session
at org.springframework.integration.file.remote.RemoteFileTemplate.execute(RemoteFileTemplate.java:345)
at org.springframework.integration.file.remote.RemoteFileTemplate.send(RemoteFileTemplate.java:211)
at org.springframework.integration.file.remote.RemoteFileTemplate.send(RemoteFileTemplate.java:201)
at org.springframework.integration.file.remote.RemoteFileTemplate.send(RemoteFileTemplate.java:193)
at org.springframework.integration.file.remote.handler.FileTransferringMessageHandler.handleMessageInternal(FileTransferringMessageHandler.java:110)
at org.springframework.integration.handler.AbstractMessageHandler.handleMessage(AbstractMessageHandler.java:78)
at org.springframework.integration.dispatcher.AbstractDispatcher.tryOptimizedDispatch(AbstractDispatcher.java:116)
... 36 more
Caused by: java.lang.IllegalStateException: failed to create SFTP Session
at org.springframework.integration.sftp.session.DefaultSftpSessionFactory.getSession(DefaultSftpSessionFactory.java:355)
at org.springframework.integration.sftp.session.DefaultSftpSessionFactory.getSession(DefaultSftpSessionFactory.java:49)
at org.springframework.integration.file.remote.RemoteFileTemplate.execute(RemoteFileTemplate.java:334)
... 42 more
Caused by: java.lang.IllegalStateException: failed to connect
at org.springframework.integration.sftp.session.SftpSession.connect(SftpSession.java:272)
at org.springframework.integration.sftp.session.DefaultSftpSessionFactory.getSession(DefaultSftpSessionFactory.java:350)
... 44 more
Caused by: com.jcraft.jsch.JSchException: java.net.ConnectException: Connection refused: connect
at com.jcraft.jsch.Util.createSocket(Util.java:349)
at com.jcraft.jsch.Session.connect(Session.java:215)
at com.jcraft.jsch.Session.connect(Session.java:183)
at org.springframework.integration.sftp.session.SftpSession.connect(SftpSession.java:263)
... 45 more
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at com.jcraft.jsch.Util.createSocket(Util.java:343)
... 48 more

我无法解决这个问题,现在已经坚持了很长时间。我是一个spring-integration-dsl的初学者,任何帮助都将非常感谢。

共有1个答案

卞博简
2023-03-14

原因:java.net.connectException:拒绝连接:connect

您连接到错误的端口。

测试连接工厂监听一个随机端口--您需要使用相同的端口。

 类似资料:
  • 我是spring集成和缓存新手,想知道如何将从出站网关接收到的对象添加到缓存中。无法确定所需的配置。 从以下配置,我从rest api收到的对象正在被记录: INFO:com.domain.IpAddress@74589991 我计划使用ehcache/caffiene,任何提示都会有帮助。 编辑2: 现在,我按照建议更改了出站网关: 并将ehache配置定义如下: 在我的服务类中,定义了可缓存的

  • Spring提供了特殊的类DelegatingVariableResolver,以无缝方式将JSF和Spring集成在一起。 在JSF中集成Spring依赖注入(IOC)功能需要以下步骤。 第1步:添加DelegatingVariableResolver 在faces-config.xml中添加一个variable-resolver条目,指向spring类DelegatingVariableRes

  • 我已经建立了一个简单的Spring集成流程,该流程由以下步骤组成: 然后定期轮询一个rest api 对有效载荷做一些处理 并将其置于Kafka主题上。 请遵守以下代码: 这非常有效,然而,我正在努力想出一些好的测试。 我应该如何模拟外部RESTAPI

  • 主要内容:AspectJ Jar 包下载我们知道,Spring AOP 是一个简化版的 AOP 实现,并没有提供完整版的 AOP 功能。通常情况下,Spring AOP 是能够满足我们日常开发过程中的大多数场景的,但在某些情况下,我们可能需要使用 Spring AOP 范围外的某些 AOP 功能。 例如 Spring AOP 仅支持执行公共(public)非静态方法的调用作为连接点,如果我们需要向受保护的(protected)或私有的(

  • 我正在尝试从JMS队列(使用ActiveMQ)读取消息。我面临的问题是,消息正在从队列中读取,但没有显示在“服务激活器”中。 非常感谢您的帮助。 我的代码如下: (1) Spring配置 (2) 服务激活器MDP: (3) 申请开始课程: 谢谢

  • 我正在使用GWT和Spring集成为控制台编写代码。我已经学习了GWT教程,并阅读了一些关于如何使用spring qith GWT的论坛。 服务: 异步: Impl: 编辑:如果有用,这里是错误: com.google.gwt.user.server.rpc.unceptionedException:服务方法'public abstract java.util.collection com.san