This discussion is archived
8 Replies Latest reply: Jul 28, 2012 9:27 AM by Nico RSS

JAX WS multiple requests management

Nico Newbie
Currently Being Moderated
Hi all,
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
  • 1. Re: JAX WS multiple requests management
    murali veligeti - oracle Expert
    Currently Being Moderated
    Hi Nico,

    In multithreading, all the threads are independent. Here the threads miao and bau are independent.

    There is no guarantee which thread executes first. In your program for miao you explisitly called sleep and hence bau thread executed.

    Explanation:
    ========
    When you start both the threads miao and bau both are in runnable start. Only one thread can go to running. Lets consider miao goes to running state. Because you called sleep miao goes to sleeping and bau goes to running state and once the sleep completed for miao it goes to runnable state and as bau is in running and once bau completes miao again goes to running state and then it also completes.


    Thanks & Regards,
    Murali.
    ===========
  • 2. Re: JAX WS multiple requests management
    Nico Newbie
    Currently Being Moderated
    Thanks Murali but this is not the answer I'm looking for. I know that each thread generates a concurrent indipendent execution but the point is: if it's true that *"if the processing of a given request should hang, then all other client requests are effectively blocked"* why the request of the thread called "bau" doesn't wait until the request of "miao" has been processed and satisfied? (the ws needs at least 9 seconds to answer to "miao" client)

    Moreover, I get the same result if I call the ws with SoapUI using 2 different request instances.

    Nico
  • 3. Re: JAX WS multiple requests management
    ramp Explorer
    Currently Being Moderated
    Each request spawns a new thread of execution and if you write code keeping that in mind, one hung request should not prevent the server from servicing others.
    Which is the book that you are reading?
  • 4. Re: JAX WS multiple requests management
    Nico Newbie
    Currently Being Moderated
    "Java Web Services Up and Running". The book is not so updated (published in February 2009, speaks about jax-ws 2.1 in javaSE 6). It is possible that multiple concurrent requests were blocked in that version of jax-ws? Is the current stable release of jax-ws 2.2?

    Thanks again
  • 5. Re: JAX WS multiple requests management
    ramp Explorer
    Currently Being Moderated
    Nico wrote:
    "Java Web Services Up and Running". The book is not so updated (published in February 2009, speaks about jax-ws 2.1 in javaSE 6). It is possible that multiple concurrent requests were blocked in that version of jax-ws?
    No. Every web service is usually front-ed by a servlet in all JAX-WS implementations and can handle concurrent requests. Of course as part of your request processing a shared resource is locked (like an update on a row or an open file), a hung request would block others. But that's because of your logic and not the server's (in)ability to handle concurrent requests.
    Is the current stable release of jax-ws 2.2?
    2.2.x
  • 6. Re: JAX WS multiple requests management
    Nico Newbie
    Currently Being Moderated
    Hi ramp, thanks for your help. Just a quick clarification: a jax-ws web service is front-ed by a servlet even when publishing the ws using only javaSE (that is, without a web container). I just compile and run the previous code.

    Thanks,
    Nico
  • 7. Re: JAX WS multiple requests management
    ramp Explorer
    Currently Being Moderated
    Nico wrote:
    Hi ramp, thanks for your help. Just a quick clarification: a jax-ws web service is front-ed by a servlet even when publishing the ws using only javaSE (that is, without a web container).
    Are you asking :) ? I would imagine so. I used Servlet as a generic term because it is easy to comprehend. Internally there has to be a resource capable of intercepting a http(s) request and handing it down to an end point.
  • 8. Re: JAX WS multiple requests management
    Nico Newbie
    Currently Being Moderated
    Thanks ramp

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points