Estou estudando uma situação de utilizar um servidor embutido dentro da minha aplicação. E nos meus testes, criei um JFrame e coloquei dois botões: em um botão eu inicio o servidor e no outro (teoricamente) deveria para o servidor.
Mas quando eu clico em iniciar, eu não consigo mais apertar no botão de parar nem fechar o programa. É como se a tela travasse.
public class IniciarServer {
private Tomcat tomcat = null;
public IniciarServer() {
tomcat = new Tomcat();
tomcat.setPort(8090);
Context context = tomcat.addContext("/", new File(".").getAbsolutePath());
Tomcat.addServlet(context, "tomcatEmbbed", new HttpServlet() {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
Writer writer = resp.getWriter();
writer.write("<div align='center'>");
writer.write("<h1>JavaC - Java Community</h1>");
writer.write("</div>");
writer.flush();
}
});
context.addServletMapping("/*", "tomcatEmbbed");
try {
tomcat.start();
tomcat.getServer().await();
} catch (LifecycleException ex) {
Logger.getLogger(IniciarServer.class.getName()).log(Level.SEVERE, null, ex);
}
}
public Tomcat getTomcat() {
return tomcat;
}
}
public class PararServer {
private Tomcat tomcat = null;
public PararServer(Tomcat aThis) {
tomcat = aThis;
try {
tomcat.stop();
} catch (LifecycleException ex) {
Logger.getLogger(PararServer.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
Qual seria a melhor forma de iniciar e parar o servidor?