开发者

How to make Tomcat quick in loading changes to make Java web development fast

My app uses Struts2 MVC, Spring JDBC Template with Eclipse IDE and Tomcat. Whenever I change a class code, I have to restart the Tomcat, which waste about 15-20 seconds every time.

I have configured hot-deployment i.e. on-java-class-change, context reloaded automatically, but it often fails, and I have to restart the Tomcat.

What I wish is:

  1. When I make a change to JSP, it should be auto loaded, instead of on-1st-request.

  2. When a java class is changed and build, only that class should be loaded, without restarting Tomcat, even whole context should not restart or reload.

If someone have used Jetty, please share - does Jetty provide better solution to these issues.

In PHP, you make a change开发者_如何学C in script, and access that page, its ready. I just wish it in Java.


We are using embedded Jetty at work and it's a blast. See http://wiki.eclipse.org/Jetty/Tutorial/Embedding_Jetty for a guide on embedding Jetty.

The startup time on the Jetty side is extremely fast (1-2 seconds). In our case though spring takes a lot of time to initialize still.

It is easy to configure Jetty to reload jsps automatically just point it to a directory and not war file and jsps will reload automatically, something like this:

public class OneWebAppUnassembled
{
    public static void main(String[] args) throws Exception
    {
        Server server = new Server(8080);

        WebAppContext context = new WebAppContext();
        context.setDescriptor(webapp+"/WEB-INF/web.xml");
        context.setResourceBase("../test-jetty-webapp/src/main/webapp");
        context.setContextPath("/");
        context.setParentLoaderPriority(true);

        server.setHandler(context);

        server.start();
        server.join();
    }
}

Class reloading is possible using hot swap, I don't know if there are ready solutions for when hot swap doesn't work.


Have a look at JRebel. You have to pay for it, but it may be worth it in the long run for the time saved! :)


Tapestry5 has live class and template reloading, works in Tomcat and Jetty. This makes development much faster.

As to Jetty over Tomcat, I generally use Jetty for development these days. It is super fast to start, and very configurable. You can write your own simple server, and run it in eclipse, like so:

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.webapp.WebAppContext;

public class JettyServer {
    public static void main(String[] args) throws Exception {
        run(8080, "/path/to/eclipse/project/WebContent", "/WEB-INF/web.xml");
    }
    private static void run(int port, String resourceBase, String descriptor) throws Exception {
        Server server = new Server(port);

        WebAppContext context = new WebAppContext();
        context.setResourceBase(resourceBase);
        context.setDescriptor(resourceBase+descriptor);
        context.setContextPath("/");
        context.setParentLoaderPriority(true);
        server.setHandler(context);
        server.start();
        server.join();
    }
}


Check out SpringSource Tool Suite. I started using it a couple of months ago.

It comes with a pre-configured SpringSource tc Server (development version). Basically when I am working on a web application I simply drag it from the Project Explorer onto the SpringSource tc Server and continue developing. The code including Java class changes and the JSP changes are reloaded on the fly. So any changes you make can be viewed pretty much instantly.

The other thing I do is with Jetty, similar to what Yuval mentioned however my configuration is in Maven 2's pom.xml and looks something like

<plugin>
    <groupId>org.mortbay.jetty</groupId>
    <artifactId>maven-jetty-plugin</artifactId>
    <configuration>
        <contextPath>/tas</contextPath>
        <tmpDir>/tmp</tmpDir>
        <jettyEnvXml>${basedir}/jetty-env.xml</jettyEnvXml>
        <systemProperties>
            <systemProperty>
                <key> org.mortbay.jetty.webapp.parentLoaderPriority </key>
                <value>true</value>
            </systemProperty>
        </systemProperties>
    </configuration>
</plugin>

I do however prefer to do manual reloading with jetty so I usually add

<reload>manual</reload>

into the <configuration> element. The manual reload only applies to changes that I make in Java files. So after a number of substantial changes and successful pass of the test cases then I hit enter in the Jetty console and the whole thing is reloaded. (I like the Ctrl-S shortcut too much and hence hit it after every second line, but I don't want Jetty to reload after every second line of code I write).

Also keep in mind that the manual reload applies only to Java class changes and is required only when you change the Java class or perhaps an XML configuration. If all you have changed is the JSP then you should not need to reload the change is picked up automatically and you can hit refresh in the browser to see the JSP change without the manual reload.

Hope this helps!


I used Sysdeo Tomcat Laucher plugin of Eclipse with great success http://www.eclipsetotale.com/tomcatPlugin.html. The DevLoader thing is amazing. The only problem that I have with it is that it cannot reload a class which introduces new properties or methods. The author of the plugin is very responsive as well.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