Wednesday, July 7, 2010

SCDJWS 5, web service design patterns notes


Web Service Design Patterns






The
design patterns related to Web Service help to enhance
maintainability of the solution or to minimize QoS impact associated
with building applications using web service frameworks. Web Services
based interaction might be expensive because of


  • operation
    is expensive in term of server-side processing
  • communication
    overhead (amount of data transfer/bandwidth)
  • encoding/decoding
    may be expensive

Application
designers should look for alternatives in this situation.


Asynchronous Interaction Pattern


Goals
of this pattern are:


  • decouple
    input and output.
  • deliver
    output from server to client.
  • Associate
    output message with corresponding input message

This
can be achieved with various application level designs to achieve the
above goals which are Server-side push, Client-side pull. JMS-based
and JAX-WS based which are described below.




Server-side
push


Approach
1: Client supplies the address of a web service dedicated to
process specific response to the request, as part of request and the
server contacts the dedicated web service to reply to the query back
to the client.


Approach
2: Client supplies address of a generic web service that can be
invoked to supply the result of operation back to client, along with
unique token to identify this request, The server later invokes this
generic web service to deliver an answer to this client the server
uses the same token as part of response message to identify the
response to client. WS-Addressing defines a way to create these
tokens portably.




Client-side
pull


Client
issues a request along with a unique token to server, the server
accepts the request, allowing client to continue processing. server
process each request to obtain response and stores all responses
indexed by tokens supplied by client in each request in a data
structure accessible as a new Response web service. Each client
queries the new web service for answer to earlier requests using the
same token. Enough storage required on server side to store all
response, until the client retrieves it. For reliability the storage
might need to be persistence. Increases network overhead as client
may poll periodically for it's response.




JMS
based


Non-portable
web service solution, uses JMS as message transport instead of HTTP.
Both Client-side pull and Server-side push can be implemented using
JMS. In case of Client-side pull the client uses multiple requests
first being a JMS message and the subsequent ones being synchronous
and portable.




JAX-WS
based


JAX-WS
introduces Dispatch<T> and Provider<T> interfaces to
describe client and server side of the interaction. On client side it
introduced the ability to indicate whether the interaction is
synchronous or asynchronous, whether it's Client-side pull or
Server-side push or one way.

interface
Dispatch<T>{ // client-side
 
T invoke(T
msg);
 
Response<T>
invokeAsync(T msg);
 
Future<?>
invokeAsync(T msg, AsyncHandler<T> h);
 
void
invikeOneWay(T msg);

}



interface
Provider<T>{ // server-side
 
T invoke(T msg,
Map<String, Object> context);
}

Example

MessagingAPIMessage
request = new MessagingAPIMessage( "sayHello", "Tracy"
);

MessagingAPIMessage
response = MessagingAPIMessage) port.invoke( request );

System.out.println("Response:
" + response.getResult());

AsyncHandler<Object>
responseHandler = new AsyncHandler<Object>() {
 
public void
handleResponse(Response<Object> resp){
  
try {
    
MessagingAPIMessage
result = (MessagingAPIMessage) response.get();
   
 
System.out.println(
"Response: " + result.getResult() );
  
} catch(
Exception e ) {  
}
 
}
};
port.invokeAsync(
request, responseHandler );



Advantages:
More responsive application, JAX-WS provides transparent
implementation

Disadvantages: Other
than JAX-WS requires more complex designs

JMS Bridge


The
Characteristics of JMS Bridge pattern are as follow:


  • Keep
    different subsystems using their own JMS implementation
  • Introduce
    a client which can relay messages from one JMS implementation to
    next
  • the
    JMS clients should be implemented as Web Services

Advantages:
No need to develop vendor specific to bridge two underlying
middle-ware vendor. It's vendor and JMS independent.

Disadvantage:
Overhead XML encoding/transmission and decoding

Web Service Cache


Cache
can be introduced at two places which will be transparent to client
(as Endpoint Handlers). The overhead is reduced by short-circuiting
requests that do not need to be executed.


Advantages:
Reduce communication and processing overhead

Disadvantage:
Increased memory
footprint, application must realize when to invalidate or refresh
cache.

Web Service Broker


Can
be used implement some services as Web Service and still address the
concerns that web services don't address like transaction
propagation. Web Service broker is introduced as a middle-man between
the client and the remote service in which the client is interested.
Can be implemented as a state-full session bean.


Advantage:
Simpler client design

Disadvantage:
Complex to implement (not guaranteed)

Web Service Logger


A
common approach to introduce logging into the design of an
application involves the application of Decorator pattern as follows:



  • An additional object is
    introduced as a wrapper around the actual service provider.
  • The logging functionality is
    captured in the wrapper.