Chapter 4. JAX-RPC

Explain the service description model, client connection types, interaction modes, transport mechanisms/protocols, and endpoint types as they relate to JAX-RPC.

JAX-RPC is for Web services interoperability across heterogeneous platforms and languages. This makes JAX-RPC a key technology for Web services integration.

You can use the standard JAX-RPC programming model to develop Web service clients and endpoints based on SOAP. A Web service endpoint is described using a Web Services Description Language (WSDL) document. JAX-RPC enables JAX-RPC clients to invoke Web services developed across heterogeneous platforms. In a similar manner, JAX-RPC Web service endpoints can be invoked by heterogeneous clients. JAX-RPC requires SOAP and WSDL standards for this cross-platform interoperability.

JAX-RPC provides an easy to develop programming model for development of SOAP based Web services. You can use the RPC programming model to develop Web service clients and endpoints. For typical scenarios, you are not exposed to the complexity of the underlying runtime mechanisms (for example, SOAP protocol level mechanisms, marshalling and unmarshalling). A JAX-RPC runtime system (a library) abstracts these runtime mechanisms for the Web services programming model. This simplifies Web service development.

JAX-RPC provides support for WSDL-to-Java and Java-to-WSDL mapping as part of the development of Web service clients and endpoints. In a typical development environment, tools provide these mapping functionality. This further simplifies the application development.

JAX-RPC enables a Web service endpoint to be developed using either a Java Servlet or Enterprise JavaBeans (EJB) component model. A Web service endpoint is deployed on either the Web container or EJB container based on the corresponding component model. These endpoints are described using a WSDL document. This WSDL document can be published in public or private registry, though this is not required. A client uses this WSDL document and invokes the Web service endpoint. A JAX-RPC client can use stubs-based, dynamic proxy or dynamic invocation interface (DII) programming models to invoke a heterogeneous Web service endpoint.

JAX-RPC requires SOAP over HTTP for interoperability. JAX-RPC provides support for SOAP message processing model through the SOAP message handler functionality. This enables developers to build SOAP specific extensions to support security, logging and any other facility based on the SOAP messaging. JAX-RPC uses SAAJ API for SOAP message handlers. SAAJ provides a standard Java API for constructing and manipulating SOAP messages with attachments.

JAX-RPC provides support for document-based messaging. Using JAX-RPC, any MIME-encoded content can be carried as part of a SOAP message with attachments. This enables exchange of XML documents, images and other MIME types across Web services.

JAX-RPC supports HTTP level session management and SSL based security mechanisms. This enables you to develop secure Web services. More advanced SOAP message-level security will be addressed in the evolution of JAX-RPC technology.

Steps for Implementing a Service:

  1. Define Web Service Endpoint Interface

  2. Implement Web Service class and methods

  3. Package and deploy

Endpoint Interface example:

package com.example;

import java.rmi.Remote;
import java.rmi.RemoteException;

public interface HelloIF extends Remote {
	public String sayHello(String s) throws RemoteException;
}					
					

Service endpoint interface [SEI] MUST extend java.rmi.Remote interface. NOTE: Service implementation class MAY not implement SEI (using implements Java reserved word). But it MUST provide all SEI methods implementations with same signatures.

Service endpoint interface's methods MUST throw java.rmi.RemoteException exception. NOTE: Service implementation class' methods MUST NOT throw java.rmi.RemoteException.

Service endpoint interface [SEI] MAY be generated from WSDL (WSDL-to-Java approach).

Web Service can be implemented in form:

  1. Java class (for servlet-based endpoint)

    package com.example;
    
    public class HelloImpl implements HelloIF {
    	public String message = "Hello ";
    	public String sayHello(String s) {
    		return message + s;
    	}
    }								
    								
    NOTE: Your implementation class does not throw RemoteException. This is the responsibility of the container as this implementation will be deployed in a managed J2EE container.

  2. Stateless session bean (for EJB-based endpoint)

    com.example.Greeting is the bean's Web service endpoint interface. It provides the client's view of the Web service, hiding the stateless session bean from the client. A Web service endpoint interface must conform to the rules of a JAX-RPC service definition interface:

    • It extends the java.rmi.Remote interface.

    • It MUST NOT have constant declarations, such as public final static.

    • The methods MUST throw the java.rmi.RemoteException or one of its subclasses. The methods may also throw service-specific exceptions.

    • Method parameters and return types must be supported JAX-RPC types.

    Here is the source code for the com.example.Greeting endpoint interface:

    package com.example;
    
    public interface Greeting extends Remote {
    	public String sayHello(String name) throws RemoteException;
    }								
    								

    The com.example.GreetingBean class implements the sayHello method defined by the com.example.Greeting interface. The interface decouples the implementation class from the type of client access. For example, if you added remote and home interfaces to com.example.GreetingBean, the methods of the com.example.GreetingBean class could also be accessed by remote clients. No changes to the com.example.GreetingBean class would be necessary.

    The source code for the com.example.GreetingBean Stateless Session Bean Class follows:

    package com.example;
    
    import javax.ejb.SessionBean;
    import javax.ejb.SessionContext;
    import java.rmi.RemoteException;
    								
    public class GreetingBean implements SessionBean {
    	public String sayHello(String name) {
    		return "Hello" + name;
    	}
    	
    	public void ejbCreate() {    
    	}
    	
    	// Standard callback methods  
    	public void ejbActivate() {
    	}
    
    	public void ejbPassivate() {
    	}
    
    	public void ejbRemove() {    
    	}
    
    	public void setSessionContext(SessionContext ctx) {
    	}	
    }								
    								
    Deployment Descriptor:
    
    <?xml version='1.0' encoding='UTF-8'?>
    
    <ejb-jar version="2.1" xmlns="http://java.sun.com/xml/ns/j2ee"                        
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"                        
    	xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee                                            
    			http://java.sun.com/xml/ns/j2ee/ejb-jar_2_1.xsd"> 
    	
    	<enterprise-beans>
    		<session>
    			<ejb-name>GreetingBean</ejb-name>
    			<service-endpoint>com.example.Greeting</service-endpoint>
    			<ejb-class>com.example.GreetingBean</ejb-class>
    			<session-type>Stateless</session-type>
    			<transaction-type>Container</transaction-type>      
    			<security-identity>        
    				<use-caller-identity/>      
    			</security-identity>
    		</session>
    	</enterprise-beans>
    	
    </ejb-jar>	
    								
    								
    								

Professional hosting     Belorussian informational portal         Free SCWCD 1.4 Study Guide     Free SCBCD 1.3 Study Guide     SCDJWS 1.4 Quiz     Free IBM Certified Associate Developer Study Guide     Free SCJP 5.0 (Tiger) Study Guide     Free Mock Exam Engine     IBM Test 000-287. Enterprise Application Development with IBM WebSphere Studio, V5.0 Study Guide     IBM Test 000-255. Developing with IBM Rational Application Developer for WebSphere Software V6 Study Guide