Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Java_J2EE_Job_Interview_Companion

.pdf
Скачиваний:
21
Добавлен:
13.05.2015
Размер:
14.25 Mб
Скачать

300

How would you go about …?

Reuse: SOA provides even greater code reuse by utilizing OO development, component based development and also by identifying and organizing well-defined and non-repeatable services into a hierarchy of composite services (e.g. Purchase order service makes use of an authentication service). You can build a new application from a existing service. For example: you can make use of Google’s search service and eBay’s services with the service you intend to provide.

R euse of w ell-defined and non -repeatable services

S ervice do es no t play a w ell d efin ed role : a uthenticates the user and creates a purchase order

S ales S ervice

P u rchase o rder +

A u thentication

 

S ervice

S O A S ervice h as a w ell d efin ed role : separate services to authenticate the user and to crea te a purchase order. P urchase order service m akes use of the authe ntication service . M a rketing S ervice also can ta ke advan tage of the authentication se rvice . T his m eans you can have single sign on m echanism .

 

P u rch ase ord er

S ales S ervice

S ervice

M arketin g S ervice

A uth enticatio n

S ervice

D a ta b a s e P u rc h a s e o rd er d ata

L D A P u ser d ata

D atab ase P u rch ase ord er d ata

L D A P u ser d ata

Loosely coupled: SOA results in loosely coupled application components, in which code is not necessarily tied to a particular database. Loose coupling enables enterprises to plug in new services or upgrade existing services in a modular fashion to react to the new business requirements. For example: An application where a JSP presentation tier is not tightly integrated with the business objects and the data model when access is carried out via a service layer (e.g. a façade -- stateless session EJB).

Loosely coupled service

Tightly coupled - JSP client has to make 4 calls to the business layer

 

 

 

 

 

 

Business Logic Layer

Presentation Tier

