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:
- Package ch01.ts;
- Import Java.util.concurrent.ThreadPoolExecutor;
- Import Java.util.concurrent.LinkedBlockingQueue;
- Import Java.util.concurrent.TimeUnit;
- Import Java.util.concurrent.locks.ReentrantLock;
- Import java.util.concurrent.locks.Condition;
- Public class Mythreadpool extends Threadpoolexecutor {
- private static final int pool_size = 10;
- private Boolean is_paused;
- private Reentrantlock Pause_lock = new Reentrantlock ();
- private Condition unpaused = Pause_lock.newcondition ();
- Public Mythreadpool () {
- Super (Pool_size, //core pool size
- Pool_size, //maximum pool size
- 0L, //keep-alive time for idle thread
- Timeunit.seconds, //time unit for keep-alive setting
- New Linkedblockingqueue<runnable> (pool_size)); //Work Queue
- }
- //Some overrides
- protected void BeforeExecute (Thread T, Runnable R) {
- Super.beforeexecute (t, R);
- Pause_lock.lock ();
- try {
- while (is_paused) unpaused.await ();
- }
- catch (Interruptedexception e) {t.interrupt ();}
- finally {pause_lock.unlock ();}
- }
- public void Pause () {
- Pause_lock.lock ();
- try {
- is_paused = true;
- }
- finally {pause_lock.unlock ();}
- }
- public void Resume () {
- Pause_lock.lock ();
- try {
- is_paused = false;
- Unpaused.signalall ();
- }
- finally {pause_lock.unlock ();}
- }
- }
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:
- Package ch01.ts;
- Import Javax.xml.ws.Endpoint;
- Class TIMEPUBLISHERMT { //MT for multithreaded
- private Endpoint Endpoint;
- public static void Main (string[] args) {
- TIMEPUBLISHERMT self = new timepublishermt ();
- Self.create_endpoint ();
- Self.configure_endpoint ();
- Self.publish ();
- }
- private void Create_endpoint () {
- Endpoint = endpoint.create (new Timeserverimpl ());
- }
- private void Configure_endpoint () {
- Endpoint.setexecutor (new Mythreadpool ());
- }
- private Void Publish () {
- int port = 8888;
- String url = "http://localhost:" + port + "/ts";
- Endpoint.publish (URL);
- System.out.println ("Publishing timeserver on port" + port);
- }
- }
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)