Java_J2EE_Job_Interview_Companion
.pdf302 |
How would you go about …? |
•Adapts an application to changing business needs and technological changes due to well-defined interfaces.
•Easily integrates applications with other systems in a loosely coupled manner.
•Reuse and improved asset utilization by leveraging existing investments in legacy applications.
•Business agility by quickly and easily creating a business process by leveraging on existing services.
•Provides interoperability and multi channel access.
Q. What is the difference between SOA and a Web service?
SOA (Service Oriented Architecture) |
Web services |
SOA is a software design principle and an architectural |
Web service is an implementation technology and one of the ways to |
pattern for implementing loosely coupled, reusable and |
implement SOA. You can build SOA based applications without using Web |
coarse grained services. |
services – for example by using other traditional technologies like Java |
|
RMI, EJB, JMS based messaging, etc. But what Web services offer is the |
|
standards based and platform-independent service via HTTP, XML, |
|
SOAP, WSDL and UDDI, thus allowing interoperability between |
|
heterogeneous technologies such as J2EE and .NET. |
You can implement SOA using any protocols such as |
Interfaces must be based on Internet protocols such as HTTP, FTP and |
HTTP, HTTPS, JMS, SMTP, RMI, IIOP (i.e. EJB uses |
SMTP. There are two main styles of Web services: SOAP and REST. |
IIOP), RPC etc. Messages can be in XML or Data |
Messages must be in XML and binary data attachments. |
Transfer Objects (DTOs). |
|
Q. What is the difference between a Web (website) and a Web service?
|
Web (website) |
|
Web Service |
|||||
|
A Web is a scalable information space with |
|
A Web service is a service, which lives on the Web. A Web service posses both the |
|||||
|
interconnected resources. A Web |
|
characteristics of a Web and a service. We know what a Web is; let’s look at what a |
|||||
|
interconnects resources like Web pages, |
|
service is? |
|||||
|
images, an application, word document, e-mail |
|
A service is an application that exposes its functionality through an API (Application |
|||||
|
etc. |
|
||||||
|
|
|
|
Programming Interface). So what is a component you may ask? A service is a |
||||
|
|
|
|
component that can be used remotely through a remote interface either |
||||
|
|
|
|
synchronously or asynchronously. The term service also implies something special |
||||
|
|
|
|
about the application design, which is called a service-oriented architecture |
||||
|
|
|
|
(SOA). One of the most important features of SOA is the separation of interface |
||||
|
|
|
|
from implementation. A service exposes its functionality through interface and |
||||
|
|
|
|
interface hides the inner workings of the implementation. The client application (i.e. |
||||
|
|
|
|
user of the service) only needs to know how to use the interface. The client does not |
||||
|
|
|
|
have to understand actually how the service does its work. For |
example: |
There are |
||
|
|
|
|
so many different models of cars like MAZDA, HONDA, TOYOTA |
|
|
||
|
|
|
|
etc using different |
||||
|
|
|
|
types of engines, motors etc but as a user or driver of the car you do not have to be |
||||
|
|
|
|
concerned about the internals. You only need to know how to start the car, use the |
||||
|
|
|
|
steering wheel etc, which is the interface to you. |
||||
|
|
|
|
Usually a service runs on a server, waiting for the client application to call it and ask |
||||
|
|
|
|
to do some work? These services are often run on application servers, which |
||||
|
|
|
|
manage scalability, availability, reliability, multi-threading, transactions, security etc. |
||||
|
|
|
|
|||||
|
Designed to be consumed by humans (i.e. |
|
Designed to be consumed by software (i.e. other applications). |
|||||
|
users, clients, business partners etc). For |
|
|
|
|
|
|
|
|
example: |
www.google.com is a Web search |
|
For |
example: |
Google also provides a Web service interface through the Google API |
||
|
|
|
|
|
|
search engine from an application rather than a browser. |
||
|
engine that contains index to more than 8 |
|
to query their |
|||||
|
billion of other Web pages. The normal |
|
Refer http://www.google.com/apis/ for Google Web API |
|||||
|
interface is a Web browser like Internet |
|
|
|
|
|
|
|
|
Explorer, which is used by human. |
|
|
|
|
|
|
Q. Why use Web services when you can use traditional style middleware such as RPC, CORBA, RMI and DCOM?
Traditional middleware Web Services
Tightly coupled connections to the application |
Web Services support loosely coupled connections. The interface of the |
and it can break if you make any modification to your |
Web service provides a layer of abstraction between the client and the server. |
application. Tightly coupled applications are hard to |
The loosely coupled applications reduce the cost of maintenance and |
maintain and less reusable. |
increases reusability. |
|
|
Generally does not support heterogeneity. |
Web Services present a new form of middleware based on XML and Web. |
|
Web services are language and platform independent. You can develop a |
|
Web service using any language and deploy it on to any platform, from small |
|
device to the largest supercomputer. Web service uses language neutral |
|
protocols such as HTTP and communicates between disparate |
|
applications by passing XML messages to each other via a Web API. |
|
|
Does not work across Internet. |
Does work across Internet. |
More expensive and hard to use. |
Less expensive and easier to use. |
304 |
|
How would you go about …? |
|
|
|
|
|
|
|
... |
|
|
|
<soap:Fault> |
|
|
|
... |
|
|
|
... |
|
|
|
</soap:Fault> |
|
|
|
</soap:Body> |
|
|
|
</soap:Envelope> |
|
|
|
Let’s look at a SOAP request and a SOAP response: |
|
|
|
SOAP Request: |
|
|
|
|
|
|
|
POST /Price HTTP/1.1 |
|
|
|
Host: www.mysite.com |
|
|
|
Content-Type: application/soap+xml; charset=utf-8 |
|
|
|
Content-Length: 300 |
|
|
|
<?xml version="1.0"?> |
|
|
|
<soap:Envelope |
|
|
|
xmlns:soap="http://www.w3.org/2001/12/soap-envelope" |
|
|
|
soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding"> |
|
|
|
<soap:Body> |
|
|
|
<m:GetPrice xmlns:m="http://www.mysite.com/prices"> |
|
|
|
<m:Item>PlasmaTV</m:Item> |
|
|
|
</m:GetPrice> |
|
|
|
</soap:Body> |
|
|
|
</soap:Envelope> |
|
|
|
SOAP Response: |
|
|
|
|
|
|
|
HTTP/1.1 200 OK |
|
|
|
Content-Type: application/soap; charset=utf-8 |
|
|
|
Content-Length: 200 |
|
|
|
<?xml version="1.0"?> |
|
|
|
<soap:Envelope |
|
|
|
xmlns:soap="http://www.w3.org/2001/12/soap-envelope" |
|
|
|
soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding"> |
|
|
|
<soap:Body> |
|
|
|
<m:GetPriceResponse |
|
|
|
xmlns:m="http://www.mysite.com/prices"> |
|
|
|
<m:Price>3500.00</m:Price> |
|
|
|
</m:GetPriceResponse> |
|
|
|
</soap:Body> |
|
|
|
</soap:Envelope> |
|
|
|
Let’s look at a HTTP header: |
|
|
|
|
|
|
|
POST /Price HTTP/1.1 |
|
|
|
Host: www.mysite.com |
|
|
|
Content-Type: text/plain |
|
|
|
Content-Length: 200 |
|
|
|
SOAP HTTP Binding |
|
|
|
A SOAP method is an HTTP request/response that complies with the SOAP encoding rules. |
|
|
|
|
|
|
|
HTTP + XML = SOAP |
|
|
|
Let’s look at a HTTP header containing a soap message: |
|
|
|
|
|
|
|
POST /Price HTTP/1.1 |
|
|
|
Host: www.mysite.com |
|
|
|
Content-Type: application/soap+xml; charset=utf-8 |
|
|
|
Content-Length: 200 |
|
|
|
|
|
WSDL (Web |
|
WSDL stands for Web Services Description Language. A WSDL document is an XML document that describes how |
|
Services |
|
the messages are exchanged. Let’s say we have created a Web service. Who is going to use that and how does the |
|
Description |
|
client know which method to invoke and what parameters to pass? There are tools that can generate WSDL from the |
|
Language) |
|
Web service. Also there are tools that can read a WSDL document and create the necessary code to invoke the Web |
|
|
|
service. So the WSDL is the Interface Definition Language (IDL) for Web services. |
|
|
|
|
|
UDDI |
|
UDDI stands for Universal Description Discovery and Integration. UDDI provides a way to publish and discover |
|
(Universal |
|
information about Web services. UDDI is like a registry rather than a repository. A registry contains only reference |
|
306 |
How would you go about …? |
JAX-RPC JAX-RPC (Java API for XML based RPC) supports XML based RPC for Java and J2EE platforms. JAX-RPC provides an easy to develop programming model to develop Web services. As shown in the diagram above, a JAX-RPC runtime system and API abstracts the complexities of SOAP protocol by :
•Providing a standard way of marshalling Java to XML and Java to WSDL and unmarshalling XML to Java and WSDL to Java.
•Standardizing the creation of SOAP requests and responses.
•Supporting and dispatching SOAP requests to methods on JAX-RPC Service Endpoint classes in the Web Container.
•Specifying a standard way to plug in SOAP message handlers, allowing both pre and post processing of SOAP requests and responses.
Q.What are JAX-RPC message handlers?
A.The JAX-RPC message handlers are similar to servlet filters. They provide additional messagehandling facilities to Web service endpoints (both client and server) as extensions to the basic service implementation logic by providing logging, auditing, encryption, decryption etc.
|
|
|
W e b C o n ta in e r |
|
|||||
|
S O A P /H T T P |
|
|
|
|
|
|
|
|
|
H T T P /S S L |
S e rv le ts |
|
J S P |
|
P o rts |
JAX-RPC |
||
|
I |
RMI/IIOP |
JNDI |
JTA |
JDBC |
JMS |
JavaMail |
JAF |
|
|
|
|
|
|
|
|
|
|
|
|
n |
|
|
|
|
|
|
|
|
Client |
t |
|
|
|
|
|
|
|
|
e |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
r |
|
R M I / |
|
|
S O A P / |
|
||
|
n |
|
IIO P |
|
|
H T T P |
|
||
|
e |
|
|
|
|
|
|
|
|
|
t |
|
E J B C o n ta in e r |
|
|||||
|
|
|
|
||||||
|
S O A P /H T T P |
|
|
|
|
|
|
|
|
|
|
|
E J B s |
|
P o rts |
|
JAX-RPC |
||
|
|
RMI/IIOP |
JNDI |
JTA |
JDBC |
JMS |
JavaMail |
JAF |
Q.How does a client view(or accesses) a Web service ? What is a Web service port?
A.A client views each J2EE Web service as an SEI (Service Endpoint Interface) and port combination. A single J2EE container can host many combinations of SEI + port and each SEI+port combination is an instance of a Web service. Ports are effectively instances of Web services similar in concept to instances in a JVM. A port’s lifecycle is governed by the container it runs in. The container is responsible for creating, initializing, destroying or recreating the port. Web service ports are stateless and the container may decide to destroy and recreate the port across the same client’s invocations.
Q.How do you implement a Web service in a J2EE environment?
A.A Web service on the J2EE platform may be implemented using a JAX-RPC sevice endpoint or using a EJB service endpoint.
•JAX-RPC sevice endpoint - The sevice implementation is a plain Java class deployed in the Web container. The service adheres to the Web container’s servlet life cycle and concurrency requirements. The service implementation class run like a servlet inside the Web container but it does not need to extend the Servlet or HttpServlet class.
•EJB service endpoint – The service implementation is a stateless session bean in an EJB container. The service adheres to the EJB container’s lifecycle and concurrency requirements.
How would you go about …? |
307 |
|
Web container – JAX-RPC Endpoint |
EJB container – EJB Endpoints |
|
Hello.war |
Hello.jar |
|
Hello.war |
Hello.jar |
|
|
|
WEB-INF |
|
META-INF |
com.HelloImpl.class |
|
|
|
|
|
|
classes |
mapping.xml |
|
|
|
|
com.Hello.class |
ejb-jar.xml |
|
|
|
|
mapping.xml |
webservices.xml |
|
|
|
|
web.xml |
HelloService.wsdl |
|
|
|
|
webservices.xml |
wsdl |
|
|
|
|
|
com.HelloBean.class |
|
HelloService.wsdl |
|
|
wsdl |
com.Hello.class |
|
|
Build a class that implements a business method, which |
|
Build a stateless session bean with the service method |
can be exposed as a Web service. This class does not |
|
you want to expose. If your stateless session bean is |
have to extend the Servlet or HttpServlet class. |
|
going to serve only as a Web service then you can omit |
|
|
home interface, remote interface, local interface etc. |
package com; |
|
|
public class HelloImpl { |
|
package com; |
|
|
|
… |
|
public class HelloBean implements SessionBean { |
public void howAreYou(String name) { |
|
… |
log.info ( “Fine thank you ” + name) ; |
|
public void howAreYou(String name) { |
} |
|
log.info( “Fine thank you ” + name) ; |
} |
|
} |
|
|
public void ejbCeate(){} |
|
||
|
|
public void ejbRemove(){} |
|
|
public void ejbActivate(){} |
|
|
public void ejbPassivate(){} |
|
|
} |
|
|
|
Build a remote interface for the service that declares all the methods that the service exposes.
package com;
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface Hello extends Remote {
public String howAreYou (String name) throws RemoteException
}
|
Create a Servlet mapping in your web.xml file that maps |
|
|
In your ejb-jar.xml file, add a <session-endpoint> node. |
|
|
a URL pattern to your service servlet. |
|
|
|
|
|
… |
|
|
|
|
|
|
|
… |
|
|
|
<servlet> |
|
|
<session> |
|
|
<servlet-name>HelloServlet</ servlet-name> |
|
|
<ejb-name>HelloBean</ejb-name> |
|
|
<servlet-class>com.HelloImpl</servlet-class> |
|
|
<service-endpoint>com.Hello</service-endpoint> |
|
|
</servlet> |
|
|
<ejb-class>com.HelloBean</ejb-class> |
|
|
|
|
|
<session-type>Stateless</session-type> |
|
|
<servlet-mapping> |
|
|
<transaction-type>Container</transaction-type> |
|
|
<servlet-name> HelloServlet </servlet-name> |
|
|
</session> |
|
|
<url-pattern>/Hello</url-pattern> |
|
|
|
|
|
</servlet-mapping> |
|
|
|
|
|
… |
|
|
|
|
|
The web.xml file does not contain any web services |
|
|
|
|
|
related information. |
|
|
|
|
|
|
|
|
|
|
Build or generate the WSDL file HelloService.wsdl. This file is a part of the Web services standard and it describes the services you publish along with arguments passed, their types and return values in a platform independent manner. This file can be generated using tools such as wscompile that ships as part of the Java Web Services
308 |
How would you go about …? |
Developer Pack (WSDP)
|
|
|
WSDL |
|
Abstract Definitions |
Types |
<definitions name=”HelloService” targetNamespace=”http://hello.com/” |
||
|
|
|
xmlns:tns=”http://hello.com” xmlns:xsd=”http://www.w3.org/2001/XMLSchema” |
|
|
|
reference |
xmlns:soap=”http://schemas.xmlsoap.org/wsdl/soap” ….. |
|
|
|
> |
||
|
|
|
||
|
|
Messages |
<types /> |
|
|
|
|
||
|
|
|
<message name=”hello_param”> |
|
|
|
reference |
<part name=”arg_1” type=”xsd:string” /> |
|
|
|
|
</message> |
|
Operations |
contains |
PortTypes |
<portType name=”Hello”> |
|
modifier |
|
<operation name=”howAreYou” parameterOrder=”arg_1”> |
||
|
reference |
<input message=”tns:hello_param” /> |
||
|
</portType> |
|||
Concrete Definitions |
||||
|
||||
|
|
|
Operations |
Bindings |
<binding name=”HelloBinding” type=”tns:Hello”> |
|
<soap:binding transport=”http://schemas.xmlsoap.org/soap/http” style=”rpc” /> |
|||
|
contains |
||
|
|
<operation name=”howAreYou” > |
|
|
reference |
<soap:operation soapAction=”” /> |
|
|
|
<input> <soap:body use=”literal” namespace=”http://hello.com/”> </input> |
|
Ports |
Services |
</operation> |
|
</binding> |
|||
|
contains |
|
<service name=”HelloService”>
<port name=”HelloPort” binding=”tns:HelloBinding” > <soap:address location=”http://myServer:8080/hello.do” />
</port>
</service>
</definitions>
Build or generate the JAX-RPC mapping file mapping.xml. Your application server uses this file to map Web service requests to your Servlet. This file can be generated using tools such as wscompile that ships as part of the Java Web Services Developer Pack (WSDP).
|
|
Build a webservices.xml file. |
|
|
Build a webservices.xml file. |
|
|
|
|
|
|
|
|
|
|
|
|
…. |
…. |
|
|
||
|
|
<webservice-description> |
|
|
<webservice-description> |
|
|
|
|
< webservice-description-name> |
|
|
< webservice-description-name> |
|
|
|
|
HelloService |
|
|
HelloService |
|
|
|
|
< webservice-description-name> |
|
|
< webservice-description-name> |
|
|
|
|
<wsdl-file> |
|
|
<wsdl-file> |
|
|
|
|
WEB-INF/wsdl/HelloService.wsdl |
|
|
META-INF/wsdl/HelloService.wsdl |
|
|
|
|
</wsdl-file> |
|
|
</wsdl-file> |
|
|
|
|
<jaxrpc-mapping-file> |
|
|
<jaxrpc-mapping-file> |
|
|
|
|
WEB-INF/mapping.xml |
|
|
META-INF/mapping.xml |
|
|
|
|
</jaxrpc-mapping-file> |
|
|
</jaxrpc-mapping-file> |
|
|
|
|
<port-component> |
|
|
<port-component> |
|
|
|
|
<port-component-name> |
|
|
<port-component-name> |
|
|
|
|
Hello |
|
|
Hello |
|
|
|
|
</port-component-name> |
|
|
</port-component-name> |
|
|
|
|
<wsdl-port>HelloPort</wsdl-port> |
|
|
<wsdl-port>HelloPort</wsdl-port> |
|
|
|
|
<service-endpoint-interface> |
|
|
<service-endpoint-interface> |
|
|
|
|
com.Hello |
|
|
com.Hello |
|
|
|
|
</service-endpoint-interface> |
|
|
</service-endpoint-interface> |
|
|
|
|
<service-impl-bean> |
|
|
<service-impl-bean> |
|
|
|
|
HelloServlet |
|
|
<ejb-link> |
|
|
|
|
</service-impl-bean> |
|
|
HelloBean |
|
|
|
|
</port-component> |
|
|
</ejb-link> |
|
|
|
|
</webservice-description> |
|
|
</service-impl-bean> |
|
|
|
|
…. |
|
|
</port-component> |
|
|
|
|
|
|
|
</webservice-description> |
|
|
|
|
|
|
|
…. |
|
|
|
|
|
|
|
|
|
|
|
|
Assemble the Hello.war file as shown above and deploy |
|
|
Assemble the Hello.jar file as shown above in the |
|
|
|
|
it. |
|
|
diagram and deploy it. |
|
|
How would you go about …? |
309 |
Q. How would you publish and find WSDL descriptions? What are the 4 primary UDDI data types?
A. UDDI provides a method for publishing and finding service descriptions. A complete WSDL service description is a combination of service interface and service implementation document. Since the service interface represents a reusable definition of a service, it is published in a UDDI as a tModel. The service implementation describes instances of a service. Each instance is defined using WSDL service element. Each service element in a service implementation document is used to publish a UDDI business service.
|
WSDL to UDDI mapping |
|
WSDL |
UDDI |
|
Service interface |
BusinessEntity |
|
<types> |
|
|
|
|
|
<messages> |
BusinessService |
|
<portType> |
|
|
<binding> |
|
|
Service implementation |
BindingTemplate |
tModel |
|
|
|
<service> |
BindingTemplate |
|
<port> …. </port> |
|
|
<port> …. </port> |
|
|
<service> |
|
|
|
|
tModel |
There are 4 primary UDDI data types: businessEntity, businessService, bindingTemplate, and tModel.
Q.What are the modes of operations supported by the JAX-RPC?
A.The JAX-RPC supports following 3 modes of operation:
•Synchronous request/response mode – After a remote method is invoked, the client’s thread blocks until a return value is returned or an exception is thrown.
•Fire and forget mode – After a remote method is invoked, the client’s thread is not blocked and it continues processing. A return value or an exception is not expected.
•Non-blocking RPC invocation mode -- After a remote method is invoked, the client’s thread is not blocked and it continues processing. Later, the client processes the remote method return by performing a blocked receive call or by polling for the return value.
Q.How can a client application make a request to a Web service?
A.A client application can make use of one of the following 3 ways (Refer diagram “Publish, Discover, Bind and Invoke using JAX-RPC & JAXR”):
•Invoking methods on a generated static stubs - Based on the contents of a WSDL file description of a service, tools (WSDL2Java) can be used to generate stubs. The client application uses the stubs to invoke remote methods available as a Web service.
•Using a Dynamic Proxy - The client runtime creates a dynamic proxy that supports a Web service endpoint.
•Using a Dynamic Invocation Interface (DII) - Operations on target service endpoints are accessed dynamically based on an in-memory model of the WSDL file description of the service. This eliminates the need for clients to know in advance a service’s exact name and parameters. A DII client can discover this at runtime by using a service broker that can look up the service’s information.
All the above modes are passed through the JAX-RPC client side runtime.
JAXR |
Stands for Java API for XML Registries (JAXR). JAXR provides a uniform way (just like JDBC, JMS, JNDI etc) to use |
|
business registries based on open standards like UDDI, ebXML etc in Java programming language. Businesses can |
|
use registries to register themselves or discover other businesses in a loosely coupled manner. A business can use |
|
JAXR to search a registry for other businesses. |
SAAJ Stands for SOAP with Attachments API for Java. SAAJ enables developers to produce and consume messages conforming to SOAP specifications and provides an abstraction for handling SOAP messages with attachments.