JAXRPC The Java API for XML Remote Procedure

  • Slides: 27
Download presentation
JAX-RPC The Java API for XML Remote Procedure Calls Notes from The J 2

JAX-RPC The Java API for XML Remote Procedure Calls Notes from The J 2 EE Tutorial 1. 4 From Sun Microsystems

JAX-RPC Notes • Based on W 3 C Technologies • SOAP • WSDL •

JAX-RPC Notes • Based on W 3 C Technologies • SOAP • WSDL • HTTP • Java types are mapped to XML/WSDL • String to xsd: string • Big. Decimal[] and more… • Primitive types

JAX-RPC Server Side The service endpoint interface package helloservice; import java. rmi. Remote; Import

JAX-RPC Server Side The service endpoint interface package helloservice; import java. rmi. Remote; Import java. rmi. Remote. Exception; public interface Hello. IF extends Remote { public String say. Hello(String s) throws Remote. Exception; }

Implement the service package helloservice; public class Hello. Impl implements Hello. IF { public

Implement the service package helloservice; public class Hello. Impl implements Hello. IF { public String message ="Hello"; public String say. Hello(String s) { return message + s; } }

Provide a config-interface. xml <? xml version="1. 0" encoding="UTF-8"? > <configuration xmlns="http: //java. sun.

Provide a config-interface. xml <? xml version="1. 0" encoding="UTF-8"? > <configuration xmlns="http: //java. sun. com/xml/ns/jax-rpc/ri/config"> <service name="My. Hello. Service" target. Namespace="urn: Foo" type. Namespace="urn: Foo" package. Name="helloservice"> <interface name="helloservice. Hello. IF"/> </service> </configuration>

Three steps to build 1. compile-service 2. generate-wsdl 3. generate-mapping from service classes package

Three steps to build 1. compile-service 2. generate-wsdl 3. generate-mapping from service classes package names to namespace URI’s in the WSDL and create ties (skeletons) J 2 EE 1. 4 provides an ant task to perform all three steps

Behind the Scenes • The JAX-RPC web service is actually a servlet and placed

Behind the Scenes • The JAX-RPC web service is actually a servlet and placed in the web tier

On the client side We’ll examine four options 1. 2. 3. 4. Static stubs

On the client side We’ll examine four options 1. 2. 3. 4. Static stubs compiled by wscompile before runtime Dynamic stubs has an interface but fetches the WSDL at runtime Dynamic Invocation Interface knows no interface - the method names and signatures A J 2 EE Application Client Locate the local web service with JNDI

Client Using a Static Stub import javax. xml. rpc. Stub; It almost looks like

Client Using a Static Stub import javax. xml. rpc. Stub; It almost looks like a local call. public class Hello. Client { private String endpoint. Address; public static void main(String[] args) { System. out. println("Endpoint address = " + args[0]);

try { Stub stub = create. Proxy(); stub. _set. Property (javax. xml. rpc. Stub.

try { Stub stub = create. Proxy(); stub. _set. Property (javax. xml. rpc. Stub. ENDPOINT_ADDRESS_PROPERTY args[0]); // cast the stub to the proper interface Hello. IF hello = (Hello. IF)stub; // call the methods defined there System. out. println(hello. say. Hello("Duke!")); } catch (Exception ex) { ex. print. Stack. Trace(); } }

private static Stub create. Proxy() { // My. Hello. Service_Impl is created by wscompile

private static Stub create. Proxy() { // My. Hello. Service_Impl is created by wscompile return (Stub) (new My. Hello. Service_Impl(). get. Hello. IFPort()); } }

Using a Dynamic Stub public class Hello. Client { // We have more work

Using a Dynamic Stub public class Hello. Client { // We have more work to do public static void main(String[] args) { try { String Url. String = args[0] + "? WSDL"; String name. Space. Uri = "urn: Foo"; String service. Name = "My. Hello. Service"; String port. Name = "Hello. IFPort";

System. out. println("Url. String = " + Url. String); URL hello. Wsdl. Url =

System. out. println("Url. String = " + Url. String); URL hello. Wsdl. Url = new URL(Url. String); Service. Factory service. Factory = Service. Factory. new. Instance(); Service hello. Service = service. Factory. create. Service(hello. Wsdl. Url, new QName(name. Space. Uri, service. Name)); WSDL is fetched at runtime

dynamicproxy. Hello. IF my. Proxy = (dynamicproxy. Hello. IF) hello. Service. get. Port( new

dynamicproxy. Hello. IF my. Proxy = (dynamicproxy. Hello. IF) hello. Service. get. Port( new QName(name. Space. Uri, port. Name), dynamicproxy. Hello. IF. class); System. out. println(my. Proxy. say. Hello("Buzz")); } catch (Exception ex) { ex. print. Stack. Trace(); } } }

Client Using a Dynamic Invocation Interface import javax. xml. rpc. Call; import javax. xml.

Client Using a Dynamic Invocation Interface import javax. xml. rpc. Call; import javax. xml. rpc. Service; import javax. xml. rpc. JAXRPCException; import javax. xml. namespace. QName; import javax. xml. rpc. Service. Factory; import javax. xml. rpc. Parameter. Mode;

public class Hello. Client { private static String qname. Service = "My. Hello. Service";

public class Hello. Client { private static String qname. Service = "My. Hello. Service"; private static String qname. Port = "Hello. IF"; private static String BODY_NAMESPACE_VALUE = "urn: Foo"; private static String ENCODING_STYLE_PROPERTY = "javax. xml. rpc. encodingstyle. namespace. uri"; private static String NS_XSD = "http: //www. w 3. org/2001/XMLSchema"; private static String URI_ENCODING = "http: //schemas. xmlsoap. org/soap/encoding/";

public static void main(String[] args) { System. out. println("Endpoint address = " + args[0]);

public static void main(String[] args) { System. out. println("Endpoint address = " + args[0]); try { Service. Factory factory = Service. Factory. new. Instance(); Service service = factory. create. Service( new QName(qname. Service)); “My. Hello. Service” Hello. IF QName port = new QName(qname. Port); The URL and service names are all specified at runtime

Who we Call call = service. create. Call(port); are calling and call. set. Target.

Who we Call call = service. create. Call(port); are calling and call. set. Target. Endpoint. Address(args[0]); how is determined at call. set. Property(Call. SOAPACTION_USE_PROPERTY, runtime new Boolean(true)); call. set. Property(Call. SOAPACTION_URI_PROPERTY ""); call. set. Property(ENCODING_STYLE_PROPERTY, URI_ENCODING); QName QNAME_TYPE_STRING = new QName(NS_XSD, "string"); SOAP call. set. Return. Type(QNAME_TYPE_STRING); details must be specified

Very carefully prepare the call. set. Operation. Name( new QName(BODY_NAMESPACE_VALUE, "say. Hello")); call. add.

Very carefully prepare the call. set. Operation. Name( new QName(BODY_NAMESPACE_VALUE, "say. Hello")); call. add. Parameter("String_1", QNAME_TYPE_STRING, Parameter. Mode. IN); String[] params = { "Murph!" }; String result = (String)call. invoke(params); System. out. println(result); Make the call } catch (Exception ex) { Get the result ex. print. Stack. Trace(); } } } Harder to program but allows for a great deal of flexibility. There is a lot less hard coding going on here. A lot of this could be chosen by a sophisticated user at runtime

J 2 EE Application Client import javax. xml. rpc. Stub; import javax. naming. *;

J 2 EE Application Client import javax. xml. rpc. Stub; import javax. naming. *; public class Hello. Client { private String endpoint. Address; public static void main(String[] args) { System. out. println("Endpoint address = " + args[0]); try { Context ic = new Initial. Context(); My. Hello. Service my. Hello. Service = (My. Hello. Service) ic. lookup("java: comp/env/service/My. JAXRPCHello"); Ask JNDI for a reference to a stub for the object

Hello. IF hello. Port = my. Hello. Service. get. Hello. IFPort(); ((Stub)hello. Port). _set.

Hello. IF hello. Port = my. Hello. Service. get. Hello. IFPort(); ((Stub)hello. Port). _set. Property (Stub. ENDPOINT_ADDRESS_PROPERTY, args[0]); System. out. println(hello. Port. say. Hello("Jake!")); System. exit(0); } catch (Exception ex) { ex. print. Stack. Trace(); System. exit(1); } } }

From The J 2 EE Tutorial

From The J 2 EE Tutorial

From The J 2 EE Tutorial

From The J 2 EE Tutorial

The Web Service as an EJB • A Web service client can access J

The Web Service as an EJB • A Web service client can access J 2 EE applications in two ways. • First, the client can access a Web service created with JAX-RPC. Behind the scenes, JAXRPC uses a servlet to implement the Web service. • Second, a Web service client can access a stateless session bean through the service endpoint interface of the bean. Other types of enterprise beans cannot be accessed by Web service clients.

A Stateless Session Bean as a Web Service • The client need not know

A Stateless Session Bean as a Web Service • The client need not know that its interacting with a Java EJB • It calls the bean like it calls any other web service

The Web Service Endpoint Interface The client cannot see that it’s interacting with an

The Web Service Endpoint Interface The client cannot see that it’s interacting with an EJB package helloservice; import java. rmi. Remote. Exception; import java. rmi. Remote; public interface Hello. IF extends Remote { public String say. Hello(String name) throws Remote. Exception; }

The Web Service Session Bean package helloservice; import java. rmi. Remote. Exception; import javax.

The Web Service Session Bean package helloservice; import java. rmi. Remote. Exception; import javax. ejb. Session. Bean; import javax. ejb. Session. Context; If we added remote and home Interfaces then this bean could also be called using in the traditional manner – with remote references. No change to the bean would be necessary. public class Hello. Service. Bean implements Session. Bean { public String say. Hello(String name) { return "Hello " + name + "from Hello. Service. EJB"; } public Hello. Service. Bean() {} WSDL can be generated and all public void ejb. Create() {} of the previous clients will work. public void ejb. Remove() {} public void ejb. Activate() {} public void ejb. Passivate() {} public void set. Session. Context(Session. Context sc) {} }