简体   繁体   English

如何仅使用Java SE创建支持多个客户端请求的Java Web服务?

[英]How can i create a java web service that supports multiple client requests using only java SE?

I have the task of creating a web service that takes requests from multiple clients. 我的任务是创建一个接受来自多个客户端的请求的Web服务。 I am new to web services so i used this tutorial: 我是Web服务的新手,所以我使用了本教程:

http://www.java-forums.org/blogs/web-service/1145-how-create-java-web-service.html http://www.java-forums.org/blogs/web-service/1145-how-create-java-web-service.html

This is exactly what i am looking for. 这正是我在寻找的东西。 A web service with no java ee. 没有Java EE的Web服务。

It is preferable that is stick with java se, it's a policy that is prefered to be kept. 最好坚持使用Java se,这是首选保留的策略。

Now i would like to go one step further and implement the service so that it processes requests from multiple clients that operate on a shared resource. 现在,我想更进一步并实现服务,以便它处理来自在共享资源上运行的多个客户端的请求。

Ideally i would like something like this: 理想情况下,我想要这样的东西:

 Client client = new Client();
 client.processRequest(string);

And the web service will process the requests in the order they arrive. Web服务将按请求到达的顺序处理请求。 The requests will come in as an request is processed so it will be kept in a stack. 请求将在处理请求时进入,因此将被保留在堆栈中。

The problem is i just on't know how to send the response back to the specific client. 问题是我只是不知道如何将响应发送回特定客户端。 The response will be a string. 响应将是一个字符串。 The only thing i came up with, at least in principle, is to send a object that remembers where it came from but that just seems the web services job. 至少在原则上,我想到的唯一一件事就是发送一个对象,该对象可以记住它的来源,但这似乎只是Web服务的工作。

I have searched the internet but did not find a solution. 我已经搜索了互联网,但没有找到解决方案。

If possible using only SE please help. 如果可能仅使用SE,请帮助。 If you think it is not possible without EE you can say so, but i would very much like an answer using only SE 如果您认为没有EE是不可能的,您可以这样说,但是我非常希望仅使用SE来回答

I think what you are trying to implement is an Asynchronous Webservice. 我认为您要实现的是异步Web服务。 The following link tells you how to implement it in Java SE. 以下链接告诉您如何在Java SE中实现它。

http://java.dzone.com/articles/asynchronous-java-se-web http://java.dzone.com/articles/asynchronous-java-se-web

You can do this using the Endpoint.publish methods in Java SE. 您可以使用Java SE中的Endpoint.publish方法来执行此操作。 First, you create a simple "endpoint interface": 首先,创建一个简单的“端点接口”:

package com.example;

import javax.jws.WebService;

@WebService
public interface ExampleService {
    String getDateTime();
}

Then you specify that interface in an implementation class. 然后,在实现类中指定该接口。 As you can see, both classes must be annotated with @WebService . 如您所见,两个类都必须使用@WebService进行注释。

package com.example;

import java.io.IOException;
import java.net.URL;
import javax.jws.WebService;
import javax.xml.ws.Endpoint;
import javax.xml.ws.Service;
import javax.xml.namespace.QName;

@WebService(endpointInterface = "com.example.ExampleService",
            serviceName = "ExampleService",
            portName = "timeService",
            targetNamespace = "http://example.com/time")
public class ExampleServiceImpl
implements ExampleService {
    public String getDateTime() {
        return String.format("%tc", System.currentTimeMillis());
    }

    public static void main(String[] args)
    throws IOException {
        // Create server
        Endpoint endpoint =
            Endpoint.publish("http://localhost:8080/example",
                             new ExampleServiceImpl());

        URL wsdl = new URL("http://localhost:8080/example?wsdl");

        // Create client
        Service service = Service.create(wsdl,
            new QName("http://example.com/time", "ExampleService"));
        ExampleService e = service.getPort(ExampleService.class);

        // Test it out
        System.out.println(e.getDateTime());

        endpoint.stop();
    }
}

By default, JAX-WS will treat all public methods of an endpoint interface as web methods (since that is commonly what developers will want). 默认情况下,JAX-WS将端点接口的所有公共方法都视为Web方法(因为这通常是开发人员想要的)。 You can have more control by placing the @WebMethod annotation on only the methods you want exposed as web services. 通过将@WebMethod批注仅放置在要作为Web服务公开的方法上,可以实现更多控制。

See the JAX-WS specification for all the details. 有关所有详细信息,请参见JAX-WS规范

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM