View
214
Download
0
Category
Preview:
Citation preview
8/14/2019 Web Service J2EE
1/27
3 modulo: Web Service con JAX-RPC
Massimiliano de Leoni
Advanced Middleware: aspetti evolutivi a
supporto di sistemi distribuiti e
interoperabilit
8/14/2019 Web Service J2EE
2/27
JAX-RPC (1)
JAX-RPC stands for Java API for XML-based RPC.JAX-RPC is a technology for building Web services
and clients that use remote procedure calls(RPC)and XML.
In JAX-RPC, a remote procedure call is represented
by an XML-based protocol such as SOAP. The SOAPspecification defines the envelope structure,encoding rules, and conventions for representingremote procedure calls and responses.
These calls and responses are transmitted as SOAPmessages (XML files) over HTTP.
8/14/2019 Web Service J2EE
3/27
8/14/2019 Web Service J2EE
4/27
JAX-RPC (3)
Although SOAP messages are complex, the JAX-RPC API hides this complexity from the application
developer. With JAX-RPC, the developer does not generate or
parse SOAP messages. It is the JAX-RPC runtime
system that converts the API calls and responses toand from SOAP messages.
With JAX-RPC, clients and Web services have a bigadvantage: the platform independence of the Javaprogramming language.
8/14/2019 Web Service J2EE
5/27
JAX-RPC (4)
In addition, JAX-RPC is not restrictive: a JAX-RPCclient can access a Web service that is not runningon the Java platform, and vice versa.
This flexibility is possible because JAX-RPC usestechnologies defined by the World Wide WebConsortium (W3C): HTTP, SOAP, and the WebService Description Language (WSDL). WSDLspecifies an XML format for describing a service as a
set of endpoints operating on messages.
8/14/2019 Web Service J2EE
6/27
JAX-RPC Web service
The starting point for developing a JAX-RPC Web service is theservice endpoint interface.
A service endpoint interface(SEI) is a Java interface thatdeclares the methods that a client can invoke on the service.
You use the SEI, the wscompile tool, and two configuration filesto generate the WSDL specification of the Web service and thestubs that connect a Web service client to the JAX-RPC runtime.
Together, the wscompile tool, the deploytool utility, and theApplication Server provide the Application Serversimplementation of JAX-RPC.
8/14/2019 Web Service J2EE
7/27
Steps for creating the Web service
and client: Server
1. Code the SEI and implementation class and interface
configuration file.2. Compile the SEI and implementation class.3. Use wscompile to generate the files required to deploy the
service tra cui il WSDL.4. Use deploytool to package the files into a WAR file.5. Deploy the WAR file. The tie classes (which are used to
communicate with clients) are generated by the ApplicationServer during deployment.
Client1. Code the client class2. Use wscompile to generate and compile the stub files.3. Compile the client class.4. Run the client.
8/14/2019 Web Service J2EE
8/27
Coding the SEI and Implementation
Class A service endpoint interface must conform to a few
rules:
It extends the java.rmi.Remote interface.
It must not have constant declarations, such as publicfinal static.
The methods must throw thejava.rmi.RemoteException or one of its
subclasses.
Method parameters and return types must be supportedJAX-RPC types (Tutorial Pages 332-334)
8/14/2019 Web Service J2EE
9/27
Telefono Example: SEI
The service endpoint interface is named TelefonoIF
It declares a single method named
public String getNumero(String abbonato). This method returns a string that is the concatenation of the
string Hello with the method parameter.
package Telefono;
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface TelefonoIF extends Remote {
public String getNumero(String abbonato)throws RemoteException;
}
8/14/2019 Web Service J2EE
10/27
Telefono Example: Class
In addition to the interface, youll need the class that implements the interface. Inthis example, the implementation class is called TelefonoImpl:
package Telefono;
import java.rmi.Remote;import java.rmi.RemoteException;
public class TelefonoImpl implements TelefonoIF{private static String elenco[][] = {...}
public String getNumero(String abbonato){for (int i=0;i
8/14/2019 Web Service J2EE
11/27
The wscompile tool / 1
The tool wscompile creates the WSDL and mappingfiles. The WSDL file describes the Web service and isused to generate the client stubs in Static Stub Client.
The mapping file contains information that correlatesthe mapping between the Java interfaces and theWSDL definition.
The files created in this example areMyService.wsdl and mapping.xml
8/14/2019 Web Service J2EE
12/27
The wscompile tool / 2
wscompile is run with the following arguments:
wscompile -define -mapping mapping.xml-classpath . config.xml
The -classpath flag instructs wscompile to read the SEI in
current directory, and the -define flag instructs wscompile tocreate WSDL and mapping files.
The -mapping flag specifies the mapping file name.
8/14/2019 Web Service J2EE
13/27
The wscompile tool / 3
The wscompile tool reads an interface configuration file thatspecifies information about the SEI. In this example, the
configuration file is named config.xml and contains thefollowing:
8/14/2019 Web Service J2EE
14/27
The wscompile tool / 4
This configuration file tells wscompile to create aWSDL file named MyService.wsdl with thefollowing information:
The service name is MyService.
The WSDL target and type namespace is urn:Foo.
The SEI is Telefono.Telefono.
The packageName attribute instructs wscompile toput the service classes into the Telefono package.
8/14/2019 Web Service J2EE
15/27
Packaging and Deploying the
Service Behind the scenes, a JAX-RPC Web service is
implemented as a Servlet. Because a Servlet is aWeb component, you run the New Web Componentwizard of the deploytool utility to package theservice.
During this process the wizard performs the followingtasks:
Creates the Web application deployment descriptor
Creates a WAR file
Adds the deployment descriptor and service files to theWAR file
8/14/2019 Web Service J2EE
16/27
Packaging / 1
To start the New Web Component wizard, select FileNewWebComponent.
1. Introduction dialog box1. Read the explanatory text for an overview of the wizards features.2. Click Next.
2. WAR File dialog box1. Select the button labeled Create New Stand-Alone WAR Module.
2. In the WAR Location field, click Browsea. navigate to whichever position you want to create WARb. In the File Name field, enter TelefonoService.c. Click Create Module File.
3. Click Edit Contents to add WSDL, mapping and class files to WAR
a. Select WSDL and mapping file and click onAddb. Select Telefono directory (where class files are located) and click onAdd
4. Click OK.5. Click Next.
8/14/2019 Web Service J2EE
17/27
Packaging / 2
3. Component Type dialog box1. Select the Web Services Endpoint button.
2. Click Next.4. Service dialog box
1. In the WSDL File combo box, select WEB-INF/wsdl/MyService.wsdl.
2. In the Mapping File combo box, selectmapping.xml.
3. Click Next.
5. Component General Properties dialog box1. In the Service Endpoint Implementation combo box,select Telefono.TelefonoImpl.
2. Click Next.
8/14/2019 Web Service J2EE
18/27
Packaging / 3
6. Web Service Endpoint dialog box
1. In the Service Endpoint Interface combo box, selectTelefono.Telefono.
2. In the Namespace combo box, select urn:Foo.
3. In the Local Part combo box, select TelefonoPort.
The deploytool utility will enter a default EndpointAddress URI TelefonoImpl in this dialog. This
endpoint address mustbe updated in the next
section.4. Click Next.
5. Click Finish.
8/14/2019 Web Service J2EE
19/27
Specifying the Endpoint Address
To access to service, We want client to specify this serviceendpoint address URI: http://localhost:8080/alcatel/telefono
The /hello-jaxrpc string is the context root of the servletthat implements the service. The /telefono string is theservlet alias. To specify the endpoint address, you set thecontext root and alias as follows:
1. In deploytool, select TelefonoService in the tree.2. Select the General tab and in the Context Root
field, enter alcatel.3. In the tree, select TelefonoImpl and then select the
Aliases tab.4. In the component Aliases table, add /telefono.5. In the Endpoint tab, select telefono for the Endpoint
Address in the Sun-specific Settings frame.6. Select FileSave.
8/14/2019 Web Service J2EE
20/27
Deploying the Service
In DeployTool, perform these steps:
1. In the tree, select TelefonoService.2. Select ToolsDeploy.
3. You can view the WSDL file of the deployed
service by requesting the URL4. http://localhost:8080/alcatel/tel
efono?WSDL in a Web browser.
8/14/2019 Web Service J2EE
21/27
Static Stub Client
TelefonoClient is a stand-alone programthat calls the getNumero method of thedeployed service.
It makes this call through a stub, a local object
that acts as a proxy for the remote service. Because the stub is created by wscompile atdevelopment time (as opposed to runtime), it
is usually called a static stub.
8/14/2019 Web Service J2EE
22/27
The wscompile tool (client side) / 1
The wscompile tool creates the stub with thefollowing arguments:
wscompile -gen:client -classpath .config.xml
The -gen:client flag instructs wscompile to
generate the stubs, other runtime files such asserializers, and value types.
The config.xml file contains the following:
The packageName attribute specifies the Javapackage for the generated stubs.
8/14/2019 Web Service J2EE
23/27
The wscompile tool (client side) / 2
The packageName attribute specifies the
Java package for the generated stubs. Because -classpath . has been specified,
this example will produce stubs file in
staticstub sub-directory starting fromcurrent directory
Notice that the location of the WSDL file is
specified as a URL. This causes thewscompile command to request the WSDL
file from the Web service.
8/14/2019 Web Service J2EE
24/27
TelefonoClients code
import staticstub.*;import javax.swing.*;
public class HelloClient {public static void main(String[] args){
try{TelefonoIF stub=
(TelefonoIF)(new MyService_Impl().getPort(TelefonoIF.class));String abbonato=(String)JOptionPane.showInputDialog(null,"Specificare il nome di un abbonato:");
if (abbonato==null)return;
String numero=stub.getNumero(abbonato);
JOptionPane.showMessageDialog(null,numero,"Il numero richiesto",JOptionPane.INFORMATION_MESSAGE);
System.exit(0);}catch (Exception ex) { ex.printStackTrace(); }
}}
8/14/2019 Web Service J2EE
25/27
Compiling code
In order to compile code, classpath mustcontains some jar filelocated in libdirectory of application server. They are the
following ones: jaxrpc-impl.jar
jax-qname.jar
jaxrpc-api.jar
So, classpath environment has to be updated as a consequenceor it can be specified at compile-time as compiler parameter:
javac -classpath C:\Sun\AppServer\lib\jaxrpc-
impl.jar;.;c:\Sun\AppServer\lib\jax-qname.jar;c:\Sun\AppServer\lib\jaxrpc-api.jarTelefonoClient.java
8/14/2019 Web Service J2EE
26/27
Packaging (client)
To start the New Application Client wizard, select FileNewApplicationClient.
1. Introduction dialog box
1. Read the explanatory text for an overview of the wizards features.2. Click Next.
2. JAR File content box1. Select the button labeled Create New Stand-Alone AppClient Module.
2. In the AppClient Location field, click Browsea. navigate to whichever position you want to create JARb. In the File Name field, enter client.jarc. Click Create Module File.
3. Click Edit Contents to add class files to JARa. Select staticstub directory (where client stub class files are located) and click
onAddb.
Select TelefonoClient file and click onAdd4. Click Next.
3. General box1. Select TelefonoClient as Main Class
2. Click Next.
4. Click Finish
8/14/2019 Web Service J2EE
27/27
Starting web client
Move in directory where client.jar is
located and here execute the followingcommand:
appclient jar client.jar
Recommended