Embedded Tomcat Cluster
Posted
by
ThreaT
on Programmers
See other posts from Programmers
or by ThreaT
Published on 2014-08-22T07:29:12Z
Indexed on
2014/08/24
22:32 UTC
Read the original article
Hit count: 255
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
© Programmers or respective owner