Svcc2009 Async Ws

50
Slide 1 Asynchronous Web Services Pyounguk Cho, Oracle Corporation Manoj Kumar, Oracle Corporation

description

Asynchronous Web Services

Transcript of Svcc2009 Async Ws

Page 1: Svcc2009 Async Ws

Slide 1

Asynchronous Web Services

Pyounguk Cho, Oracle Corporation

Manoj Kumar, Oracle Corporation

Page 2: Svcc2009 Async Ws

Slide 2

Agenda

• Introduction• Client side asynchrony• Server side asynchrony• Implementation of clients and services• Declarative asynchronous services• Advanced Topics• Q & A

Page 3: Svcc2009 Async Ws

Slide 3

IntroductionWeb Service

• a software system designed to support interoperable machine-to-machine interaction over a network

• Using JAX-WS based tools..• Publish a POJO/SLSB annotated with

javax.jws.WebService• WSDL gets created• Clients generate STUB using this WSDL• Make calls to WS using interface on stub

Page 4: Svcc2009 Async Ws

Slide 4

IntroductionWeb Service

package stockquote;

import javax.jws.WebService;

@WebServicepublic class StockQuote { public float getPrice(String ticker) { return 30; }}

Page 5: Svcc2009 Async Ws

Slide 5

IntroductionWSDL

<?xml version="1.0" encoding="UTF-8"?>

<definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://stockquote/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://stockquote/" name="StockQuoteService">

Page 6: Svcc2009 Async Ws

Slide 6

WSDL Types

<types>

<xsd:schema>

<xsd:import namespace="http://stockquote/" schemaLocation="http://localhost:7101/StockQuote-StockQuote-context-root/StockQuotePort?xsd=1"/>

</xsd:schema>

</types>

Page 7: Svcc2009 Async Ws

Slide 7

WSDL Messages

<message name="getPrice">

<part name="parameters“

element="tns:getPrice"/>

</message>

<message name="getPriceResponse">

<part name="parameters“

element="tns:getPriceResponse"/>

</message>

Page 8: Svcc2009 Async Ws

Slide 8

WSDL Port Type

<portType name="StockQuote">

<operation name="getPrice">

<input message="tns:getPrice"/>

<output message="tns:getPriceResponse"/>

</operation>

</portType>

Page 9: Svcc2009 Async Ws

Slide 9

WSDL Binding

<binding name="StockQuotePortBinding“

type="tns:StockQuote">

<soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>

<operation name="getPrice">

</operation>

</binding>

Page 10: Svcc2009 Async Ws

Slide 10

WSDL Service

<service name="StockQuoteService">

<port name="StockQuotePort“

binding="tns:StockQuotePortBinding">

<soap:address location="http://localhost:7101/StockQuote-StockQuote-context-root/StockQuotePort"/>

</port>

</service>

</definitions>

Page 11: Svcc2009 Async Ws

Slide 11

Web Service API

• JAX-RPC• Java API for XML-based RPC

• JAX-WS• JAVA API for XML Web Services

• Invoke a method on a stub• Call gets converted to SOAP message• Soap Message is sent using http transport

Page 12: Svcc2009 Async Ws

Slide 12

IntroductionSynchronous Call

StockQuoteService service = new ...;

StockQuote stockQuote = service.getStockQuotePort();

float price = stockQuote.getPrice("ORCL");

Page 13: Svcc2009 Async Ws

Slide 13

IntroductionAsynchronous Call

StockQuoteService service = new ...;

StockQuote stockQuote = service.getStockQuotePort();

quoteService.getPriceAsync(“ORCL”);

// Do some other work

// How and when do we get the response?

Page 14: Svcc2009 Async Ws

Slide 14

IntroductionWhy Asynchronous?

• The world is fundamentally asynchronous• Improvement in .. • User’s experience• Reliability in distributed applications• Scalability

• Only way to go in long running processes• Implementation• Client side asynchrony• Server side asynchrony

Page 15: Svcc2009 Async Ws

Slide 15

IntroductionAlternatives to JAX-*

• BPEL• Integration of other operations• Can be re-exposed as an asynchronous web service

• Asynchronous 3.1 EJBTM architecture • Java only

• JMS, MQueue, Native Database Queues• Generally platform specific

• SMTP• Golden oldie

• SCA

Page 16: Svcc2009 Async Ws

Slide 16

Agenda

• Introduction• Client side asynchrony• Server side asynchrony• Implementation of clients and services• Declarative asynchronous services• Advanced Topics• Q & A

Page 17: Svcc2009 Async Ws

Slide 17

Client Side AsynchronyJAXWS 2.x API

• Calling synchronous services in asynchronous way• Not really synchronous though• WSDL to Java customization option

• <jaxws:enableAsyncMapping>true</jaxws:ena…>

• Asynchronous Interface• Polling• Callback

• Control of executors to manage the number of threads used

Page 18: Svcc2009 Async Ws

