开发者

Calling a stand alone java program from a servlet

I have a stand alone java program having 'main method' that runs continuously in the background polling for any requests in a socket.

I also have web application that needs to pass specific data to clients that connect to the socket opened by the stand alone java program. Is there a way for me to directly call this java program from my servlet or is this not possible at all since java code is executed by jam and servlet is managed by servlet container?

If this is not 开发者_如何学编程possible any other suggestions would be very helpful.

Thanks, V


Any solution that lets you communicate between two processes/JVMs will let you communicate between a servlet and a standalone java process--sockets for example. If your standalone process is already accepting socket connections, have it listen for connections from the servlet as well.

Update: Each time you invoke the java command (which servlet containers do), you start a new JVM. Two Java processes on the same machine can communicate like any other two processes can. They could write to files, communicate to a common database, whatever. Being Java processes, you could also use JMX to communicate. There are plenty of other off-the-wall options, too. You're really limited only by your imagination. If I were doing it, I'd probably embed a lightweight HTTP server in the standalone process and use HTTP for communication because it's just simple. Some easily embeddable servers include Simple, Jetty, and Grizzly.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