views:

22

answers:

1

Imagine you have a standard java web-app using plain servlets, or SpringMVC, or whatever. You also want (for whatever reason) a way to talk to the server not using HTTP - I'll use direct sockets as it's the easiest example I can think of.

Writing a web-app is easy, you have servlets acting as entrypoints. Writing a java app which monitors ports is also pretty easy. But what about one that does both? Is it allowed without hacking? And if it turns out we agree this is a Bad Idea, what's a better architecture? Note that one of the motivations behind this is performance... we could easily have two separate apps sharing a DB but prefer to avoid using the DB as a communication tool, when information could be cached in memory much more efficiently.

+2  A: 

So I assume there is a Java EE container in play, like Tomcat. If you want it to listen on some other port besides or in addition to 80, sure. You would make a new Connector in server.xml, in Tomcat's case, and specify whatever port you like.

If you want this connector to speak a custom protocol, you need to implement and register your own customer Connector. I've not done it, but seems straightforward.

If you're answering substantially the same requests via two protocols, it makes sense to use one server with different endpoints. I imagine it makes it far easier to share all that common logic.

Even if you want to run a separate app, it still probably pays to go this way, since you'll be leveraging the container's management of connections and such.

Sean Owen
+1 The most well known non-http connector is probably AJP_13, the protocol to connect Apache and Tomcat.
ewernli
I'm not familiar with the concept of Connectors, do you have a link to an example, or some sample code? In my case, the most likely use case _would_ be a client sending a direct socket message to the server.
John
or another example might be that a Flex client wants to talk to the server using BlazeDS, if that can fit into a servlet system... not something I've used before to have a clear idea if it might be possible.
John