Slide 18

Client Side Asynchrony Synchronous Interface

@WebService

public interface StockQuote {

float getPrice(String ticker);

}

Page 19: Svcc2009 Async Ws

Slide 19

Client Side Asynchrony Asynchronous Interface

@WebServicepublic interface StockQuote {float getPrice(String ticker);

Response<Float> getPriceAsync(String ticker);

}

Page 20: Svcc2009 Async Ws

Slide 20

Client Side AsynchronyPolling Style

StockQuote quoteService = (StockQuote)service.getPort(portName);

Response<Float> response = quoteService.getPriceAsync(ticker);

// do something else while the// stock quote is in flight

float quote = response.get();

Page 21: Svcc2009 Async Ws

Slide 21

Client Side Asynchrony Asynchronous Interface

@WebService

public interface StockQuote {

float getPrice(String ticker);

Response<Float> getPriceAsync(String ticker);

Future<?> getPriceAsync(String ticker, AsyncHandler<Float>

responseReceiver);

}

Page 22: Svcc2009 Async Ws

Slide 22

Client Side AsynchronyCallback Style

PriceReceiver priceReceiver = new PriceReceiver();

quoteService.getPriceAsync(ticker, priceReceiver);

..

class PriceReceiver implements AsyncHandler<Float> {

public void handleResponse(Response<Float> response) {

Float price = response.get();

// do something with the result

}

}

Page 23: Svcc2009 Async Ws

Slide 23

Client Side AsynchronySummary

• Call synchronous service in asynchronous way

• Invoke async operation and • Poll for the result• Provide a callback class

• Use your tricks to coordinate

• Http time out still a problem• But works with “Any” synchronous service

Page 24: Svcc2009 Async Ws

Slide 24

Agenda

• Introduction• Client side asynchrony• Server side asynchrony• Implementation of clients and services• Declarative asynchronous services• Advanced Topics• Q & A

Page 25: Svcc2009 Async Ws

Slide 25

Server Side Asynchrony

Page 26: Svcc2009 Async Ws

Slide 26

Server Side Asynchrony

• Invocation and response separated• A new connection for the call and the

response• The invoker and the receiver don’t have to be

on the same machine• Invoker must pass the replyTo address• A message Id must flow for correlation

Page 27: Svcc2009 Async Ws

Slide 27

Server Side Asynchrony HTTP

• Generally has two port types, two binding; but one service

• Can use partnerLinkType to relate the two different ports • Part of the BPEL specification; but an extension to WSDL

Page 28: Svcc2009 Async Ws

Slide 28

Server Side Asynchrony WS-Addressing

• Transport agnostic SOAP message delivery• Endpoints references and message information

headers• Message information headers

• Allow you to relate the message to a particular instance

• Endpoint references• Tell you where to send the message along with relevant

metadata

• They can tell you where to reply to and send faults to, important for asynchronous services

Page 29: Svcc2009 Async Ws

Slide 29

Server Side Asynchrony Request

<soap:Envelope> <soap:Header> <wsa:MessageID>uuid:35f19ca8-c9fe</wsa:MessageID> <wsa:Action>http://lh:80/request/…</wsa:Action> <wsa:ReplyTo> <wsa:Address>http://lh:77/response</wsa:Address> <wsa:ReferenceParameters> <customHeader>correlationKey</customHeader> </wsa:ReferenceParameters> </wsa:ReplyTo> <wsa:To>http://lh:80/request</wsa:To> </soap:Header> <soap:Body>…</soap:Body></soap:Envelope>

Page 30: Svcc2009 Async Ws

Slide 30

Server Side Asynchrony Response

<soap:Envelope> <soap:Header> <wsa:To>http://lh:77/response</wsa:To> <wsa:Action>urn:response</wsa:Action> <wsa:MessageID>uuid:cb383139-cdf2</wsa:MessageID> <wsa:RelatesTo>uuid:35f19ca8-c9fe</wsa:RelatesTo> <customHeader wsa:IsReferenceParameter=“1”> correlationKey</customHeader> </soap:Header> <soap:Body>…</soap:Body></soap:Envelope>

Page 31: Svcc2009 Async Ws

Slide 31

Agenda

• Introduction• Client side asynchrony• Server side asynchrony• Implementation of clients and services• Declarative asynchronous services• Advanced Topics• Q & A

Page 32: Svcc2009 Async Ws

Slide 32

Server Side AsynchronyClient Implementation

• Create a proxy for the service• Implement the callback binding to receive the response• Deploy the callback web service

• Instantiate a proxy for the initiation endpoint. • Set the MessageId property to a new unique value• Set the To; ReplyTo; and FaultTo URI• Invoke the web service

Page 33: Svcc2009 Async Ws

Slide 33

Server Side AsynchronyClient Implementation (contd)

• In the callback web service use the “RelatesTo” header or reference parameters to correlate the response

Page 34: Svcc2009 Async Ws

Slide 34

