WS-Addressing and stateful webservice

 


WS-addressing and its use



  • Provides transport-neutral mechanisms to address Web services and messages.

  • Stateful webservice, Web services are usually stateless, , however it is possible to enable webservice to be stateful i.e. to share multiple instances of service endpoints between subsequent invocation.

  • Simple and complex Message Exchange Patterns

  • Used for other WS-\* specification like reliable Messaging, WS-MetadataExchange, WS-Security


WS-Addressing and stateful webservice


We will dive in details how stateful sebservice is created? how a client can access the same? and how metro/jax-ws maintain the state in between subsequent invocation?, We will make use of the examples which comes with metro/jax-ws RI


Examples 



Server (Book.java and BookStore.java)



@Stateful @WebService @Addressing
public class Book {
    private final String id;
    private volatile List<String> reviews = new ArrayList<String>();
    public Book(String id) {
        this.id = id;
    }
    public String getId() {
        return id;
    }
    public synchronized void postReview(String review) {
        List<String> l = new ArrayList<String>(reviews);
        l.add(review);
        reviews = l;
    }
    public List<String> getReviews() {
        return reviews;
    }
    public boolean equals(Object that) {
        if (that == null || this.getClass() != that.getClass()) return false;
        return this.id.equals(((Book)that).id);
    }
    public int hashCode() {
        return id.hashCode();
    }


    public static StatefulWebServiceManager<Book> manager;
}



@WebService
public class BookStore {
    public W3CEndpointReference getProduct(String id) {
        return Book.manager.export(new Book(id));
    }
}
 




See the Book.java which is stateful webservice, this is done by declaring Stateful annotation on a Book.java class. Note the class also have a public static method/field that takes StatefulWebServiceManager. Once the service is deployed the jax-ws does the resource injection on the field or the method. A stateful web service (Book.java) does not need to have a default constructor. Most of the time user would to define a constructor that takes some arguments, so that each instance carries certain state. Each instance of a stateful web service( Book.java) is identified by an unique EndpointReference. BookStore.java creates an instance of a class, JAX-WS RI assign a unique EPR for the instance as follows   Book.manager.export(new Book(id))



Client
        BookStore bookstore = new BookStoreService().getBookStorePort();
        BookService service = new BookService();


        1. Book book1 = service.getPort(bookstore.getProduct("abc001"), Book.class);
        2.  book1.getId();
        3. book1.postReview("This book is great!");
        4. book1.getReviews();


        similar code for book2....




Once the client runtime receives the EPR info , it uses the EPR info in the subsequent call, i.e When client send messages to this EPR, the JAX-WS RI makes sure that the particular exported instance associated with that EPR will receive a service invocation. We will see shortly how this is achieved


 


Digging Deeper 


We will trace the soap request response for the client code (line 1, 2, and 3). This will give an clear idea how an Endpoint can associate the request with an exported instance of an EPR, and how the exported instance is  identified using an unique Identifier. 


1. Invoke BookStore service( bookstore.getProduct("abc001")) , Note the soap request-response, The response contain the EPR details which contains the address and the unique <jaxws:objectid>, note this objectid is used in subsequent call to the BookService, and this how the jaxws RI find out which instance of the EPR will get the invocation




get endpoint reference 



2. book1.getId(); note the soap request, here the jax-ws inserts the required addressing headers and the <jaxws:objectid>,  





get id of the book 






3.  book1.postReview("This book is great!"); note the soap request, here also the jax-ws inserts the required addressing headers and the <jaxws:objectid>,  





post review of the book



 





 More Info



 


 

Comments:

Good stuff. I was hoping you could also provide an example using the book and bookstore classes of setting a timeout and utilizing a callback on the StatefulWebServiceManager.

Posted by David on May 22, 2009 at 05:11 AM PDT #

Do u know how to call this in asp.net?

Here, getProduct returns EndpointReference, How to use this in asp.net to get stateful bookService

Posted by pawan on June 23, 2009 at 04:53 PM PDT #

Post a Comment:
  • HTML Syntax: NOT allowed
About

sujit

Search

Categories
Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today