JSP)(e.g.

ca

ll

3

 

Business

 

 

 

 

1

 

Business

Business

 

 

all2

 

Object

Object

 

 

c

 

 

 

 

 

 

call

 

 

 

Business

 

 

 

 

call

 

Object

 

 

 

 

4

Object

 

 

 

 

 

 

Data Access

 

Objects (DAO)

Relational

Layer

Database

 

(RDBMS)

Loosely coupled SOA - JSP client has to make 2 coarse-grained calls to the service layer

Presentation Tier

(e.g. JSP)

 

 

Service Layer

 

 

 

Business Logic Layer

call

1

Service 1

call

1

Business

 

(façade - e.g. EJB)

c

Business

 

 

 

 

Object

Object

 

 

 

 

all 2

call 2

Service 2

c

all

3

Business

 

 

Business

 

 

(façade - e.g. EJB)

call 4

 

 

 

Object

Object

 

 

 

 

 

 

 

 

 

 

 

 

 

Data

 

Access

Relational

Objects

(DAO)

Database

(RDBMS)

Layer

 

Coarse grained: Services are also should be coarse grained (i.e. should be a business level service. fine grained services can not only adversely affect performance but also result in tight coupling). For example: You may have a purchase component with individual methods to create a purchase order, add line items, and a customer component to set the customer information etc. Invoking these individual methods locally would not cause any problem but if you use remote service calls then performance problems can be evident. The solution is for the service to provide a single method

How would you go about …?

301

call to create an order, add line items and set the customer details using a façade design pattern by receiving a single DTO (Data Transfer Object) or an XML containing the full data structure for the entire order.

Coarse grained service (using a façade )

 

 

invoke

Service

Service Implementation

 

 

 

Consumer

I

coarse-grained

uses

 

 

Façade

 

 

 

service

method

Interface

e.g. EJB, Web service

 

n

processOrder(...)

 

 

 

 

 

 

 

 

 

t

 

 

 

 

 

 

 

 

 

 

 

e

 

 

 

 

e

 

e

 

 

 

 

r

 

 

 

 

 

 

 

 

 

 

 

 

 

 

n

r s

 

 

 

 

 

t

r

l s

 

 

 

 

 

 

ma

 

 

 

 

 

 

a

i

e

t

 

n

 

 

 

 

e

e

d m

t

 

 

 

 

 

 

 

Ste

 

 

 

 

 

r

d

d e

 

sd

 

 

 

 

 

 

c

r

t

eo

 

 

 

 

 

 

. u

 

e

 

 

 

 

.

o

.

3 c

 

 

 

 

Subsystem

1

 

2

 

 

 

 

t

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Purchase

 

 

Customer

 

 

 

 

 

 

(e.g. EJB)

 

 

(e.g. EJB)

Note: Fine grained services can result in tight coupling. So

 

 

 

 

 

 

 

prefer coarse-grained services.

 

 

 

 

 

 

 

 

Note: SOAs are very popular and there is a huge demand exists for development and implementation of SOAs. Also examine topics such as BPM (Business Process Management) and BPEL (Business Process Execution Language).

Q. What are the best practices to follow when building applications based on SOA?

Choose your implementation technologies carefully. Carefully consider if Web services are suitable. Sometimes using traditional technologies such as Java RMI, EJB and/or JMS may be more appropriate for your use cases than using Web services.

Build coarse grained services as opposed to fine grained services. Fine grained services can not only adversely affect performance but also can result in tightly coupled services.

Services need to have well-defined interfaces (i.e. contract) that are implementation independent. An important aspect of SOA is the separation of the service interface (i.e. the what) from its implementation (i.e. the how). This allows you to change implementation without breaking the contract. The same interface can be shared by many implementations.

XML document should be preferred over Data Transfer Objects (DTOs). Data Transfer Objects (DTOs or aka Value Objects) must respect some of the rules such as:

They must be serializable into XML. Ability to serialize into XML guarantees platform independence.

They must be independent of the data source. Do not include any persistence code. SOA does not go well with Object-Relational mapping tools.

Q. What are the key advantages offered by SOA?

Breaks down the silos of data, applications, and functionalities into enterprise services.

SOA breaks the silo approach

traditional silo approach : tightly coupled & functions are repeated (no reuse) as shown below.

Finance

CRM

 

Investor

application

application

 

Application

 

 

 

 

SOA approach: loosely coupled ,

 

 

 

 

greater reuse etc.

 

Investor

 

 

Finance

 

Application

 

 

 

 

 

 

application

 

 

 

 

 

 

 

 

 

 

 

Service C

 

function A

function F

function A

Service A

 

function C

function C

function C

Data

Data

 

 

function E

function Z

function Y

 

Data

 

Service D

 

 

 

 

 

 

 

CRM

Service B

 

 

 

application

 

Data

Data

Data

 

 

302

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.

How would you go about …?

W e b s e r v i c e s o v e r v i e w

U D D I [f in d ]

S e r v ic e R e q u e s to r

W S

D L ,

 

 

 

 

 

 

 

 

 

 

 

 

S

 

 

[

 

 

 

O

A P

b in d ]

W

 

 

 

 

 

 

 

 

 

S

 

 

 

 

 

 

D

 

 

 

 

 

 

L

 

 

 

 

 

 

,

 

 

 

 

 

S e r v ic e P r o v id e r

S

 

 

 

 

 

O

[

 

 

 

 

A

 

 

 

 

P

b

 

 

 

 

 

 

i

 

 

 

 

 

 

n

 

 

 

 

 

 

d

 

 

 

 

 

 

]

 

 

 

 

 

 

 

 

]

 

 

 

 

 

 

h

 

 

 

 

 

s

 

 

 

 

 

li

 

 

 

 

 

b

 

 

 

 

 

u

 

 

 

 

 

I [p

 

 

 

 

 

 

D

 

 

 

 

 

 

 

D

 

 

 

 

 

 

 

U

 

 

 

 

 

 

 

 

 

 

 

 

 

 

]

 

 

 

 

 

 

 

