Apache SOAP and the Java API for XML

  • Slides: 43
Download presentation
Apache SOAP and the Java API for XML Messaging (JAXM) Notes from the Axis

Apache SOAP and the Java API for XML Messaging (JAXM) Notes from the Axis User’s Guide and the books “Java Web Services” by Deitel and “Distributed Systems Concepts and Design” by Coulouris 1

Middleware layers Applications RMI, RPC and events Request reply protocol Middleware layers External data

Middleware layers Applications RMI, RPC and events Request reply protocol Middleware layers External data representation Operating System 2

Remote and local method invocations local remote invocation A B C local E invocation

Remote and local method invocations local remote invocation A B C local E invocation local invocation D remote invocation F 3

A remote object and its remote interface remoteobject remote interface { Data m 1

A remote object and its remote interface remoteobject remote interface { Data m 1 m 2 m 3 implementation of methods m 4 m 5 m 6 4

Invocation semantics Fault tolerance measures Retransmit request message Duplicate filtering Invocation semantics Re-execute procedure

Invocation semantics Fault tolerance measures Retransmit request message Duplicate filtering Invocation semantics Re-execute procedure or retransmit reply No Not applicable Maybe Yes No Re-execute procedure At-least-once Yes Retransmit reply At-most-once 5

The role of proxy and skeleton in remote method invocation server client object A

The role of proxy and skeleton in remote method invocation server client object A proxy for B Request skeleton & dispatcher for B’s class remote object B Reply Communication Remote reference module Communication module Remote reference module 6

What is SOAP? • • • XML-based communication protocol XML-based encoding format Supports inter-application

What is SOAP? • • • XML-based communication protocol XML-based encoding format Supports inter-application communication Cross platform Cross Language 7

What is Axis? • Apache Extensible Integration System • Axis began as IBM’s SOAP

What is Axis? • Apache Extensible Integration System • Axis began as IBM’s SOAP 4 J • A Framework for constructing SOAP clients and servers • Written in Java 8

Who competes with Axis? • • Cape. Connect from Cape. Clear GLUE Standard from

Who competes with Axis? • • Cape. Connect from Cape. Clear GLUE Standard from Mind. Electric Orbix E 2 A XMLBus 5. 2 From IONA WASP Server for Java 4. 0 from Systinet 9

Axis Architecture Client Transport Listener Message Handlers Provider Java Class 10

Axis Architecture Client Transport Listener Message Handlers Provider Java Class 10

Axis WSDL Support • The Web Service Description Language is a machine readable description

Axis WSDL Support • The Web Service Description Language is a machine readable description of a web service • Given a WSDL document generate communication stubs (WSDL 2 Java) • Given a Java based web services generate WSDL (Java 2 WSDL) 11

WSDL • • An Interface Definition Language (IDL) Java RMI uses Java Remote Interfaces

WSDL • • An Interface Definition Language (IDL) Java RMI uses Java Remote Interfaces An IDL is needed when languages differ Other example IDL’s Corba IDL (Object-oriented syntax) OSF’s DCE (C like syntax) DCOM IDL based on OSF’s DCE and used by Microsoft’s DCOM • Sun XDR (An IDL for RPC) 12

CORBA IDL example // In file Person. idl struct Person { string name; string

CORBA IDL example // In file Person. idl struct Person { string name; string place; long year; }; interface Person. List { readonly attribute string listname; void add. Person(in Person p) ; void get. Person(in string name, out Person p); long number(); }; 13

File interface in Sun XDR (Originally External Data Representation but now an IDL) const

File interface in Sun XDR (Originally External Data Representation but now an IDL) const MAX = 1000; typedef int File. Identifier; typedef int File. Pointer; typedef int Length; struct Data { int length; char buffer[MAX]; }; struct writeargs { File. Identifier f; File. Pointer position; Data data; }; struct readargs { File. Identifier f; File. Pointer position; Length length; }; program FILEREADWRITE { version VERSION { void WRITE(writeargs)=1; // procedure Data READ(readargs)=2; // numbers }=2; // version number } = 9999; // program number // numbers passed in request message // rpcgen is the interface compiler 14

WSDL • Given a Description of a service (WSDL document) Axis uses wsdl 2

WSDL • Given a Description of a service (WSDL document) Axis uses wsdl 2 java to create the client code • Given a Java service, Axis uses java 2 wsdl to generate a description (WSDL document) • wsdl 2 java is an interface compiler 15

Example Client import org. apache. axis. client. Call; import org. apache. axis. client. Service;

Example Client import org. apache. axis. client. Call; import org. apache. axis. client. Service; import org. apache. axis. encoding. XMLType; import javax. xml. namespace. QName; import javax. xml. rpc. Parameter. Mode; public class Client { public static void main(String [] args) { try { if(args. length != 2) { System. out. println("usage: java Client Big. Int"); System. exit(-1); } 16

Service service = new Service(); Call call = (Call) service. create. Call(); // hold

Service service = new Service(); Call call = (Call) service. create. Call(); // hold data about the service call. set. Target. Endpoint. Address( new java. net. URL("http: //localhost: 8080/axis/servlet/Axis. Servlet") ); call. set. Operation. Name( new QName("Big. Calculator. Service", "add") ); call. add. Parameter( "arg 1", XMLType. XSD_STRING, Parameter. Mode. IN); call. add. Parameter( "arg 2", XMLType. XSD_STRING, Parameter. Mode. IN); call. set. Return. Type( org. apache. axis. encoding. XMLType. XSD_STRING ); // This code does not look like normal // application code! 17

String ret = (String) call. invoke( new Object[] { args[0], args[1] } ); System.

String ret = (String) call. invoke( new Object[] { args[0], args[1] } ); System. out. println("The Big. Calculator Service computed : " + ret); } catch (Exception e) { System. err. println(e. to. String()); } } } 18

On The Server // copy compiled code to the axis/WEB-INF/classes directory import java. math.

On The Server // copy compiled code to the axis/WEB-INF/classes directory import java. math. *; public class Big. Calculator { public String add(String i 1, String i 2) { Big. Integer x = new Big. Integer(i 1); Big. Integer y = new Big. Integer(i 2); return new String(x. add(y). to. String()); } //This code does not look //like normal server side Java! public String subtract(String i 1, String i 2) { Big. Integer x = new Big. Integer(i 1); Big. Integer y = new Big. Integer(i 2); return new String(x. subtract(y). to. String()); } 19

public static void main(String args[]) { Big. Calculator bc = new Big. Calculator(); String

public static void main(String args[]) { Big. Calculator bc = new Big. Calculator(); String a = bc. add("9999999", "1"); System. out. println(a); } } STEPS : javac Big. Calculator. java Copy Big. Calculator. class to D: jwsdp-1. 2webappsaxisWEB-INFclasses Startup Tomcat adminclient deploy. wsdd java org. apache. axis. client. Admin. Client deploy. wsdd Processing file deploy. wsdd <Admin>Done processing</Admin> 20

Web Service Deployment <!-- deploy. wsdd --> <!-- tell axis about this deployment with

Web Service Deployment <!-- deploy. wsdd --> <!-- tell axis about this deployment with java org. apache. axis. client. Admin. Client deploy. wsdd --> <deployment xmlns="http: //xml. apache. org/axis/wsdd/" xmlns: java="http: //xml. apache. org/axis/wsdd/providers/java"> <service name="Big. Calculator. Service" provider="java: RPC"> <parameter name="class. Name" value="Big. Calculator"/> <parameter name="allowed. Methods" value="*"/> </service> </deployment> 21

java Client 1 99999999999999999 The Big. Calculator Service computed : 10000000000000000 But it works.

java Client 1 99999999999999999 The Big. Calculator Service computed : 10000000000000000 But it works. 22

SOAP To Server POST /axis/servlet/Axis. Servlet HTTP/1. 0 Content-Type: text/xml; charset=utf-8 Accept: application/soap+xml, application/dime,

SOAP To Server POST /axis/servlet/Axis. Servlet HTTP/1. 0 Content-Type: text/xml; charset=utf-8 Accept: application/soap+xml, application/dime, multipart/related, text/* User-Agent: Axis/1. 1 Host: 127. 0. 0. 1 Cache-Control: no-cache Pragma: no-cache SOAPAction: "" Content-Length: 520 <? xml version="1. 0" encoding="UTF-8"? > <soapenv: Envelope xmlns: soapenv="http: //schemas. xmlsoap. org/soap/envelope/" xmlns: xsd="http: //www. w 3. org/2001/XMLSchema" xmlns: xsi="http: //www. w 3. org/2001/XMLSchema-instance"> 23

<soapenv: Body> <ns 1: add soapenv: encoding. Style="http: //schemas. xmlsoap. org/soap/encoding/" xmlns: ns 1="Big.

<soapenv: Body> <ns 1: add soapenv: encoding. Style="http: //schemas. xmlsoap. org/soap/encoding/" xmlns: ns 1="Big. Calculator. Service"> <myarg 1 xsi: type="xsd: string">1</myarg 1> <myarg 2 xsi: type="xsd: string">999999999999999999999 </myarg 2> </ns 1: add> </soapenv: Body> </soapenv: Envelope> 24

SOAP Back To Client HTTP/1. 1 200 OK Content-Type: text/xml; charset=utf-8 Date: Mon, 27

SOAP Back To Client HTTP/1. 1 200 OK Content-Type: text/xml; charset=utf-8 Date: Mon, 27 Oct 2003 22: 48: 01 GMT Server: Apache-Coyote/1. 1 Connection: close <? xml version="1. 0" encoding="UTF-8"? > <soapenv: Envelope xmlns: soapenv="http: //schemas. xmlsoap. org/soap/envelope/" xmlns: xsd="http: //www. w 3. org/2001/XMLSchema" xmlns: xsi="http: //www. w 3. org/2001/XMLSchema-instance"> 25

<soapenv: Body> <ns 1: add. Response soapenv: encoding. Style="http: //schemas. xmlsoap. org/soap/encoding/" xmlns: ns

<soapenv: Body> <ns 1: add. Response soapenv: encoding. Style="http: //schemas. xmlsoap. org/soap/encoding/" xmlns: ns 1="Big. Calculator. Service"> <ns 1: add. Return xsi: type="xsd: string"> 1000000000000000000000 </ns 1: add. Return> </ns 1: add. Response> </soapenv: Body> </soapenv: Envelope> 26

Use WSDL 2 Java • From the WSDL document we can generate client side

Use WSDL 2 Java • From the WSDL document we can generate client side code • The WSDL document describes the method names and variable types • WSDL 2 Java is very much like rmic in Java RMI • It allows application programmers to think about applications rather than distributed systems 27

Code Generation using WSDL java org. apache. axis. wsdl. WSDL 2 Java bigcalc. wsdl

Code Generation using WSDL java org. apache. axis. wsdl. WSDL 2 Java bigcalc. wsdl localhost └───axis └───services └───Big. Calculator. Service Big. Calculator. java Big. Calculator. Service. Locator. java Big. Calculator. Service. Soap. Binding. Stub. java 28

Big. Calc. Client. java import localhost. axis. services. Big. Calculator. Service. *; public class

Big. Calc. Client. java import localhost. axis. services. Big. Calculator. Service. *; public class Big. Calc. Client { public static void main(String args[]) throws Exception { Big. Calculator. Service bs = new Big. Calculator. Service. Locator(); Big. Calculator bc = bs. get. Big. Calculator. Service(); String a = new String("1"); String b = new String("99999999"); String c = bc. add(a, b); // This code looks like System. out. println(c); } // application code } java Big. Calc. Client 100000000 29

But We Are Still Working With Strings • Create a server class that works

But We Are Still Working With Strings • Create a server class that works with Big. Integers and not Strings • Place it behind Axis under Tomcat • Generate the WSDL • The client uses the WSDL to generate the client side code • Write the client code 30

import java. math. *; // Server Code // Working with Big. Integers public class

import java. math. *; // Server Code // Working with Big. Integers public class Big. Calculator. Big. Int { public Big. Integer add(Big. Integer i 1, Big. Integer i 2) { return i 1. add(i 2); } public Big. Integer subtract(Big. Integer i 1, Big. Integer i 2) { return i 1. subtract(i 2); } 31

public static void main(String args[]) { Big. Calculator. Big. Int bc = new Big.

public static void main(String args[]) { Big. Calculator. Big. Int bc = new Big. Calculator. Big. Int(); Big. Integer a = bc. add( new Big. Integer("9999999"), new Big. Integer("1")); System. out. println(a); } } 32

Write the Client import localhost. axis. services. Big. Calculator. Big. Integer. Service. *; import

Write the Client import localhost. axis. services. Big. Calculator. Big. Integer. Service. *; import java. math. *; public class Big. Calc. Client { public static void main(String args[]) throws Exception { Big. Calculator. Big. Int. Service bs = new Big. Calculator. Big. Int. Service. Locator(); Big. Calculator. Big. Int bc = bs. get. Big. Calculator. Big. Integer. Service(); 33

Big. Integer a = new Big. Integer("1"); Big. Integer b = new Big. Integer("99999999");

Big. Integer a = new Big. Integer("1"); Big. Integer b = new Big. Integer("99999999"); Big. Integer c = bc. add(a, b); System. out. println(c); } } // Looks even better! 34

SOAP To Server POST /axis/services/Big. Calculator. Big. Integer. Service HTTP/1. 0 Content-Type: text/xml; charset=utf-8

SOAP To Server POST /axis/services/Big. Calculator. Big. Integer. Service HTTP/1. 0 Content-Type: text/xml; charset=utf-8 Accept: application/soap+xml, application/dime, multipart/related, text/* User-Agent: Axis/1. 1 Host: 127. 0. 0. 1 Cache-Control: no-cache Pragma: no-cache SOAPAction: "" Content-Length: 487 35

<? xml version="1. 0" encoding="UTF-8"? > <soapenv: Envelope xmlns: soapenv="http: //schemas. xmlsoap. org/soap/envelope/" xmlns:

<? xml version="1. 0" encoding="UTF-8"? > <soapenv: Envelope xmlns: soapenv="http: //schemas. xmlsoap. org/soap/envelope/" xmlns: xsd="http: //www. w 3. org/2001/XMLSchema" xmlns: xsi="http: //www. w 3. org/2001/XMLSchema-instance"> <soapenv: Body> <ns 1: add soapenv: encoding. Style="http: //schemas. xmlsoap. org/soap/encoding/" xmlns: ns 1="http: //Default. Namespace"> <in 0 xsi: type="xsd: integer">1</in 0> <in 1 xsi: type="xsd: integer">99999999</in 1> </ns 1: add> </soapenv: Body> </soapenv: Envelope> There are 44 simple types available in the XMLShema namespace Integer is defined as an arbitrarily long integer 36

SOAP To Client HTTP/1. 1 200 OK Content-Type: text/xml; charset=utf-8 Date: Tue, 28 Oct

SOAP To Client HTTP/1. 1 200 OK Content-Type: text/xml; charset=utf-8 Date: Tue, 28 Oct 2003 00: 04: 51 GMT Server: Apache-Coyote/1. 1 Connection: close <? xml version="1. 0" encoding="UTF-8"? > <soapenv: Envelope xmlns: soapenv="http: //schemas. xmlsoap. org/soap/envelope/" xmlns: xsd="http: //www. w 3. org/2001/XMLSchema" xmlns: xsi="http: //www. w 3. org/2001/XMLSchema-instance"> <soapenv: Body> <ns 1: add. Response soapenv: encoding. Style="http: //schemas. xmlsoap. org/soap/encoding/" xmlns: ns 1="http: //Default. Namespace"> <ns 1: add. Return xsi: type="xsd: integer">100000000 </ns 1: add. Return> </ns 1: add. Response> 37 </soapenv: Body> </soapenv: Envelope>

Events and Notifications • Examples of the local event model -- a keystroke causes

Events and Notifications • Examples of the local event model -- a keystroke causes an interrupt handler to execute storing a key character in the keyboard buffer -- a mouse click causes an interrupt handler to call a registered listener to handle the mouse event 38

Distributed Event Based Systems • Suppose the whiteboard server is willing to make calls

Distributed Event Based Systems • Suppose the whiteboard server is willing to make calls to all registered clients when the drawing is changed by any one client • Clients may subscribe to this service (register interest) • The whiteboard server publishes the events that it will make available to clients • This is the publish-subscribe paradigm 39

Two Characteristics of Distributed Event Based Systems (1) Heterogeneous -- event generators publish the

Two Characteristics of Distributed Event Based Systems (1) Heterogeneous -- event generators publish the types of events they offer -- other objects subscribe and provide callable methods -- components that were not designed to work together may interoperate 40

Two Characteristics of Distributed Event Based Systems (2) Asynchronous -- Publishers and subscribers are

Two Characteristics of Distributed Event Based Systems (2) Asynchronous -- Publishers and subscribers are decoupled -- notifications of events are sent asynchronously to all subscribers 41

Dealing room system External source Dealer’s computer Notification Information provider Notification Dealer’s computer Notification

Dealing room system External source Dealer’s computer Notification Information provider Notification Dealer’s computer Notification Information provider Notification Dealer External source 42

Architecture for distributed event notification Event service subscriber object of interest 1. notification object

Architecture for distributed event notification Event service subscriber object of interest 1. notification object of interest 2. object of interest observer notification subscriber notification observer 3. subscriber notification Next Topic …. JAXM and Asynchronous Messaging…. 43