我正在使用Apache SSHD编写SFTP服务。服务器将使用Apache commons daemon procrun安装在Windows上。我想使用静态方法start()和stop()来处理服务启动/停止,类似于Tomcat的工作方式。但是,我不明白这是如何工作的。我认为我的困惑在于了解procrun如何启动和停止服务。从我的理解,procrun启动一个新的java进程与args"开始" /"停止"? Java程序的新实例如何处理已经运行的Java实例?
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.sshd.common.NamedFactory;
import org.apache.sshd.server.Command;
import org.apache.sshd.server.SshServer;
import org.apache.sshd.server.config.keys.AuthorizedKeysAuthenticator;
import org.apache.sshd.server.keyprovider.SimpleGeneratorHostKeyProvider;
import org.apache.sshd.server.subsystem.sftp.SftpSubsystemFactory;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class SftpServer {
private static final Logger logger = LogManager.getLogger(SftpServer.class);
public static void main(String[] args){
if (args.length == 1) {
if (args[0] == "start") {
try{
start();
} catch (IOException e) {
logger.error(e);
}
} else if (args[0] == "stop") {
stop();
} else {
System.err.println("Unkown command. Use start/stop to conrol the service.");
}
} else {
System.err.println("Use start/stop to control the service.");
}
}
public static void start() throws IOException {
SshServer sshd = SshServer.setUpDefaultServer();
sshd.setPublickeyAuthenticator(new AuthorizedKeysAuthenticator(new File("conf/users.keys")));
sshd.setKeyPairProvider(new SimpleGeneratorHostKeyProvider(new File("conf/hostkey.ser")));
sshd.setPort(22);
List<NamedFactory<Command>> subSystemFactories = new ArrayList<>();
subSystemFactories.add(new SftpSubsystemFactory());
sshd.setSubsystemFactories(subSystemFactories);
sshd.start();
while(true){
}
}
public static void stop(){
// What to do here?
}
}