Server Side AsynchronyClient side invocation

StockQuote sq = service.getStockQuote();

// Populate headers

AddressingVersion av = AddressingVersion.W3C;WSBindingProvider wsbp = (WSBindingProvider)sq;WSEndpointReference replyTo = new WSEndpointReference( “http://lh:77/response”,av);String uuid = “uuid:” + UUID.randomUUID();

wsbp.setOutboundHeaders( new StringHeader(av.messageIDTag, uuid), new StringHeader(av.toTag,”http://lh:88/…”), replyTo.createHeader(av.replyToTag);

sq.getPrice(“ORCL”);

Page 35: Svcc2009 Async Ws

Slide 35

Server Side AsynchronyCallback Implementation

// Deal with response

@Resource WebServiceContext wsContext;

public void getPriceResponse(String _return){ HeaderList hl = wsContext.getMessageContext().get( JAXWSPRopertiers.INBOUND_HEADER_LIST_PROPERTY); Header h = hl.get(AddressingVersion.W3C.relatesToTag()); String relatesToMessageId = h.getStringContents();

…}

//How about receiving a fault?

Page 36: Svcc2009 Async Ws

Slide 36

Server Side AsynchronyFaultTo in Callback

• Just create a @WebServiceProvider?• There was a bug in JAX-WS RI (Issue 585,

fixed in 2.1.5)• You might end up having to use a Servlet if on

an older version. • JAX-WS should support it in future

Page 37: Svcc2009 Async Ws

Slide 37

Server Side AsynchronyService Implementation

• Accept the asynchronous request and save it (where?)

• Return 202 Accepted

• Pick the request and process it• Invoke the callback service and pass the

result

Page 38: Svcc2009 Async Ws

Slide 38

Server Side Asynchrony Using JMS Queue for incoming

request

Page 39: Svcc2009 Async Ws

Slide 39

Server Side Asynchrony Using response queue

Page 40: Svcc2009 Async Ws

Slide 40

Agenda

• Introduction• Client side asynchrony• Server side asynchrony• Implementation of clients and services• Declarative asynchronous services• Advanced Topics• Q & A

Page 41: Svcc2009 Async Ws

Slide 41

Declarative Asynchronous Services

• Implementing is not trivial• Why not use a declarative model?• Coming in Fusion Middleware, based on the

JMS model• Just add one annotation in POJO/SLSB!

Page 42: Svcc2009 Async Ws

Slide 42

Declarative Asynchronous Services

package com.stock;

import …;

@PortableWebService@AsyncWebService public class StockQuote { public float getPrice(String ticker) { return 100; }}

Yes Just One Annotation

Page 43: Svcc2009 Async Ws

Slide 43

Declarative Asynchronous Services

• The resultant WSDL

Page 44: Svcc2009 Async Ws

Slide 44

Agenda

• Introduction• Client side asynchrony• Server side asynchrony• Implementation of clients and services• Declarative asynchronous services• Advanced Topics• Q & A

Page 45: Svcc2009 Async Ws

Slide 45

Advanced Topics Policy for Asynchronous Services

• Asynchronous Service • Server side policy for incoming request (2)• Callback client side policy to send response (3)

• Asynchronous Client• Client side policy to invoke asynchronous operation (1)• Callback server side policy to receive response (4)

Page 46: Svcc2009 Async Ws

Slide 4646

Advanced TopicsPolicy for Asynchronous Services

• Multiple ad hoc clients for service• Use PKI for web of trust, servers trust servers

• Store incoming client public key for encrypting response where available

• Or pass keys using WS-Addressing headers and WS-Identity

• Use SAML, callback reuses identity of invoker• M:N configuration, nightmare

Page 47: Svcc2009 Async Ws

Slide 47

Advanced Topics Transaction Control

Page 48: Svcc2009 Async Ws

Slide 48

Advanced Topics Reliability

• Make piece wise transactional and reliable• Ensure that request and response do not get

lost• Make MDBs run in transaction• Container managed transaction demarcation• Bean managed transaction demarcation

• Setup JMS queue for retries• Setup error queues for request and response

queues

Page 49: Svcc2009 Async Ws

Slide 49

Summary• Client side asynchrony of JAX-WS

• Works for any synchronous services• Polling or callback style API

• Server side asynchrony• Two separate one way calls for request and response• Use WS-Addressing for message correlation• Client Side

• Pass Message Id and ReplyTo address• Implement Callback service, and receive relatesToMessageId

• Server side• Save the request in a queue, return asap• Process the request• Call callback service with the response

• Transaction and reliability

Page 50: Svcc2009 Async Ws

Slide 50

Q & A• See Also• http://jax-ws.dev.java.net• http://www.oracle.com/technology/products/jdev/• http://www.oracle.com/technology/software/product

s/middleware/index.html• http://forums.oracle.com/forums/

• Personal blogs• http://kingsfleet.blogspot.com• http://manoj-kumar-on.blogspot.com