Embeddable Java HTTP Servers - java

There seems to be multiple extremes when supporting embeddable Java HTTP servers. I have seen minimalist approaches such as NanoHTTPD and leveraging the com.sun.net.httpserver package to attempting to embed Jetty and Tomcat. The ideal embeddable HTTP server would be implemented such that it could be launched via Executor and come with Servlet/JSP support but otherwise should allow for using the logger of its parent and allow for integrating its JMX hooks.
Likewise, it should also be modular enough where you can have the option of NOT including certain pieces of functionality. Has anyone ran across any Java-based HTTP servers with this characteristic?

jetty:
JMX integration
configurable logging (both for request and debugging)
JSP 2.0 and 2.1

I think grizzly is an ideal solution for you. https://grizzly.dev.java.net/ . Out of box it's simply a NIO server, but there are different packages with support for HTTP, Servlets\JSP and etc.

I couldn't afford the overhead (in size) of even a minimal Jetty, so I wrote my own framework - I can't remember if Jetty had OSS licensing encumberances which ruled it out for us. But, at the time, I was highly impressed with Jetty's design and highly modular architecture - it was easily the best option I explored (back in 2001 or so) and I recommend it.

The question is how much functionality you need. The small ones typically only supports http requests, where you may want servlet API, https communications, web applications, etc. Jetty is very highly modular so you can put together what you want.

Related

Which one is the better configuration: Tomcat only OR Apache Server + Tomcat?

I have a VPS where I want to install a web application for production.
Currently, it works on Tomcat 6 connected through Apache Server using AJP module.
Every thing works ok, but I want to know if it is a good idea to configure Tomcat as the main web server due we gonna run only JSP applications.
FYI: the server must handle https requests.
Thanks in advance.
Misinformation
Years ago there was much misinformation against using Tomcat directly as a web server, but in fact Tomcat works very well as such.
I've used nearly every version of Tomcat on various projects, though all were relatively low volume. Never let me down – well, the WebDAV module never worked right. But other than that Tomcat is fast and reliable both for static content as well as dynamic servlet work.
httpd
Apache HTTP Server ("httpd") is of course very advanced and full of features. If you need those features, then use httpd. If you have very high volume traffic, then use httpd. If you have huge amounts of static access and want to relieve Tomcat of that duty thereby letting Tomcat focus its performance on your servlets, then use httpd. If you want to keep a front-facing web site running while take Tomcat up and down, use httpd. But for most relatively simple web sites or simple web apps, Tomcat suffices.
I suggest you try Tomcat alone and see how it goes. Start with your development and testing systems. Review your config files for httpd to see if all its features can be reproduced using Tomcat features. If that all works, then do some load-testing. Either hack some load-tests of your own, or try any of the many load-test frameworks.
While you can hot-deploy webapps to Tomcat, that was not always recommended in production (I'm not sure about current versions). Plan how to handle taking down Tomcat to deploy updated apps and to perform maintenance chores.
Recent Tomcat
You say you are using Tomcat 6. You may want to consider moving to Tomcat 7 or 8, but review the release notes.
Jetty
Also consider Jetty, the most direct competitor/alternative to Tomcat. Jetty is much akin to Tomcat in its purpose, scope of features, and great reputation.
If you're using SSL you will get the following benefits by front-ending with Apache HTTPD:
Ability to specify cipher suites in order of preference, which is practically mandatory if you want case-hardened SSL, and impossible with Tomcat alone.
Ability to request or require client certificates on a per-location basis instead of globally, which is Tomcat's only mechanism.
There are many other benefits as well, such as:
the ability to load-balance between multiple Tomcat instances, which alone is probably enough of a reason to do it
more control over what's logged and where the log files go
more tools to defend against attacks of various kinds.
I used standalone Tomcats for years, but having made the switch I would never go back even for a clean-sheet project.
I don't think either is better, it depends on your requirements. In environments where you may have to address security or logging requirements horizontally across heterogeneous backends (IIS, Tomcat, etc), Apache is your friend (or an expensive load balancer).
Assuming you don't have these requirements, I don't know of any advantage in using Apache for Apache sake.
SSL is easily configured in Tomcat and performance these days is likely to be on par with Apache and can be improved with APR.

Java Rest framework + server to expose service to Ruby on Rails

I have a very small Java codebase that I would like to expose as a REST service that responds in JSON format (to be used by a Ruby on Rails project).
I have only notions about Java web servers / web frameworks. What would be:
a. a suitable Java MVC and
b. a java server
to ease my deployment? I am interested in:
easy to build(wrap) the solution
easy to maintain / configure the server
stable (not experimental)
We use CXF extensively. It is very simple standard JAX-RS annotation based. There is a good tutorial to set it up quickly. It works with or without Spring, but easier with spring, this is my preference. The other option would be Spring REST it is very similar to Spring MVC which is very easy if you know spring already, but on the other hand it is not JAX-RS based.
you might want to give Grails a try - it:
shares some similarities with RoR
comes bundled with an application server so it's very easy to get up and running
allows very simple deployment to a Java application server (e.g. Tomcat, Jetty)
has built-in support for JSON
has recent stable releases
is under active development
http://www.restlet.org/
http://static.springsource.org/spring/docs/current/spring-framework-reference/html/mvc.html
https://github.com/dhanji/sitebricks (my favorit)
For the server part of the problem, build your code so that it is a servlet and then deploy in a container like Tomcat or Jetty. (There are many others too, but Tomcat and Jetty work very well and are pretty easy to use.)
For the framework, I'd recommend CXF (I've had good success with it in my project) as it keeps the amount of configuration you need to write small (typically just add annotations to mark which methods are to be exposed; the annotations are standardized as JAX-RS), but many others are valid choices too.

