我们有一个消息处理服务器,它
- 启动几个线程
- 处理消息
- 与数据库交互等......
现在客户端想要在服务器上有一个web服务服务器,他们将能够通过一个web服务客户端查询消息处理服务器。例如给我今天所有的消息,或者删除id为....的消息。
问题是:
- 服务器只是一个标准的 j2se 应用程序,不在应用程序服务器内部运行,如 tomcat 或 glassfish。
- 要处理一个 Http 请求,我需要实现一个 http 服务器吗?
- 我想使用不错的 j2ee 注释,例如 @webservice、@webmothod 等... 有没有我可以使用的库或框架
最佳答案
您不需要第三方库即可使用 jax-ws注释。 J2SE 附带 jax-ws ,因此您仍然可以使用所有注释。您可以使用以下解决方案实现轻量级结果,但对于任何优化/多线程的解决方案,都需要您自己实现:
设计一个 SEI,服务端点接口(interface),它基本上是一个带有网络服务注释的 java 接口(interface)。这不是强制性的,它只是基本 OOP 的良好设计点。
import javax.jws.WebService; import javax.jws.WebMethod; import javax.jws.WebParam; import javax.jws.soap.SOAPBinding; import javax.jws.soap.SOAPBinding.Style; @WebService @SOAPBinding(style = Style.RPC) //this annotation stipulates the style of your ws, document or rpc based. rpc is more straightforward and simpler. And old. public interface MyService{ @WebMethod String getString(); }
在称为 SIB 服务实现 bean 的 java 类中实现 SEI。
@WebService(endpointInterface = "com.yours.wsinterface") //this binds the SEI to the SIB public class MyServiceImpl implements MyService { public String getResult() { return "result"; } }
使用
端点
公开服务 导入 javax.xml.ws.Endpoint;public class MyServiceEndpoint{ public static void main(String[] params){ Endpoint endPoint = EndPoint.create(new MyServiceImpl()); endPoint.publish("http://localhost:9001/myService"); //supply your desired url to the publish method to actually expose the service. } }
上面的代码片段,就像我说的,非常基础,在生产中表现不佳。您需要为请求制定线程模型。端点 API 接受 Executor 的实例支持并发请求。线程不是我的事,所以我无法给你指点。
关于web-services - 没有 Web 应用程序服务器的 Java Web 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12860588/