Pages

Tuesday, March 22, 2011

Presentation Slides: Java Bytecode Fundamentals I - Tech Talk for JUG.lv, March 2011


JRebel 4.0 M1 Released

JRebel 4.0-M1 was released. Grab it while it's hot!

The new features include integration with HotSwap, support for anonymous classes and adding new session beans to JBoss 4.2/5.1 and Glassfish 2/3.

For the new features, I would encourage everyone to give it a try! JRebel already provides the integration for JSF (Mojarra), JAX-RS (Jersey), CDI (Weld), and now the support for EJBs is extended! It is possible to create a JavaEE6 app with almost no redeploys already. Things are improving quite fast!

Sunday, March 6, 2011

Embedded Tomcat, The Minimal Version

Tomcat 7 has been improved a lot and along with all the features that it brings, a very nice feature is provided - the API for embedding tomcat into the application. The API was provided in earlier versions of Tomcat but it was quite cumbersome to use.

To to start the embedded version of Tomcat one may need to build the required JARs.


$> svn co https://svn.apache.org/repos/asf/tomcat/trunk tomcat
$> cd tomcat
$> ant embed-jars
$> ls -l output/embed
total 5092
-rw-r--r-- 1 anton None 56802 2011-03-06 17:09 LICENSE
-rw-r--r-- 1 anton None 1194 2011-03-06 17:09 NOTICE
-rw-r--r-- 1 anton None 1690519 2011-03-06 17:09 ecj-3.6.jar
-rw-r--r-- 1 anton None 234625 2011-03-06 17:09 tomcat-dbcp.jar
-rw-r--r-- 1 anton None 2402517 2011-03-06 17:09 tomcat-embed-core.jar
-rw-r--r-- 1 anton None 781989 2011-03-06 17:09 tomcat-embed-jasper.jar
-rw-r--r-- 1 anton None 34106 2011-03-06 17:09 tomcat-embed-logging-juli.jar


The following snippet demonstrates the embedded Tomcat usage with a deployed servlet instance.

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();
  }

}

The only two JARs required are tomcat-embed-core.jar and tomcat-embed-logging-juli.jar. It means that there will be no JSP support and pooling will also be disabled. But that's enough to start a servlet and in most cases that is what you probably need.

Disqus for Code Impossible