我们有一个消息处理服务器,它
- 启动几个线程
- 处理消息
- 与数据库交互等......
现在客户想要一个网络服务服务器在服务器上,他们将能够通过 Web 服务客户端查询消息处理服务器。例如给我今天的所有消息,或者删除带有 id 的消息....
问题是:
- 服务器只是一个标准的j2se应用程序,不在应用程序服务器内部运行,如tomcat或glassfish。
- 要处理 Http 请求,我需要实现 http 服务器吗?
- 我想使用漂亮的 j2ee 注释,例如 @webservice、@webmothod 等...
有没有我可以使用的库或框架
您不需要第三方库即可使用jax-ws /questions/tagged/jax-ws注释。 J2SE 附带jax-ws /questions/tagged/jax-ws,因此所有注释仍然可供您使用。您可以使用以下解决方案实现轻量级结果,但对于任何优化/多线程,都需要您自己来实现:
-
设计一个 SEI(服务端点接口),它基本上是一个带有 Web 服务注释的 java 接口。这不是强制性的,它只是基本 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"; }
}
-
使用公开服务Endpoint
导入 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 http://docs.oracle.com/javaee/5/api/javax/xml/ws/Endpoint.html以支持并发请求。线程并不是我的专长,所以我无法给你指点。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)