Nico Tanzarella
Nico Tanzarella

Reputation: 179

JAX WS multiple requests management

I'm starting studying java web services with JAX WS. The first chapter of the book I'm reading shows how to build and deploy a simple jax ws web service using only java SE. In particular, the web service is published through the Endpoint class. After publishing the web service, the author specifies that "Out of the box, the Endpoint publisher handles one client request at a time ... if the processing of a given request should hang, then all other client requests are effectively blocked. An example at the end of this chapter shows how Endpoint can handle requests concurrently so that one hung request does not block the others."

To see this, I tried to send two requests to a web service with 2 threads. Here is the code:

@WebService(endpointInterface = "ProveVelociJava.WS.MyWsWithJavaSE.SayHello")
public class SayHelloImpl implements SayHello {
    public String greetings(String param) {
        System.out.println("\nStarting " + param + "...\n");    
        if(param.equals("miao")) {      
            try {           
                Thread.sleep(9000);
            }
            catch(Exception e) {}
        }
        System.out.println("Ended " + param + "\n\n");
        return "Hi, " + param;
    }
}

public class SayHelloPublisher {
    public static void main(String[ ] args) {   
        // 1st argument is the publication URL
        // 2nd argument is an SIB instance
        Endpoint.publish("http://127.0.0.1:9899/say", new SayHelloImpl());
    }
}

class MyClient extends Thread {
    private static URL url;
    private static QName qname;
    private static Service service;
    private static SayHello eif;

    static {
        try {
            url = new URL("http://127.0.0.1:9899/say?wsdl");
            qname = new QName("http://MyWsWithJavaSE.WS.ProveVelociJava/", "SayHelloImplService");
            service = Service.create(MyClient.url, MyClient.qname);
            // Extract the endpoint interface, the service "port".
            eif = service.getPort(SayHello.class);
        }
        catch(Exception e) {}
    }
    private String name;
    public MyClient(String n) {
        name = n;
    }
    public void run() {
        System.out.println(MyClient.eif.greetings(this.name));
    }
    public static void main(String args[ ]) throws Exception {  
        MyClient t1 = new MyClient("miao");
        MyClient t2 = new MyClient("bau");
        t1.start();
        t2.start();
    }
}

If I start MyClient class, the thread called "miao" sends its request and then goes to sleep. However, the thread called "bau" does not wait for the previous thread and its request is immediately satisfied.

Am I missing something? Can java threads be used to simulate multiple requests?

Thanks a lot for your help, Nico.

Upvotes: 3

Views: 4164

Answers (3)

Tempak
Tempak

Reputation: 21

By default Endpoint handles multiple requests at the same time, and create separate thread for every new concurrent request. You can insert following line into your WebMethod implementation, to see thread id:

System.out.println(Thread.currentThread().toString());

If you request one-by-one this will print the same thread info, because terminated thread will be reused in next http request. But if you make multiple requests at the same time then you will see that different threads will be calling your webmethod.

One request will not block any other concurrent one as long your webmethod does not have synchronized blocks, whole webmethod is not synchronized or you are using some kind semaphores.

Upvotes: 2

Nico Tanzarella
Nico Tanzarella

Reputation: 179

I downloaded JAX-WS 2.0 specification and it disproves what the book I'm reading says:

"An endpoint consists of an object that acts as the Web service implementation (called here implementor) plus some configuration information ... An Endpoint will be typically invoked to serve concurrent requests, so its implementor should be written so as to support multiple threads. The synchronized keyword may be used as usual to control access to critical sections of code. For finer control over the threads used to dispatch incoming requests, an application can directly set the executor to be used"

(http://jcp.org/aboutJava/communityprocess/final/jsr224/index.html, section 5.2.2 "Publishing", page 67)

The book speaks about JAX-WS 2.1 but I didn't manage to download that version of the specification. Anyway, JAX-WS 2.2 specification (current version) confirms the concurrent nature of the Endpoint class (it contains the same sentences as above).

I don't know what the author of the book means exactly.

Upvotes: 2

Abhishek
Abhishek

Reputation: 422

Its late but might help.

Endpoint.publish(Url, ServiceImplObj) publishes a webservice at a given url. The no. of threads assigned for request handling truly is under control of the jvm because this is a light weight deployment which is handled by jvm itself.

For better clarification you can print the current thread name at service side and you can see that the service threads are being assigned from a thread pool which is managed by jvm.

[pool-1-thread-1]: Response[57]:
[pool-1-thread-5]: Response[58]:
[pool-1-thread-4]: Response[59]:
[pool-1-thread-3]: Response[60]:
[pool-1-thread-6]: Response[61]:
[pool-1-thread-6]: Response[62]:

Upvotes: 1

Related Questions