Il s'agit d'un extrait de code.
FtpDownloader.java
ExecutorService pool = Executors.newFixedThreadPool(5);
for (FTPFile file : files) {
if (!file.isFile()) continue;
pool.submit(new FtpFileDownloader(file));
}
FTPFileDownloader.java
public class FtpFileDownloader implements Runnable{
static Logger logger = LoggerFactory.getLogger(FtpFileDownloader.class);
private FTPFile file;
public FtpFileDownloader(FTPFile file) {
this.file = file;
}
private OutputStream outputStream;
@Override
public void run() {
try{
long start = System.currentTimeMillis();
String fileName = file.getName();
logger.info("File is {}", fileName);
outputStream = new BufferedOutputStream(new FileOutputStream("/home/user/Downloads/" + "FtpDownloads" + "/" + fileName));
//get the file from the remote system
ftpClient.retrieveFile(fileName, outputStream);
showServerReply(ftpClient);
logger.info("[{}ms, {} processing finished.]",System.currentTimeMillis()-start,fileName);
}catch (Exception e){
logger.info("FtpFileDownloader expection");
e.printStackTrace();
}finally {
try {
//close output stream
outputStream.close();
} catch (IOException e) {
logger.info("Io exception happened");
e.printStackTrace();
}
}
J'ai créé un pool de threads fixes de taille 5. Ainsi, après avoir téléchargé 5 fichiers du serveur par chaque serveur individuel
Le fil ne se déconnecte pas du serveur même après le téléchargement de son fichier et attend que le serveur FTP se déconnecte.
2022-03-01 15:49:33.584 INFO 10931 --- [pool-1-thread-5] t.a.f.listener.ftp.FtpFileDownloader : File is mail-send-winforms.png
2022-03-01 15:49:33.587 INFO 10931 --- [pool-1-thread-4] t.a.f.listener.ftp.FtpFileDownloader : File is mail-editor.png
2022-03-01 15:50:33.769 INFO 10931 --- [pool-1-thread-1] t.a.f.listener.ftp.FtpFileDownloader : FtpFileDownloader expection
2022-03-01 15:50:33.771 INFO 10931 --- [pool-1-thread-1] t.a.f.listener.ftp.FtpFileDownloader : File is mime-explorer.png
java.net.SocketTimeoutException: Connect timed out
at java.base/sun.nio.ch.NioSocketImpl.timedFinishConnect(NioSocketImpl.java:546)
at java.base/sun.nio.ch.NioSocketImpl.connect(NioSocketImpl.java:597)
at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:327)
at java.base/java.net.Socket.connect(Socket.java:633)
at org.apache.commons.net.ftp.FTPClient._openDataConnection_(FTPClient.java:866)
at org.apache.commons.net.ftp.FTPClient._retrieveFile(FTPClient.java:971)
at org.apache.commons.net.ftp.FTPClient.retrieveFile(FTPClient.java:3308)
at tech.adoptnet.ftppractice.listener.ftp.FtpFileDownloader.run(FtpFileDownloader.java:35)
at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:539)
at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635)
at java.base/java.lang.Thread.run(Thread.java:833)
après avoir téléchargé les fichiers, chaque thread attend la fin de la connexion et va ensuite télécharger d'autres fichiers.
Comment faire en sorte que le fil se réutilise sans que la connexion ne soit interrompue ?