I have a big database controller which is written in Java. The controller reads information from the database, and interprets it into data structures which are then displayed in a CLI.
Java was chosen because writing code in it is fast and easy. Now I want to create an RPC server on top of the controller (XML-RPC or JSON-RPC for future AJAX calls), but it looks like I need a servlet container for my RPC service. I am confused because last year when I needed this kind of capability for another project in python, it took me less than 5 minutes to create the same functionality using the SimpleXMLRPCServer
The same ease of creation applies also to C# as far as I recall.
But in Java the story is different; now I need a servlet and therefore a servlet container (i.e Tomcat, Jetty) which means I need to install and maintain web servers. From what I can tell, JSON-RPC requires Spring
framework in order to work.
I have already spent around two hours in learning the design and sort-of how Tomcat works without writing even one line of code.
I searched the web and found out that I have standalone options: I can use this library, but it doesn’t seem maintained and it is also somewhat complex (I’m looking for something with decorators / annotations).
The other option I found is to use the so called “embedded” jetty and then trying to set it and configure it by code which also seems a tedious task.
Why isn’t there a standalone mechanism for such a popular interface? Am I missing out something here?
4
Incorrect assumptions
Your assumption that you need servlets and a server is mis-guided and incorrect. there are plenty of embeddable HTTP servers in Java, some are single classes, they aren’t full featured servers, but an HTTP server is just a TCP/IP Socket based protocol. The Servlet API is just a nice abstraction away from the details.
You don’t want to do RPC, you want to do REST
You will need a minimal Servlet container to use something that will accelerate your development like RESTEasy.
2
The enterprise features of Java (as in those in JEE) are not implemented by Oracle/Sun and are instead contained within the Java Enterprise Edition specification.
As a result there are numerous implementations of application servers – Tomcat, Jetty, WebSphere, etc, all of which implement different parts of the JEE specification.
Because you are using enterprise Java features, you need an application server – hence your present situation.
Jetty is a very lightweight server and you might have some luck there if you’re having trouble with Tomcat.
I understand where you’re coming from, doing this sort of thing in .NET is trivially easy, Java needs a little bit more work to get there but the result in the end is the same.
It is possible to simply start a Jetty instance from code, without a web.xml file or any kind of servlet configuration.
That’s possible by implementing a custom Handler and passing it to the server instance, thus completely bypassing the servlet API.
HttpServletRequest and HttpServletResponse are still used, so everything is extremely similar, except it feels a lot more like doing a similar thing in python ^^
The downside is that it’s way less common to do this, so google may not be entirely as helpful.
On the other hand, the differences from using the servlet API are really small, so when something goes profoundly wrong, you will likely end up with very similar exception messages and stack traces, so you’re not entirely on your own in case things should one day decide to take a little side track downhill ^^
You want to create a server and you’re surprised you have to install and maintain server software?
The “concept or vision” that you’re running up against is the idea that if you’re running a server, you really should use something solid and mature that can be monitored with standard tools, instead of just running some code and starting to roll your own admin tools as soon as you want to do anything more with it.
Besides, it’s not as if setting up Tomcat (let alone Jetty) were all that complex. You don’t have to understand all its capabilities to use it for a simple project like this.
3