I have coded a server that uses Protocol Buffers in Java. A client talks to it using PB. I'd like to migrate the server code to Java EE and take advantage of the containers' built-in features like clustering.
How can I have a service that receives PB messages and interprets them properly, and then gets them handled?
I was thinking of a dedicated type of servlet, but how can it be done?
I'm a Java EE newbie. I'm not familiar enough with Java EE application servers to know if there is a way to make that happen.
P.S. I'm looking for a solution that uses TLS sockets directly. No overhead-causing middleman protocols like HTTP are wel开发者_如何学运维come here.
P.P.S. Open source solutions only please.
If you look at the servlet classes, they suggest that it should be possible to build any kind of servlet that supports any kind of protocol, not just HTTP (you'd think that it should be possible to create your own kind of servlet that extends GenericServlet
, just like HttpServlet
does).
However, in practice this is not possible, because the Java EE server that you'll be running it on most likely only supports standard HTTP servlets. There's no standard way to make Java EE servers support servlets for other kinds of protocols. So trying to write your own protocol-specific servlet is not the way to go.
Sadly, Java EE doesn't offer a standard way to add new protocols to servers.
The one thing it does offer is the ability to open ServerSocket
s via the Java Connector Architecture. JCA is fairly complicated, but fortunately Mr Mark Jeffrey has written a connector called JCA Sockets which provides a simple, generic way to do this. Using it, you can write message-driven beans that are invoked whenever a connection is made to your ServerSocket
, and which have the resulting Socket
passed to them; you can then do whatever you need to decode and handle the protobuf message there.
If you don't fancy doing that, you can just open a ServerSocket
directly. This violates the Java EE rules, but most containers will let you get away with it.
精彩评论