Вы находитесь на странице: 1из 48

www.Intertech-Inc.

com 800-866-9884
COPYRIGHT 2001, INTERTECH, INC.
J AXM
J ava API f or XML
Messaging
www.Intertech-Inc.com 800-866-9884
COPYRIGHT 2001, INTERTECH, INC.
Pr imar y Goal
Get comfortable with JAXM and how
it is used to create Web Services.
www.Intertech-Inc.com 800-866-9884
COPYRIGHT 2001, INTERTECH, INC.
Lear ning Obj ectives
!As a result of this presentation, you
will
be able to:
Understand how JAXM fits in to Java
Web Services
Read and create messages using the SOAP
protocol
Install JAXM into Tomcat and create
simple Web Services
www.Intertech-Inc.com 800-866-9884
COPYRIGHT 2001, INTERTECH, INC.
Speaker s Qual if ications
!Steve is a lead Java Instructor for Intertech,
Inc. (www.intertech-inc.com).
!Steve has written Java classes for Intertech on
topics ranging from JDBC to EJB.
!Steve is currently working on a Server Side
Java book for APress.
!Steve frequently presents technical briefings in
the Minneapolis and Chicago areas on topics
like JSP, EJB, and JAXM.
www.Intertech-Inc.com 800-866-9884
COPYRIGHT 2001, INTERTECH, INC.
Dont Wait Anymor e!
Start today! You have the opportunity
to use Java to work with an exciting
emerging technology. Start now and
you start on the ground floor.
www.Intertech-Inc.com 800-866-9884
COPYRIGHT 2001, INTERTECH, INC.
Pr esentation Agenda
!Introduce the key concepts behind
Web Services
!Describe the key Java XML APIs for
Web Services
!Dissect the XML of SOAP Messages
!Explore the JAXM API
!Setup Tomcat to run Web Services
www.Intertech-Inc.com 800-866-9884
COPYRIGHT 2001, INTERTECH, INC.
The BUZZ
!For at least a year now Web Services
have been the buzz.
!The specifications and acronyms have
been introduced fast and furious.
!It is nearly impossible not to get a little
lost in the haze of XML/SOAP/Java
words.
!For the remainder of the talk I will try
to give you an intro to the topic.
www.Intertech-Inc.com 800-866-9884
COPYRIGHT 2001, INTERTECH, INC.
Web Ser vices
!The idea behind Web Services, is to
make interoperability easier.
Build application that creates a service
used by other applications.
Create applications that combine service.
!Sounds like yet another component
promise doesnt it?
These components are more flexible
because of how they communicate.
www.Intertech-Inc.com 800-866-9884
COPYRIGHT 2001, INTERTECH, INC.
Web Ser vices Basics
!SOAP Simple Object Access Protocol
XML based protocol for information exchange.
!UDDI - Universal Description, Discovery
and Integration
XML based framework that allows sharing,
finding, and registering of global business info.
!WSDL Web Services Description
Language
XML format for description of network services.
www.Intertech-Inc.com 800-866-9884
COPYRIGHT 2001, INTERTECH, INC.
Web Ser vices Basics
UDDI
Registry
Service
Client
Web
Service
1. Service Registers
3. Client calls
Service
2. Client Request
Service Location
www.Intertech-Inc.com 800-866-9884
COPYRIGHT 2001, INTERTECH, INC.
Web Ser vices Basics
!The Diagram for Web Services look no
different then that for RMI.
!The Difference is how registration,
lookup and service calls are made.
Registration of services is done by
sending XML messages
Lookups into UDDI are used by sending
XML messages
Communication between Client and
Service is done using XML messages.
www.Intertech-Inc.com 800-866-9884
COPYRIGHT 2001, INTERTECH, INC.
Web Ser vices Basics
!Using XML allows the client and the service
to be created with different technologies.
XML is language agnostic, I can send a message
using any language on any platform to any
platform.
!Many additional specifications exist and
continue to be created that specify how to
correctly format the XML.
!Java XML APIs (JAX) are being created to
make Web Services easier.
www.Intertech-Inc.com 800-866-9884
COPYRIGHT 2001, INTERTECH, INC.
J AX APIs
!The Winter 01 JAX Pack includes four
JAX APIs
JAXP gives parsing tools (SAX and
DOM) to allow developers to process
XML data.
JAXR is an API that provides standard
registration and lookup services.
JAX-RPC gives a developer easy to use
standard tool to make procedure calls.
JAXM provides standard interfaces to
send and receive SOAP messages.
www.Intertech-Inc.com 800-866-9884
COPYRIGHT 2001, INTERTECH, INC.
J AX APIs
!In a future release you can expect
addition of JAXB.
JAXB gives developers an automatic
conversion between Java Objects and
XML.
!Returning to our simplified Web
Services diagram we can see how JAX
fits in.
www.Intertech-Inc.com 800-866-9884
COPYRIGHT 2001, INTERTECH, INC.
J AX APIs
UDDI
Registry
Service
Client
Web
Service
1. Service Registers
using JAXR
3. Client uses
JAX-RPC or
JAXM to call
service
2. Client does lookup
using JAXR
www.Intertech-Inc.com 800-866-9884
COPYRIGHT 2001, INTERTECH, INC.
J AX APIs
!Some confusion exists between JAXM and
JAX-RPC.
JAX-RPC gives a Java interface to XML RPC
calls as defined in SOAP 1.1.
JAXM is an API that allows users to focus on
sending and receiving messages.
!You can think of JAX-RPC vs. JAXM as
similar to RMI vs. JMS.
JAX-RPC makes XML method calls simple.
JAXM makes XML message sending simple.
www.Intertech-Inc.com 800-866-9884
COPYRIGHT 2001, INTERTECH, INC.
J AXM
!JAXM is similar to Message Oriented
Middleware or JMS.
!JAXM add in profiles.
!Profiles allow developers to plug in higher
level messaging specifications such as
ebXML.
!These Higher level specifications often
contain more functionality beyond basic
SOAP.
!By adding profiles to JAXM developers can
plug-in more XML functionality easily.
www.Intertech-Inc.com 800-866-9884
COPYRIGHT 2001, INTERTECH, INC.
J AXM and SOAP
!Enough summary, to help understand
you need to see some code.
!We will work through a very simple
example.
We will create a server that returns
random messages to questions. (Magic
Eight Ball)
We will look at the code and map the
creation of XML back to the code.
www.Intertech-Inc.com 800-866-9884
COPYRIGHT 2001, INTERTECH, INC.
SOAP
!Before we get into the Java code, a quick
view of SOAP and SOAP terminology.
!Web Services communicate using the SOAP
protocol.
SOAP is Simple Object Access Protocol.
SOAP serves as a packaging protocol, it specifies
the basics on how to exchange information.
SOAP is lightweight protocol and is commonly
extended to add more powerful rules for services.
www.Intertech-Inc.com 800-866-9884
COPYRIGHT 2001, INTERTECH, INC.
SOAP
!SOAP defines the basic structure of
how an XML message will look.
!A SOAP message includes 4 parts.
SOAP Envelope
SOAP Header
SOAP Body
SOAP Attachment
!The SOAP envelope serves as a
container for the SOAP Body and
SOAP Header.
www.Intertech-Inc.com 800-866-9884
COPYRIGHT 2001, INTERTECH, INC.
SOAP
!An empty SOAP message might look
like the below code.
<soap-env:Envelope xmlns:soap-env=
"http://schemas.xmlsoap.org/soap/envelope/" >
<soap-env:Header>
//Header information goes here
</soap-env:Header>
<soap-env:Body>
//Body information goes here
</soap-env:Body>
</soap-env:Envelope>
www.Intertech-Inc.com 800-866-9884
COPYRIGHT 2001, INTERTECH, INC.
SOAP Header
!The header might contain several pieces of
information including the following.
Routing or forwarding information
Transaction or handling information
Security and signature information
Licensing information
!These features are standardized using specs
that build on top of SOAP.
ebXML
SOAP-DSIG
XML RP
www.Intertech-Inc.com 800-866-9884
COPYRIGHT 2001, INTERTECH, INC.
SOAP Body
!The body of a message contains the
information being passed in the
message.
The can include any XML data.
The XML data in the body commonly
contains method invocation.
How the information in the body is laid
out is another more significant question.
There are many additional specs that
dictate the format of a SOAP Body.
www.Intertech-Inc.com 800-866-9884
COPYRIGHT 2001, INTERTECH, INC.
Exampl e SOAP Body
!The following is an example of one
possible SOAP Body.
This body is calling a method named
GetAge passing in the string Emma.
<close:GetAge
xmlns:close="http://www.thecloses.com">
<close:person>Emma</close:person>
</close:GetAge>
www.Intertech-Inc.com 800-866-9884
COPYRIGHT 2001, INTERTECH, INC.
SOAP Attachments
!The Attachment portion of a SOAP
message can be used to pass additional
data.
Anything that conforms to a MIME type
can be passed as an attachment to a SOAP
message.
Each attachment has 2 parts, the MIME
type and the content of the message.
You can add more then one attachment to
a single message.
www.Intertech-Inc.com 800-866-9884
COPYRIGHT 2001, INTERTECH, INC.
J AXM
!Lets create and dissect a simple
JAXM Message Receiving Servlet.
Since we are using HTTP to send XML
messages, so we will create a servlet.
This simple Servlet will respond to every
request in a similar way.
We will not validate, or check the
message sent.
!The outline of the Servlet will look
like the following.
www.Intertech-Inc.com 800-866-9884
COPYRIGHT 2001, INTERTECH, INC.
J AXM
public class MagicEightBall extends JAXMServlet
implements ReqRespListener
{
private MessageFactory fac = null;
public void init(){
//Create a message Factory
}
public SOAPMessage onMessage(
SOAPMessage message)
{
//React to message
}
}
www.Intertech-Inc.com 800-866-9884
COPYRIGHT 2001, INTERTECH, INC.
J AXM
!The class we create extends JAXMServlet to
make coding it easier.
!We implement the ReqRespListener
interface, this is a Service that will reply to
the user.
!To create a Service with no Reply implement
OnewayListener instead.
!For either Oneway or ReqResp you must
implement the onMessage method.
For Oneway onMessage has a void return type.
www.Intertech-Inc.com 800-866-9884
COPYRIGHT 2001, INTERTECH, INC.
public void init(){
try {
fac = MessageFactory.newInstance();
} catch (Exception ex) {
ex.printStackTrace();
}
}
J AXM
!In the init method we will just initialize the
message factory.
This will allow us to send a return message.
www.Intertech-Inc.com 800-866-9884
COPYRIGHT 2001, INTERTECH, INC.
public SOAPMessage onMessage(SOAPMessage message) {
try {
SOAPMessage msg = fac.createMessage();
SOAPEnvelope env = msg.getSOAPPart().getEnvelope();
env.getBody().addChildElement(env.createName
("Response")).addTextNode(getRandomAnswer());
return msg;
} catch(Exception e) {
System.err.println("Error in onMessage: "+ e);
return null;
}
}
J AXM
!The onMessage method will be called when
the MagicEightBall is called.
We simple create an XML message response.
www.Intertech-Inc.com 800-866-9884
COPYRIGHT 2001, INTERTECH, INC.
<soap-env:Envelope xmlns:soap-env=
"http://schemas.xmlsoap.org/soap/envelope/">
<soap-env:Body>
<Result>Dont Know</Result>
</soap-env:Body>
</soap-env:Envelope>
J AXM
!The code in the onMessage method created
the following XML.
The SOAPEnvelop to get a SOAPBody.
The SOAPBody to create XML.
We added a child element with a text element
www.Intertech-Inc.com 800-866-9884
COPYRIGHT 2001, INTERTECH, INC.
J AXM
!To invoke this web Service create a
client application.
!The flow of our Web Service calling
Client is simple.
1)Get a SOAP Connection and create
message.
2)Populate the message with some
significant information.
3)Send the message
www.Intertech-Inc.com 800-866-9884
COPYRIGHT 2001, INTERTECH, INC.
SOAPConnectionFactory conFactory =
SOAPConnectionFactory.newInstance();
SOAPConnection con =
conFactory.createConnection();
MessageFactory mf = MessageFactory.newInstance();
SOAPMessage msg = mf.createMessage();
J AXM
!First step is to get a SOAP Connection
and create a message.
www.Intertech-Inc.com 800-866-9884
COPYRIGHT 2001, INTERTECH, INC.
SOAPPart sp = msg.getSOAPPart();
SOAPEnvelope envelope = sp.getEnvelope();
SOAPBody body = envelope.getBody();
SOAPBodyElement baseElement =
body.addBodyElement(envelope.createName
(EightBall", "m", "http://tempuri.org/message/"));
baseElement.addTextNode(message);
J AXM
!Step Two is to use the message object
to make some XML
www.Intertech-Inc.com 800-866-9884
COPYRIGHT 2001, INTERTECH, INC.
String url = http://www.theclose.com/xyz/EightBall;
URLEndpoint endpoint = new URLEndpoint(url);
SOAPMessage reply = con.call(msg, endpoint);
J AXM
!Third step is to send the message.
A URLEndpoint is created to send the
message directly to the service.
This URL would need to lead to the
JAXM Servlet we created.
www.Intertech-Inc.com 800-866-9884
COPYRIGHT 2001, INTERTECH, INC.
J AXM
! This simple example was created just
to get your feet wet.
We did the simplest of client/server
relationships.
JAXM support use of a remote provider
which we didnt use
JAXM also support ebXML allowing
you to send standard business
communications.
www.Intertech-Inc.com 800-866-9884
COPYRIGHT 2001, INTERTECH, INC.
J AXM
!Remote Providers create a middleman
for our messages.
Messages are not delivered directly to the
consumer of the message.
This middleman is similar to Java
Messaging Service (JMS).
Using a middleman or Message Oriented
Middleware adds flexibility and power to
delivery.
The code used to introduce providers is
not long or involved.
www.Intertech-Inc.com 800-866-9884
COPYRIGHT 2001, INTERTECH, INC.
J AXM
!Beyond the simple basics we got to see
JAXM includes powerful API helpers.
The package com.sun.xml.messaging.ebxml adds
in interfaces to send ebxml messages.
JAXM ships with a small set of tag libraries that
make creation of simple Web Services trivial.
<jaxm:context jndiLookup="Sender"/>
<jaxm:onMessage msgId="received">
<jaxm:soapBody>
<!-- Put soap message here -->
</jaxm:soapBody>
</jaxm:onMessage>
www.Intertech-Inc.com 800-866-9884
COPYRIGHT 2001, INTERTECH, INC.
J AXM
!In 60 minutes its very difficult to show
everything you want to know about one API.
!You need to see it yourself.
!To help you get started, let me show you
how easy it is to get started with JAXM.
!I will quickly walk through the steps to setup
Tomcat and install JAXM in it.
!Examples of tags, ebXML, and remote
providers come with the install.
www.Intertech-Inc.com 800-866-9884
COPYRIGHT 2001, INTERTECH, INC.
Instal l ing J AXM
! There are actually very few steps
needed to get JAXM running.
1) Download and install Tomcat
2) Download and install JAX Pack
3) Transfer a few files from JAX Pack to
Tomcat.
www.Intertech-Inc.com 800-866-9884
COPYRIGHT 2001, INTERTECH, INC.
Instal l ing J AXM
! You can use any Web Server, but Tomcat is
easy enough to install and free.
1) Download Tomcat from
http://jakarta.apache.org/.
2) Unzip the file to a convenient directory, Ill call
this directory <tomcat location> for now.
3) Set a few System variables.
Depending on the version of Tomcat you need to set
either TOMCAT_HOME and or CATALINA home
to be <install location>.
www.Intertech-Inc.com 800-866-9884
COPYRIGHT 2001, INTERTECH, INC.
Instal l ing J AXM
! Installing JAXM is even easier then
Tomcat.
1) Download JAX Pack from
http://java.sun.com/xml/downloads/java
xmlpack.html.
2) Unzip the JAX Pack into some directory,
we will call this <jax location>.
3) If you want to get code to compile you
will need to modify your classpath so the
jar files that ship with JAX Pack are
included.
www.Intertech-Inc.com 800-866-9884
COPYRIGHT 2001, INTERTECH, INC.
Instal l ing J AXM
!The most difficult install is putting JAXM
into Tomcat.
1) The jar files found in <jax location>/jaxm-1.0.1-
ea1/lib must be copied to <tomcat
location>/common/lib.
2) The war files found in <jax location>/jaxm-
1.0.1-ea1/samples must be copied to <tomcat
location>/webapps.
3) To get the provider sample working you need to
copy a war and jar file found in <jax
location>/jaxm-1.0.1-ea1/jaxm to the same
locations that jars and wars were copied in steps
one and two.
www.Intertech-Inc.com 800-866-9884
COPYRIGHT 2001, INTERTECH, INC.
Instal l ing J AXM
!Startup Tomcat by running startup.bat
(Windows) or startup.sh.
!To test if its working open your
browser to http://localhost:8080/jaxm-
simple.
!All the code for the samples is freely
available.
!Architecting, designing and creating
Web Services might prove more
difficult.
www.Intertech-Inc.com 800-866-9884
COPYRIGHT 2001, INTERTECH, INC.
Summar y
!Web Services is still young and this is
the perfect time start working with it.
!JAXM makes coding Web Services
easier.
!Understanding Web Services and its
continuing evolution is not easy.
!With a few hours of work you can take
JAXM for a spin using free tools.
www.Intertech-Inc.com 800-866-9884
COPYRIGHT 2001, INTERTECH, INC.
Why havent you al r eady
star ted?
Get out there! Download JAX Pack get
it working and see how easy it is to get
started using this technology!
www.Intertech-Inc.com 800-866-9884
COPYRIGHT 2001, INTERTECH, INC.
Pr esentation Compl ete
Thanks for attending!
Check out our Java classes
at www.intertech-inc.com
Complete Web Services with
Java
www.Intertech-Inc.com 800-866-9884
COPYRIGHT 2001, INTERTECH, INC.

Вам также может понравиться