Question

Using Apache Commons FTPClient in a Scala application works as expected on my local machine, but always times out when running on Heroku. Relevant code:

val ftp = new FTPClient
ftp.connect(hostname)
val success = ftp.login(username, pw)
if (success) {
  ftp.changeWorkingDirectory(path)
  //a logging statement here WILL print
  val engine = ftp.initiateListParsing(ftp.printWorkingDirectory)
  //a logging statement here will NOT print
  while (engine.hasNext) {
    val files = engine.getNext(5)
    //do stuff with files
  }
}

By adding some loggings I've confirmed the client is successfully connecting, logging in, and changing the directory. But stops when trying to begin retrieving files (either via the above paged access or unpaged with ftp.listFiles) and throws a connection time out exception (after about 15 minutes).

Any reason the above code works fine locally but not on Heroku?

Was it helpful?

Solution

Turns out FTP has two modes, active and passive, and Apache-Commons' FTPClient defaults to active. Heroku's firewall presumably doesn't allow active FTP (which is why this was functioning properly locally but not once deployed) - changing to passive mode by adding ftp.enterLocalPassiveMode() resolved the issue.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top