問題描述
沒有 HTTP 的 Java 應用服務器 (Java application server without HTTP)
我有一個用 C++/C# 編寫的客戶端軟件和一個數據庫。現在不想讓客戶端直接訪問數據庫,於是想到了在中間放一個應用服務器。這應該從客戶端得到一個簡短的請求,向數據庫請求新數據,進行一些過濾(在 sql 中無法完成),然後將數據返回給客戶端。
我的搜索這種軟件把我帶到了 Glassfish 或 Tomcat,但我理解的問題是,這些軟件總是想用 html/jsp 來談 http。因為無論如何我的大部分數據都是加密的,所以我不需要這樣的純文本協議,並且對只需要字節流的東西完全滿意。另一方面,讓服務器為我處理線程池會很好(不想從頭開始實現所有這些)。<
參考解法
方法 1:
Jetty and Tomcat are so called servlet container and thus primarly targeted at HTTP exchanges. Glassfish is an application server that uses a servlet container as one of its modules. I would stop thinking in that direction ‑ that's all more like web applications and web services ‑ some levels too high what you are asking for.
I think you should more look into sth. like Netty which is merley a "high performance protocol" server. Take a look at the documentation here (even some sort of tutorial there which might fit your use case).
方法 2:
GlassFish is an "enterprise application server", targeting the Java EJB specification. That's surely overdone for your purpose. You can give Tomcat a try. It is a "servlet container", targeting Java Servlet specification. Servlets have one purpose: listening to an incoming URL (request), executing Java code and returning a response, usually over HTTP.
For sure, you may start your own (plain) ServerSocket, for example using a ServletContextListener (which will be started once your application starts). But you should go for a higher protocol to send the data, like Hessian and Burlap, which is implemented in both, Java and C++ and easy to set up.
(by Daniel Schulze、spa、Stefan)