为什么我的 Alpakka SFTP 连接器永远无法连接?
Why does my Alpakka SFTP connector never connect?
我是 Alpakka/Akka Streams 的新手,我正在尝试设置一个流,在其中我在两个 SFTP 服务器之间传输数据,我的系统在中间,这是代码。
import akka.actor.ActorSystem
import akka.stream.ActorMaterializer
import akka.stream.alpakka.ftp.scaladsl.Sftp
import akka.stream.alpakka.ftp.{FtpCredentials, SftpSettings}
import akka.stream.scaladsl.Keep
import net.schmizz.sshj.{DefaultConfig, SSHClient}
import java.net.InetAddress
class StreamingSftpTransport {
implicit val system: ActorSystem = ActorSystem("dr-service")
implicit val materializer: ActorMaterializer = ActorMaterializer()
private val PORT = 22
private val USER = "testsftp"
private val CREDENTIALS = FtpCredentials.create(USER, "t3st123")
private val BASEPATH = s"/home/$USER"
private val FILE_NAME = "testfile"
// Set up the source system connection
private val SOURCE_HOSTNAME = "host1"
private val sourceSettings = SftpSettings.apply(host = InetAddress.getByName(SOURCE_HOSTNAME))
.withCredentials(CREDENTIALS)
.withPort(22)
private val sourceClient = new SSHClient(new DefaultConfig)
private val configuredSourceClient = Sftp(sourceClient)
// Set up the destination system connection
private val DEST_HOSTNAME = "host2"
private val destSettings = SftpSettings.apply(host = InetAddress.getByName(DEST_HOSTNAME))
.withCredentials(CREDENTIALS)
.withPort(22)
private val destClient = new SSHClient(new DefaultConfig)
private val configuredDestClient = Sftp(destClient)
/**
* Execute the stream from host1 to host2
*/
def doTransfer(): Unit = {
val source = configuredSourceClient.fromPath(s"$BASEPATH/$FILE_NAME", sourceSettings)
val sink = configuredDestClient.toPath(s"$BASEPATH/$FILE_NAME", destSettings)
val runnable = source.toMat(sink)(Keep.right).run()
}
}
我在 new StreamingSftpTransport.doTransfer()
的单元测试中调用了它,但它从未尝试连接。我做错了什么?
正如 artur 在对我的问题的评论中所建议的那样,我并没有阻止未来,因此 JVM 在建立连接之前就退出了。
添加以下行允许建立连接
Await.result(runnable, 180 seconds)
PS:不要在生产中这样做:)
我是 Alpakka/Akka Streams 的新手,我正在尝试设置一个流,在其中我在两个 SFTP 服务器之间传输数据,我的系统在中间,这是代码。
import akka.actor.ActorSystem
import akka.stream.ActorMaterializer
import akka.stream.alpakka.ftp.scaladsl.Sftp
import akka.stream.alpakka.ftp.{FtpCredentials, SftpSettings}
import akka.stream.scaladsl.Keep
import net.schmizz.sshj.{DefaultConfig, SSHClient}
import java.net.InetAddress
class StreamingSftpTransport {
implicit val system: ActorSystem = ActorSystem("dr-service")
implicit val materializer: ActorMaterializer = ActorMaterializer()
private val PORT = 22
private val USER = "testsftp"
private val CREDENTIALS = FtpCredentials.create(USER, "t3st123")
private val BASEPATH = s"/home/$USER"
private val FILE_NAME = "testfile"
// Set up the source system connection
private val SOURCE_HOSTNAME = "host1"
private val sourceSettings = SftpSettings.apply(host = InetAddress.getByName(SOURCE_HOSTNAME))
.withCredentials(CREDENTIALS)
.withPort(22)
private val sourceClient = new SSHClient(new DefaultConfig)
private val configuredSourceClient = Sftp(sourceClient)
// Set up the destination system connection
private val DEST_HOSTNAME = "host2"
private val destSettings = SftpSettings.apply(host = InetAddress.getByName(DEST_HOSTNAME))
.withCredentials(CREDENTIALS)
.withPort(22)
private val destClient = new SSHClient(new DefaultConfig)
private val configuredDestClient = Sftp(destClient)
/**
* Execute the stream from host1 to host2
*/
def doTransfer(): Unit = {
val source = configuredSourceClient.fromPath(s"$BASEPATH/$FILE_NAME", sourceSettings)
val sink = configuredDestClient.toPath(s"$BASEPATH/$FILE_NAME", destSettings)
val runnable = source.toMat(sink)(Keep.right).run()
}
}
我在 new StreamingSftpTransport.doTransfer()
的单元测试中调用了它,但它从未尝试连接。我做错了什么?
正如 artur 在对我的问题的评论中所建议的那样,我并没有阻止未来,因此 JVM 在建立连接之前就退出了。
添加以下行允许建立连接
Await.result(runnable, 180 seconds)
PS:不要在生产中这样做:)