Why does my Alpakka SFTP connector never connect?

286 views Asked by At

I'm new to Alpakka/Akka Streams and I'm trying to set up a stream where I stream data between two SFTP servers with my system in the middle, here's the code.

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()
  }
}

I've called this from a unit test with new StreamingSftpTransport.doTransfer() but it never attempts to connect. What am I doing wrong?

1

There are 1 answers

0
conorgriffin On BEST ANSWER

As suggested by artur in the comment on my question, I wasn't blocking on the future so the JVM was exiting before the connection could be established.

Adding the following line allowed the connections to be established

Await.result(runnable, 180 seconds)

PS: Don't do this in production :)