Embedding Jetty in your application

The era of deploying the application to the web server is over. There are many disadvantages by using the war file to an application server, and microservices is the term to help the enterprises can manage its services more efficiency. When designing the MyCollab product, we have several separated services, and we may add more in the future:

  • Web application
  • API endpoints
  • Billing Integration
  • Scheduler
  • Caching
  • Reporting
  • Application monitoring

These services could plug into the system, but their removal out of the MyCollab without making MyCollab functionalities broken. One example is the caching is only presented in the Premium edition, but the lacking does not make the Community edition stop working. Our microservices architect designs to run as the standalone application not a web application in the web container.

Jetty has a slogan, “Don’t deploy your application in Jetty, deploy Jetty in your application!“. It matches with our vision about the web container should be. We develop a program, and it has the web connector to serve all HTTP requests. That leads us to learn how to embed Jetty in our application.

Running a Jetty server

Creating a running Jetty server is easy

You can add the custom settings to your server such as timeout, packet size, host, etc

If you run the command, the console will show Jetty is running on the port 8080. However, the Jetty does not do any useful thing yet.

Adding the servlets to serve HTTP requests

Create a servlet handler and add as many servlets as you want

The servlet1, servlet2 are the HttpServlet instances while the path1, path2 are the paths serving by these servlets. However, you may want to embed the web application instead of declaring every servlet and its paths.

Embedding Web Application with web.xml descriptor

Like the war deployment file, you may want to use the web.xml descriptor instead of using the Jetty API to declare your web configuration. To let it happens, you should keep the layout of your program has the webapp/WEB-INF folder like the standard war application layout.

Then initiate the WebAppContext instance

The baseDir is the path of the webapp folder in your project layout.

WebAppContext instance is a ServletContextHandler and you can add the other Handler to WebAppContext .For example, you may need to ask Gzip support from the response to save your internet bandwidth, and keep the site responses faster

You may want to ask Jetty to use the custom ClassLoader, and let it support Annotation scanner for servlet, or enable JNDI resources, etc

In some specific cases, you may want to ask Jetty seek the additional classes, libraries of your application; The following commands are helpful

With some tries, you can embed the Jetty into your application. Depends on the requirements and the project structure, you should define the jar files and folder classpaths by using the above commands. You can find the complete example and more complex use case at here. Any comment is welcome.

Leave a Reply

Your email address will not be published. Required fields are marked *