Embedded Tomcat Cluster
- by ThreaT
Can someone please explain with an example how an Embedded Tomcat Cluster works. Would a load balancer be necessary?
Since we're using embedded tomcat, how would two separate jar files (each a standalone web application with their own embedded tomcat instance) know where eachother are and let eachother know their status, etc?
Here is the code I have so far which is just a regular embedded tomcat without any clustering:
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.Writer;
public class Main {
public static void main(String[] args)
throws LifecycleException, InterruptedException, ServletException {
Tomcat tomcat = new Tomcat();
tomcat.setPort(8080);
Context ctx = tomcat.addContext("/", new File(".").getAbsolutePath());
Tomcat.addServlet(ctx, "hello", new HttpServlet() {
protected void service(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
Writer w = resp.getWriter();
w.write("Hello, World!");
w.flush();
}
});
ctx.addServletMapping("/*", "hello");
tomcat.start();
tomcat.getServer().await();
}
}
Source: java dzone