Benefits of Tomcat (or equivalent) for a simple service

I'll need to develop a Java service that is simple because:
It only communicates via a TCP socket, no HTTP.
It runs on a dedicated server (there are no other services except the basic SSH and such)
Should I make this a standalone service (maybe in something like Java Service Wrapper) or make it run in a container like Tomcat? What are the benefits and detriments of both?
If you aren't working with HTTP, you will have to build your own connectors for Tomcat. When I've written these types of applications, I've just written them as standard Java applications. On Windows machines, I use a service wrapper that allows them to be part of the Windows startup process. On non-windows machines, you just need to add a start up script.
Using a container (regardless which) buys you that all the details about starting, stopping, scaling, logging etc, which you have to do yourself otherwise, and it is always harder than you think (at least when you reach production).
Especially the scalability is something you need to consider already now. Later it will be much harder to change your mind.
So, if somebody already wrote most of what you need, then use that.
Tomcat doesn't sound like a good choice for me in your situation. AFAIK it's primarily made for Servlets and JSPs, and you have neither. You also don't need to deploy multiple applications on your app. server etc. (so no benefit from ".war").
If you need dependency injection, connection pooling, logging, network programming framework etc., there are a lot of good solutions out there and they don't need tomcat.
For example, in my case I went for a standalone app. that used Spring, Hibernate, Netty, Apache Commons DBCP, Log4j etc. These can be easily setup, and this way you have a lot more freedom.
Should you need a HTTP server, maybe embedding Jetty is another option. With this option too, you have more control over the app. and this can potentially simplify your implementation compared to using a tomcat container.
Tomcat doesn't really buy you much if you don't use HTTP.
However, I was forced to move a non-HTTP server to Tomcat for following reasons,
We need some simple web pages to display the status/stats of the server so I need a web server. Java 6 comes with a simple HTTP server but Tomcat is more robust.
Our operation tools are geared to run Tomcat only and standalone app just falls off radar in their monitoring system.
We use DBCP for database pooling and everyone seems more comfortable to use it under Tomcat.
The memory foot-print of Tomcat (a few MBs) is not an issue for us so we haven't seen any performance change since moved to Tomcat.
A container can save you from reinventing the wheel in terms of startup, monitoring, logging, configuration, deployment, etc. Also it makes your service more understandable to non-developers.
I wouldn't necessarily go for tomcat, check out glassfish and germonimo as they are more modular, and you can have just the bits the need, and exclude the http server.
We faced a similar decision a while back, and some parts of the system ended up being jsw based, and the others as .war files. The .war option is simpler (well more standard for sure) to build and configure.

A Java HTTP Server

