没有 Web 应用程序服务器的 Java Web 服务 [英] Java web service without a web application server

查看:39
本文介绍了没有 Web 应用程序服务器的 Java Web 服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

We have a message processing server, which

  • 开始一些话题
  • 处理消息
  • 与数据库等交互......

现在客户端想要在服务器上有一个Web 服务服务器,他们将能够通过一个 Web 服务客户端查询消息处理服务器.例如给我今天的所有消息,或者删除带有id的消息......

now the client want to have a web service server on the server, they will be able to querying the message processing server, with a web service client. e.g. give me all the messages for today, or delete the message with id....

问题是:

  • 服务器只是一个标准的 j2se 应用程序,不在应用程序服务器中运行,如 tomcat 或 glassfish.
  • 要处理一个 Http 请求,我需要实现一个 http 服务器吗?
  • 我想使用漂亮的 j2ee 注释,例如@webservice、@webmothod 等...有没有我可以使用的库或框架

推荐答案

你不需要第三方库来使用 注释.J2SE 附带 ,因此,您仍然可以使用所有注释.您可以使用以下解决方案获得轻量级的结果,但对于任何优化/多线程的内容,都需要您自己来实现:

You don't need a third party library to use jax-ws annotations. J2SE ships with jax-ws, so all the annotations are still available to you. You can achieve lightweight results with the following solution, but for anything optimized/multi-threaded, it's on your own head to implement:

  1. 设计一个SEI,服务端点接口,它基本上是一个带有web-service注解的java接口.这不是强制性的,它只是来自基本 OOP 的良好设计的一点.

  1. Design a SEI, service endpoint interface, which is basically a java interface with web-service annotations. This is not mandatory, it's just a point of good design from basic 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.

  • Implement the SEI in a java class called a SIB service implementation bean.

    @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;

  • Expose the service using an Endpoint import 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 的实例支持并发请求.线程不是我真正擅长的,所以我无法给你指点.

    The snippets above, like I said, are pretty basic, and will perform poorly in production. You'll need to work out a threading model for requests. The endpoint API accepts an instance of Executor to support concurrent requests. Threading's not really my thing, so I'm unable to give you pointers.

    这篇关于没有 Web 应用程序服务器的 Java Web 服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