Multithreaded Endpoint Service Publishers (excerpt)

Source: Internet
Author: User

Multithreaded Endpoint Service Publishers

Excerptfrom: JAVA Web services: building and running

Ningzhanga

Java Web Services: Build and Run is an example-driven way of describing the Java-related APIs covered by XML Web services and RESTful Web services in a clear and pragmatic way, and the 1th chapter describes the Java Web Services QuickStart. This section is about multithreaded endpoint service publishers.

AD:2014WOT Global Software Technology Summit Beijing Station course video release

1.1 Multi-Threaded endpoint Service publisher

Multithreading the Endpoint Publisher

In previous examples, the endpoint publisher was single-threaded, meaning that only one client request could be processed concurrently at the same time. Before you process the next request, you must wait for the published service to complete the process that is currently being processed. If the service request that is currently being processed is suspended, no further service requests can be made until the pending request has been processed.

In a formal product environment, the endpoint Publisher needs to process multiple client requests concurrently, so that multiple unpredictable client requests can be processed at the same time. For the underlying computer systems, such as for the Chenduo processor (smp,symmetric multiprocessor) system, each individual CPU can concurrently handle different client requests. In a single-processor computer environment, a time-sharing mechanism can be used to complete concurrent processing of client requests within the same time. The Java language supports multi-threaded concurrency processing mechanisms. Therefore, what we need to solve is how to make the endpoint publisher also support multithreading mechanism. The JWS framework supports endpoint multi-threading mode, and the program staff does not have to face complex and difficult-to-control synchronization blocks (Synchronized block), wait and notify method invocations, and so on.

The endpoint object has a executor property that defines the standard Get/set method. Excutor is used to perform a running (Runnable) task, such as a Java thread instance. (The Runnable interface in Java only defines a common interface method with no return value: Run ().) The Executor object is a good choice for multithreading because the executor provides a high level of encapsulation for the commit and management of the task being performed. The first step in enabling the endpoint Publisher to support multithreading is to create a executor class with the following code:

  1. Package ch01.ts;
  2. Import Java.util.concurrent.ThreadPoolExecutor;
  3. Import Java.util.concurrent.LinkedBlockingQueue;
  4. Import Java.util.concurrent.TimeUnit;
  5. Import Java.util.concurrent.locks.ReentrantLock;
  6. Import java.util.concurrent.locks.Condition;
  7. Public class Mythreadpool extends Threadpoolexecutor {
  8. private static final int pool_size = 10;
  9. private Boolean is_paused;
  10. private Reentrantlock Pause_lock = new Reentrantlock ();
  11. private Condition unpaused = Pause_lock.newcondition ();
  12. Public Mythreadpool () {
  13. Super (Pool_size, //core pool size
  14. Pool_size, //maximum pool size
  15. 0L, //keep-alive time for idle thread
  16. Timeunit.seconds, //time unit for keep-alive setting
  17. New Linkedblockingqueue<runnable> (pool_size)); //Work Queue
  18. }
  19. //Some overrides
  20. protected void BeforeExecute (Thread T, Runnable R) {
  21. Super.beforeexecute (t, R);
  22. Pause_lock.lock ();
  23. try {
  24. while (is_paused) unpaused.await ();
  25. }
  26. catch (Interruptedexception e) {t.interrupt ();}
  27. finally {pause_lock.unlock ();}
  28. }
  29. public void Pause () {
  30. Pause_lock.lock ();
  31. try {
  32. is_paused = true;
  33. }
  34. finally {pause_lock.unlock ();}
  35. }
  36. public void Resume () {
  37. Pause_lock.lock ();
  38. try {
  39. is_paused = false;
  40. Unpaused.signalall ();
  41. }
  42. finally {pause_lock.unlock ();}
  43. }
  44. }

Mythreadpool creates a thread pool of 10 threads, which are stored using a fixed-size queue. When a thread pool is used, the next task in the queue must wait until an idle thread in the thread sink is freed. The details of these management are handled automatically by the thread pool. Mythreadpool rewritten several methods in the Threadpoolexecutor.

With the Mythreadpool object, the endpoint Publisher can support multithreading. Here is the revised code based on the previous release program, which decomposes the publishing task in different ways:

  1. Package ch01.ts;
  2. Import Javax.xml.ws.Endpoint;
  3. Class TIMEPUBLISHERMT { //MT for multithreaded
  4. private Endpoint Endpoint;
  5. public static void Main (string[] args) {
  6. TIMEPUBLISHERMT self = new timepublishermt ();
  7. Self.create_endpoint ();
  8. Self.configure_endpoint ();
  9. Self.publish ();
  10. }
  11. private void Create_endpoint () {
  12. Endpoint = endpoint.create (new Timeserverimpl ());
  13. }
  14. private void Configure_endpoint () {
  15. Endpoint.setexecutor (new Mythreadpool ());
  16. }
  17. private Void Publish () {
  18. int port = 8888;
  19. String url = "http://localhost:" + port + "/ts";
  20. Endpoint.publish (URL);
  21. System.out.println ("Publishing timeserver on port" + port);
  22. }
  23. }

After the Threadpoolworker is written, the rest of the work is to set the executor property of the endpoint instance in this example, and the Publisher does not involve any details of thread management.

Multi-threaded endpoint publishers are better suited to some lightweight products, but in a real application scenario, the above is not a service container; A service container can publish multiple Web services on the same port. Some web containers, such as Tomcat, provide these feature implementations, which are more appropriate for use when publishing multiple Web services. The Web service in the following example will show you how to publish through Tomcat.

Multithreaded Endpoint Service Publishers (excerpt)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.