h

 

 

 

 

 

 

s

 

 

 

 

 

 

li

 

 

 

 

 

 

 

b

 

 

 

 

 

 

u

 

 

 

 

 

p

 

 

 

 

I [

 

 

 

 

D

 

 

 

 

 

 

D

 

 

 

 

 

 

 

U

 

 

 

 

 

 

303

R e g is tr y

 

S e r v ic e P r o v id e r

Let’s look at some of the key terms

What is

Explanation

XML

XML provides the way to structure data and XML provides the foundation on which Web services are built.

 

 

SOAP

SOAP stands for Simple Object Access Protocol. It is an XML based lightweight protocol, which allows software

 

components and application components to communicate, mostly using HTTP (can use SMTP etc). SOAP sits on top

 

of the HTTP protocol. SOAP is nothing but XML message based document with pre-defined format. SOAP is

 

designed to communicate via the Internet in a platform and language neutral manner and allows you to get around

 

firewalls as well. Let’s look at thr structure of a SOAP messages:

A SOAP message MUST be encoded using XML

A SOAP message MUST use the SOAP Envelope namespace

A SOAP message MUST use the SOAP Encoding namespace

A SOAP message must NOT contain a DTD reference

A SOAP message must NOT contain XML Processing Instructions

SOAP M essage (An XM L docum ent)

SO A PPart

SO A PEnvelope

SOAPHeader (optional)

Header

Header

SOAPBody

XM L Content or SOAP fault

Attachm entPart

M IM E Headers

Content (XM L, Im age etc)

Attachm entPart

M IM E Headers

Content (XM L, Im age etc)

<?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:Header>

...

...

</soap:Header> <soap:Body>

...

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

 

 

How would you go about …?

305

 

 

Description

information like the JNDI, which stores the EJB stub references. UDDI has white pages, yellow pages and green

Discovery and

pages. If the retail industry published a UDDI for a price check standard then all the retailers can register their

Integration)

services into this UDDI directory. Shoppers will search the UDDI directory to find the retailer interface. Once the

 

interface is found then the shoppers can communicate with the services immediately.

 

 

The Web services can be registered for public use at http://www.uddi.org. Once the Web service is selected through

 

the UDDI then it can be located using the discovery process.

 

 

Before UDDI, there was no Internet standard for businesses to reach their customers and partners with information

 

about their products and services. Neither was there a method of how to integrate

businesses into each other's

 

systems and processes. UDDI uses WSDL to describe interfaces to Web services.

 

 

 

 

So far we have looked at some open standards/protocols relating to Web services, which enable interoperability between disparate systems (e.g. Between .Net and J2EE etc). These standards provide a common and interoperable approach for defining (WSDL), publishing (UDDI) and using (SOAP) Web services. Now we will look at some of the Java related APIs for Web services. The J2EE 1.4 platform provides comprehensive support for Web services through the JAX-RPC (Java API for XML based RPC (Remote Procedure Call)) and JAXR (Java API for XML Registries). In the J2EE 1.4 platform you can build Web services without knowing anything about the above mentioned XML based standards and protocols. A Web service client accesses the service through the Web container or the EJB container.

Publish, Discover, Bind and Invoke using JAX-RPC & JAXR

Client uses these WSDL documents to see what services are offered and how to contact them.

WSDL

 

 

 

 

 

R

 

 

 

 

 

X

 

 

 

r)

 

 

A

 

 

 

 

 

J

 

 

 

 

 

e

 

 

 

 

 

 

 

v

 

 

 

 

 

 

 

o

 

 

 

 

 

 

 

c

 

 

 

 

 

 

is

 

 

 

 

 

 

D

 

 

 

 

 

 

.

 

 

 

 

 

 

 

 

 

