What is this?

This is similar to the other extension HttpSessionScope . Traditionally JAX-WS has never taken advantage of object state, just like servlet. That is, the container creates only one instance of your service class, and then have it serve all the requests concurrently. This makes it impossible to set values to instance fields, as you'll experience concurrency problem as soon as multiple threads hit your service.

Say you have a service, and its web methods try to access Database using Connection object. Connection object doesn't handle concurrent accesses, hence the access needs to be synchronized. For e.g.:

@WebService
public class DataService {
    Connection con;

    public synchronized void delete() {
        Statement stmt = con.createStatement();
        stmt.executeQuery();
    }

    public synchronized void add() {
        Statement stmt = con.createStatement();
        stmt.executeQuery();
    }
}

This will have performance issues since some of the requests need to wait until a current request is finished. To avoid synchronization, one could create Connection objects in each invocation and that is even more expensive! So we need exclusive access to Connection object and reuse them exclusively for other invocations. This can be achieved by creating more instances and using an instance exclusively for one invocation. To achive this, jax-ws runtime creates instances and stores and reuses them from ThreadLocal.

import org.jvnet.jax_ws_commons.thread_scope.ThreadScope;

@WebService
@ThreadScope
public class DataService {
    Connection con;

    public void delete() {
        Statement stmt = con.createStatement();
        stmt.executeQuery();
    }

    public void add() {
        Statement stmt = con.createStatement();
        stmt.executeQuery();
    }
}

The @ThreadScope annotation tells the JAX-WS RI to create one instance of DataService per each thread. No need to have synchronized keyword.

How it's implemented?

This extension is based on a publicly available extension point in the JAX-WS RI 2.1 called InstanceResolver . This provides a pluggability point where 3rd party could control how the JAX-WS RI dispatches incoming requests to service instances. It is implemented similar to HttpSessionScope annotation .

This extension requires the JAX-WS RI 2.1 EA2 or later.


Terms of Use; Privacy Policy; Copyright ©2013-2014 (revision 20140418.2d69abc)
 
 
Close
loading
Please Confirm
Close