2015-12-17 90 views
1

我使用Apache Mina sshd-core-1.0.0啓動SFTP守護進程。該程序在sshd.start()之後退出。沒有錯誤。但是如果我使用sshd-core-0.0.14,服務器啓動就好了,我可以啓動一個SFTP會話。我錯過了1.0.0的東西嗎?Apache Mina SSHD 1.0服務器立即退出

代碼1.0.0片斷(不工作)

public static void main(String[] args) throws IOException { 
    SshServer sshd = SshServer.setUpDefaultServer(); 
    sshd.setPort(2222); 
    sshd.setKeyPairProvider(new SimpleGeneratorHostKeyProvider(new File ("hostkey.ser"))) 
    sshd.setPasswordAuthenticator(new AuthenticatorImpl()); 
    sshd.start(); 
} 

代碼段與0.0.14(作品)

public static void main(String[] args) throws IOException { 
    SshServer sshd = SshServer.setUpDefaultServer(); 
    sshd.setPort(2222); 
    sshd.setKeyPairProvider(new SimpleGeneratorHostKeyProvider("hostkey.ser")); 
    sshd.setPasswordAuthenticator(new AuthenticatorImpl()); 
    sshd.start(); 
} 

以下是輸出1.0.0運行時。代碼開始正常,但在sshd.start()聲明後終止。

2015-12-16 19:57:38,510 DEBUG SFTPServer.main(SFTPServer.java:26) message 
2015-12-16 19:57:38,767 INFO org.apache.sshd.common.util.SecurityUtils$BouncyCastleRegistration.call(SecurityUtils.java:145) Trying to register BouncyCastle as a JCE provider 
2015-12-16 19:57:39,076 INFO org.apache.sshd.common.util.SecurityUtils$BouncyCastleRegistration.call(SecurityUtils.java:149) Registration succeeded 
2015-12-16 19:57:39,105 DEBUG org.apache.sshd.common.io.nio2.Nio2Acceptor.bind(Nio2Acceptor.java:57) Binding Nio2Acceptor to address 0.0.0.0/0.0.0.0:2222 
2015-12-16 19:57:39,114 INFO SFTPServer.main(SFTPServer.java:32) Started 

回答

1

SshServer.Start只開始監聽傳入端口。它不會阻止。因此main之後立即終止。這在0.0.14中應該沒有什麼不同,儘管我無法嘗試。

您必須明確等待main以保持服務器正常運行。

查看如何SshServer.main實現(無論是在0.0.14和1.0.0):

public static void main(String[] args) throws Exception { 

    ... 

    SshServer sshd = SshServer.setUpDefaultServer(); 

    ... 

    sshd.start(); 

    Thread.sleep(Long.MAX_VALUE); 
} 
+0

謝謝你的作品。 – Rocky

+0

該解決方案徹底打破了您可能希望在應用程序上運行的任何集成測試。 – christopher