(2

 

 

 

 

 

 

 

 

Web Services

Directory (aka Registry)

WSDL

J

 

A

 

X

 

R

 

(

 

1. P

 

u

 

bli

 

s

 

h)

Service provider stores WSDL documents along with other

documents in the registry

 

 

 

Web Services

 

 

JAX-RPC (3. Bind & Invoke service)

 

 

 

 

Client

 

 

 

 

 

 

 

 

 

 

 

 

 

3 models for

 

JAX-RPC Runtime - client-

 

 

 

invoking Service

 

side

XMLtoJavaMaps Assembles& messageSOAP

 

 

 

 

EndPoint from

 

 

 

 

 

 

 

client

 

actionshandlermessageSOAP

SOAP Msg over HTTP

1

clientRPC-JAX

APIsRPC-JAX

Dynamic

1.request

JavatoXMLMaps

Disassembles& messageSOAP

 

I

 

 

 

 

1

 

 

n

 

 

 

Invocation

 

 

 

 

 

 

 

 

Interface

 

 

 

 

 

t

 

 

 

(DII)

 

 

 

 

 

e

 

 

 

 

 

 

 

 

 

r

 

 

 

 

 

 

 

 

 

n

 

 

 

Dynamic

 

 

 

 

 

e

 

 

 

Proxy

 

 

 

 

 

t

 

 

 

Generated

 

 

2

 

 

 

 

 

 

Stubs

2.response

 

 

 

 

 

 

 

 

Proxy

 

 

 

 

 

SOAP Msg over HTTP

2

 

 

 

 

 

 

 

 

Deployment XML

Web Services

Provider

(Stateless Session Bean,

Servlets etc)

J2EE Container

JAX-RPC Runtime within the J2EE container

--representingPortServiceWeb a network ofinstanceendpointa Web Service (SEI)

messageSOAPhandler actions + containerservices

1

XMLtoJavaMapsMaps XML to Java

Assembles& &Disassembles

messageSOAPSOAPmessage

 

 

 

 

 

 

 

 

 

 

1

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

1

 

 

 

 

 

 

 

 

 

 

 

 

.

 

 

 

 

 

 

 

 

 

 

 

 

r

 

 

 

 

 

 

 

 

 

 

 

e

 

 

 

 

 

 

 

 

 

 

 

q

 

 

 

 

 

 

 

 

 

 

 

u

 

 

 

 

 

 

 

 

 

 

 

e

 

 

 

 

 

 

 

 

 

 

 

s

 

 

 

 

 

 

 

 

 

 

 

 

t

 

 

 

 

 

 

 

 

 

 

 

 

n

s

e

 

 

 

 

 

 

 

 

 

 

 

o

 

 

 

 

 

 

 

 

 

 

 

p

 

 

 

 

 

 

 

 

 

 

r e

s

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

.

 

 

 

 

 

 

 

 

 

 

 

2

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

2

2

 

 

 

 

 

 

 

 

 

 

 

 

Service Implementationon J2EE (Web Tier -- Plain Java class deployed like a Servlet, EJB Tier -- Stateless Session Bean

Keypoints: There are 3 different models (DII, Dynamic Proxy, Generated Stubs Proxy) for invoking a Web Service Endpoint from a client. A client accesses a web service with a Service Endpoint Interface (SEI) as defined by JAX-RPC + port combination. The JAXRPC is used to hide the complexities of SOAP messages (generating, parsing etc) from the developer. This is all taken care of by the JAX-RPC runtime system as shown in shadowed boxes. The developer also does not have to worry about mapping Java to XML and XML back to Java. All these low level work is done behind the scenes and allowing the developer to concentrate on high level issues. The “message handler actions” provides message handling facilities like encryption, decryption, logging, auditing etc to the web service endpoints. A J2EE web service is never accessed directly and it is accessed via the container, thereby taking advantage of the container services such as security, thread management, quality-of-service guarantee etc. J2EE supports web services in the web tier via a plain Java class deployed like a servlet and packaged as a .war file and in the EJB tier via a stateless session bean.

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.

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]