小编典典

没有Web应用程序服务器的Java Web服务

tomcat

我们有一个消息处理服务器,

  • 开始几个线程
  • 处理消息
  • 与数据库等交互…

现在,客户端希望 服务器 上具有 Web服务服务器 ,他们将能够使用Web
Service客户端查询消息处理服务器。例如,给我今天的所有消息,或删除ID为…的消息。

问题是:

  • 该服务器只是一个标准的j2se应用程序,不能在应用程序服务器内部运行,例如tomcat或glassfish。
  • 要处理Http请求,我是否需要实现http服务器?
  • 我想使用漂亮的j2ee注释,例如@ webservice,@ webmothod等…我可以使用任何库或框架吗

阅读 482

收藏
2020-06-16

共1个答案

小编典典

您不需要第三方库即可使用jax-ws批注。J2SE附带jax-ws,因此所有注释仍然可供您使用。您可以使用以下解决方案来获得轻量级的结果,但是对于任何优化/多线程的解决方案,则要自己动手实现:

  1. 设计一个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();

    }

  2. 在称为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”; }
    }

  3. 使用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的实例以支持并发请求。线程并不是我真正的事情,因此我无法为您提供指针。

2020-06-16