I want to implement a Java HTTP server locally, I mean the server computer would be in my control. I'm expecting not more than 20 clients to send requests to it.
I was wondering how to go about it:
Should I use a J2EE servlet container, like Apache Tomcat?
Could I use J2SE classes and just build it using them?
Are there any existing alternatives?
What does your experience suggest?
There's a simple HTTP server embedded in the Sun 1.6 JRE. It's not JavaEE or servlet-compliant, it's very lightweight, but it might be good enough for your requirements. No need to download any 3rd party stuff if this is all you need.
The javadocs, rather bizarrely, are out on their own, here.
Embed Jetty in your application. Aside from performing quite well, it is delightfully easy to use and configure
You've got many options, not the least of which are Jetty, Grizzly, and TTiny.
I would strongly urge against writing your own web server, unless you've got time to kill and want to spend it writing things that are already available to you for free.
Seriously, reuse an existing solution. Why the hell are you even thinking rolling your own?
Now, 1. I don't understand your question as being about embedding a container. 2. You mentioned long polling several time. So I'd suggest to use GlassFish v3 / Grizzly (because there are many samples, e.g. have a look at the Dead Simple Comet Example on Glassfish v3 / Grizzly).
If you don't want to rely on the way a container implemented Comet support, use atmosphere and any of the container mentioned on the web site:
Atmosphere is a POJO based framework using Inversion of Control (IoC) to bring push/Comet to the masses! Finally a framework which can run on any Java based Web Server, including Google App Engine, Tomcat, Jetty, GlassFish, Weblogic, Grizzly, JBossWeb and JBoss, Resin, etc. without having to wait for Servlet 3.0 Async support or without the needs to learn how Comet support has been differently implemented by all those Containers.
If this is not a concern, just stick with the suggested option (GlassFish v3 / Grizzly).
For a concrete and recent comparison between Comet server implementation, checkout this awesome Comet Maturity comparison grid view (source: Comet Gazing: Maturity). It might help you to make your final choice... or not :)
I guess the biggest question is: why do you want to build this?
If it is for the purpose of personal development, I'd stick to whatever standard libraries come with your JDK and build it on top of that.
If on the other hand you have a particular application that needs a dedicated HTTP server I would try to take one of the open source servlet containers, like Jetty or Tomcat and build on those.
Perhaps look at the list of 26 open source web servers at http://java-source.net/open-source/web-servers.
http://java.sun.com/developer/technicalArticles/Networking/Webserver/WebServercode.html is actual code in a single file implementing a multi threaded webserver. For your requirements, such as they are, this should suffice.
http://java.sun.com/developer/technicalArticles/Networking/Webserver/ is an analysis of the code.
If you will write your own HttpServer you will have to implement all the methods of the HTTP protocol. Tomcat can be easily used locally.
Is it for practice, fun, to implement special requirements or why don't you just embed an existing solution?
Do you really want to build a HTTP server that deals with the protocol directly, or do you just want to write web apps? If all you care about is writing the web apps, then just use Tomcat, or Jetty, or Glassfish, or another server -- it will save you a ton of work.
If you really are interested in writing your own server from scratch, then the best way would be to just use Java SE, and not use any existing server technology.
Ad your 3) option: Try JBoss Netty.
http://fisheye.jboss.org/browse/Netty/trunk/src/main/java/org/jboss/netty/example/http/websocket

Are application servers (with EJBs) the only way for Java EE client/server communication?

Imagine a Java client/server ERP application serving up to 100 concurrent users, both web and swing clients. For persistence we can use Persistence API and Hibernate.
But when it comes to client/server communication, do we really have an alternative to using an AS with EJBs to keep down the programming costs of remote communication?
It seems a very heavyweight solution to throw in EJBs and an application server, just for remoting stuff. There is also the standard way using RMI, but who wants nowadays to code everything on his own...
I know that you'll get a lot of features for free with an AS in addition to the remoting part. And, maybe it's the way to go. But, are there really any other (low programming cost) alternatives to an AS for doing client/server communication for an enterprise application?
Personally I consider Seam to be the ultimate solution to the problem I don't have but that aside, there are lots of options and Spring is used for most of them:
RMI;
Hessian (binary HTTP based protocol);
HTTP Invokers (Java serialization over HTTP);
Web Services; or
Even JMS.
The advantage of the HTTP based remoting methods is that they easily plug into Spring's security models. Plus then you get access to things like interceptors.
You can use light weight remoting frameworks. Spring is having a number of options in this section.
If you ask me one word for both, I will suggest two. Seam or Spring. I myself like Seam, but for lightweight remoting Spring has more viable solutions and support. Again if your AS is JBoss, give a thought to Seam.
An application server is a pretty heavyweight solution. Depending on your requirements, I would try to make sure that you can run on a simple servlet container (like Tomcat). I find remoting much easier with Spring-remoting instead of full EJB. Spring-remoting provides an abstraction on the actual remoting technique used. Hessian has a good reputation as a lightweigth protocol.
Having some sort of a server framework (AS or servlet container) is a good thing as you wont need to think as much about all the low level problems (connection establishment, threading, ...).

Categories

Resources